Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Braindump] Proposal for redesigning the client interface #607

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 26 additions & 18 deletions v2/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,32 @@ type Client interface {
// transport and return any response event.
Request(ctx context.Context, event event.Event) (*event.Event, protocol.Result)

// StartReceiver will register the provided function for callback on receipt
// of a cloudevent. It will also start the underlying protocol as it has
// been configured.
// This call is blocking.
// Valid fn signatures are:
// * func()
// * func() error
// * func(context.Context)
// * func(context.Context) protocol.Result
// * func(event.Event)
// * func(event.Event) protocol.Result
// * func(context.Context, event.Event)
// * func(context.Context, event.Event) protocol.Result
// * func(event.Event) *event.Event
// * func(event.Event) (*event.Event, protocol.Result)
// * func(context.Context, event.Event) *event.Event
// * func(context.Context, event.Event) (*event.Event, protocol.Result)
StartReceiver(ctx context.Context, fn interface{}) error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about leaving StartReceiver and also add the blocking single pulling functions?

you still need a hook on client to give it a thread, I am not seeing how that is happening without dramatically changing the contract.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about leaving StartReceiver and also add the blocking single pulling functions?

So one of the points with this proposal is that I would love to relax this assumption on the protocol side https://github.com/cloudevents/sdk-go/pull/606/files#diff-484d929efc01852c00d94e8e519500757204bacd6a67224c50db3c805eabc8f5R12 and enforce only in StartReceiver, where it really matters. Nor StartReceiver or invoking from multiple threads Receive makes sense in the context of Websockets.

My worry is that, If we put everything in Client, we'll have races everywhere, because we need to handle a lot of different cases:

  • user invokes Receive, it's blocked on Receive but then invokes StartReceiver
  • user invokes StartReceiver, then invokes Receive
  • OpenInbound for StartReceiver, then user invokes Receive too
  • StartReceiver context is canceled, but then user invokes Receive

While, if we make it clear that StartReceiver is like one level up/different to the client, e.g. accepting directly the Protocol in StartReceiver, then we have a clean implementation, both for StartReceiver and for Client.Receive. And also makes pretty clear to the user the usage aka don't mess up with Receive and StartReceiver at the same time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe want you are saying is we need a new implementation for a stream based client. Client is for more like pub/sub style apis, and we can create a new one specific for streams like WS provides?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty much, but in particular my point is that I see the pub/sub APIs as a layer on top of a stream API (in fact, protocol APIs are already stream apis)

Receive(ctx context.Context) (event.Event, error)

Respond(ctx context.Context) (event.Event, ResponseFn, error)
}

type ResponseFn func(ctx context.Context, e *event.Event, r protocol.Result) error

// StartEventHandler will register the provided function for callback on receipt
// of a cloudevent. It will also start the underlying protocol as it has
// been configured.
// This call is blocking.
// Valid fn signatures are:
// * func()
// * func() error
// * func(context.Context)
// * func(context.Context) protocol.Result
// * func(event.Event)
// * func(event.Event) protocol.Result
// * func(context.Context, event.Event)
// * func(context.Context, event.Event) protocol.Result
// * func(event.Event) *event.Event
// * func(event.Event) (*event.Event, protocol.Result)
// * func(context.Context, event.Event) *event.Event
// * func(context.Context, event.Event) (*event.Event, protocol.Result)
func StartEventHandler(ctx context.Context, fn interface{}) error {

}

// New produces a new client with the provided transport object and applied
Expand Down