-
Notifications
You must be signed in to change notification settings - Fork 71
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
Permit per-connection Callbacks to allow per-connection state #148
Permit per-connection Callbacks to allow per-connection state #148
Conversation
Codecov ReportBase: 76.11% // Head: 75.54% // Decreases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## main #148 +/- ##
==========================================
- Coverage 76.11% 75.54% -0.57%
==========================================
Files 24 24
Lines 1834 1865 +31
==========================================
+ Hits 1396 1409 +13
- Misses 326 339 +13
- Partials 112 117 +5
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
I like this due to stronger typing. Earlier I said for WebSocket we may want to avoid keeping this but I think in real world scenarios you inevitably need to keep carry this state for any transport. You typically authenticate in OnConnecting and need to save the tenant id that you get as a result of authenticating somewhere since it is necessary later in OnMessage. Should we get rid of the callback in Settings and replace it with this per connection Callbacks? |
I agree.
Good example. I think it is critical that we at least figure out a way to connect OnConnecting and OnMessage and this approach feels right.
I considered that, but wanted to make sure this PR was small and easy to understand. Changing the callbacks requires changes to the Edit: Before actually merging this PR, I plan to add tests for the specific behavior and am happy to refactor the Callbacks. One benefit of this implementation is compatibility with existing configurations, but there probably aren't many out there and if the same implementation is used for both callbacks, it's easy enough to return it in the ConnectionResponse. |
@tigrannajaryan I finally had a chance to refactor this PR to separate the Callbacks and ConnectionCallbacks and update the tests. Please review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally I like this direction, just left some minor comments.
@@ -28,6 +26,11 @@ type Callbacks interface { | |||
// non-zero value to indicate the rejection reason (typically 401, 429 or 503). | |||
// HTTPResponseHeader may be optionally set (e.g. "Retry-After: 30"). | |||
OnConnecting(request *http.Request) ConnectionResponse |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add some comments to explain how the ConnectionHandler field in the returned struct is used, can it be nil or must be set, etc. Or perhaps add comments to the ConnectionResponse struct to explain it there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added that it MUST be set if Accept=true and added a comment to the ConnectionCallbacks interface.
server/types/callbacks.go
Outdated
@@ -11,12 +11,10 @@ type ConnectionResponse struct { | |||
Accept bool | |||
HTTPStatusCode int | |||
HTTPResponseHeader map[string]string | |||
ConnectionHandler ConnectionCallbacks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: should the field be also named ConnectionCallbacks? Or rename ConnectionCallbacks struct to ConnectionHandler?
The names being different somehow is not helping me to read.
server/serverimpl.go
Outdated
@@ -292,22 +296,22 @@ func (s *server) handlePlainHTTPRequest(req *http.Request, w http.ResponseWriter | |||
conn: connFromRequest(req), | |||
} | |||
|
|||
if s.settings.Callbacks == nil { | |||
if connectionHandler == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably need to document somewhere that the connection callback is required for plain http connections.
…comments describing ConnectionCallbacks usage
@andykellr I think this can be merged. |
This adds a ConnectionHandler field to the ConnectionResponse struct. If nil, the Callbacks provided with StartSettings would be used.
I don't have time at the moment to provide a working demonstration of maintaining per-connection state, but hopefully this is obvious.
The implementation would look something like this:
The callbacks passed to StartSettings.Settings would construct a new connectionState in OnConnecting.