-
Notifications
You must be signed in to change notification settings - Fork 63
Cancel Handler context when connection closes #90
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
Conversation
conn_test.go
Outdated
| func newClientServer(handler jsonrpc2.Handler) (client *jsonrpc2.Conn, server *jsonrpc2.Conn) { | ||
| // Pipe returns two jsonrpc2.Conn, connected via a synchronous, in-memory, full | ||
| // duplex network connection. | ||
| func Pipe(handlerA, handlerB jsonrpc2.Handler) (connA *jsonrpc2.Conn, connB *jsonrpc2.Conn) { |
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.
I delivered the newClientServer function in #67. I regret naming it that (despite it only existing in the jsonrpc2_test package and not publicly facing) because JSON-RPC is bidirectional where both sides can be the client and the server. I therefore renamed this function to Pipe to mirror the name of the underlying net.Pipe.
While this change is off-topic, I included it in this pull request because the new test uses this function. If you'd like me to break it out then please let me know.
81edf1e to
9ca8fdc
Compare
keegancsmith
left a comment
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.
LGTM. I think this is a good change, even though technically it changes the behaviour. One minor bit of feedback. Do you think it makes sense to remove the disconnect channel and replace it with the context channel?
Great question! The short version of my answer is that we should not replace the I realized that if the user cancels the context that they're giving to
Personally I prefer option 3 because that is the behavior I would expect. I added a ctx, cancel := context.WithCancel(context.Background())
conn := jsonrpc2.NewConn(ctx, myStream, myHandler)
cancel()
<-conn.DisconnectNotify()
conn.Close() // should return jsonrpc2.ErrClosedIf we replaced the |
keegancsmith
left a comment
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.
Sorry for missing the update. I agree with the updated implementation. It makes sense to close if ctx is closed.
| go func() { | ||
| <-ctx.Done() | ||
| c.close(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.
right so this is safe since we will always call cancel when done. lgtm
No worries, I'm sure you're busy. 🙂 Thank you very much for reviewing and merging, @keegancsmith! |
The changes in this pull request cancel the context that is given to the Handler when the connection is closed.
Before this change, the user of this library had to do something like this:
Having to manually cancel the context when the connection is disconnected is unexpected. For comparison, the following is what the documentation states for the request context in the net/http package: