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

Conversation

slinkydeveloper
Copy link
Member

I came out with this proposal after working on #606.

My problem with the Client interface is that, while Send and Request maps the interfaces in protocol, StartReceiver doesn't map the semantics of Receiver and Responder. This is particularly weird with websocket for example, when you have a bi-directional client per stream, so you can do something like:

c.Send()
[...]
c.Receive()

In particular, I find that StartReceiver is opinionated, maps to the concepts of http more than the concepts of messaging and it's less golang idiomatic than a channel or a method like Receive. Another point to consider is that going from the method invocation to the handler mode is relatively straightforward (and we could easily provide the method to do it, like showed in this PR), while going from the handler mode to the method invocation is harder and, generally, more error prone.

Signed-off-by: Francesco Guardiani [email protected]

Signed-off-by: Francesco Guardiani <[email protected]>
// * 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)

@n3wscott
Copy link
Member

n3wscott commented Jan 7, 2021

Think about a v3?

@slinkydeveloper
Copy link
Member Author

Maybe, let's get the 2.4 out first 😄

@duglin
Copy link
Contributor

duglin commented Jun 1, 2023

Would someone like to follow through on this one? If not we're probably going to close it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants