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

Add SSERouter #3

Merged
merged 13 commits into from
Jan 10, 2020
Merged

Add SSERouter #3

merged 13 commits into from
Jan 10, 2020

Conversation

m110
Copy link
Member

@m110 m110 commented Nov 11, 2019

This is a proposal for a thin abstraction of SSE router and handlers.

The idea is that it's enough to prepare two method for implementing a SSE handler:

  • GetResponse - returns a model of response to be marshalled and rendered. Typically, this will get some kind of identifier from request (r) and fetch the payload from a repository / application query handler.
  • Validate - checks if the incoming message should be handled. For example, the handler could consume post-updated topic, which has updates to all posts in the system. This method should unmarshall the payload and check if the ID matches the one in request (r) - or even better, check in message's metadata if available.

See tests for an example implementation.

How the handler works:

  • If there's no event-stream in Accept header, return model from ResponseProvider.
  • Otherwise, return model from GetResponse and start the stream.
  • Check each incoming Message with Validate and push new model to stream each time it passes.

Out of scope / TODO:

  • Tests
  • Better interface and method names
  • Godoc
  • Consider adding "ping" functionality to keep the connection alive.
    • chi seems to not support this. Maybe in future we can add our own responder.
  • Will add a full example in separate PR to the main repository.

pkg/http/sse.go Outdated Show resolved Hide resolved
pkg/http/sse.go Outdated Show resolved Hide resolved
pkg/http/sse.go Outdated Show resolved Hide resolved
pkg/http/sse.go Outdated Show resolved Hide resolved
pkg/http/sse.go Outdated Show resolved Hide resolved
pkg/http/sse.go Outdated Show resolved Hide resolved
pkg/http/sse.go Outdated Show resolved Hide resolved
@m110 m110 changed the title Add SSEHandler proposal Add SSERouter Nov 22, 2019
pkg/http/sse.go Outdated Show resolved Hide resolved
pkg/http/sse.go Show resolved Hide resolved
pkg/http/sse.go Show resolved Hide resolved
logger watermill.LoggerAdapter
}

func NewSSERouter(
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we could add some documentation about what to pass as upstreamRouter here. upstreamSubscriber is maybe a little bit more intuitive, but what is the router here for?
What arguments to pass to the constructor?

I figure that there will be an example for SSE in due time, but I feel that a godoc might help

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, will add it. 👍

pkg/http/sse_test.go Outdated Show resolved Hide resolved
pkg/http/sse_test.go Outdated Show resolved Hide resolved
@m110 m110 merged commit f9529a1 into master Jan 10, 2020
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.

4 participants