-
Notifications
You must be signed in to change notification settings - Fork 83
client/server: Don't block the main connection loop for transport IO #73
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -318,6 +318,7 @@ func (c *serverConn) run(sctx context.Context) { | |
| active int | ||
| state connState = connStateIdle | ||
| responses = make(chan response) | ||
| responseErr = make(chan error) | ||
| requests = make(chan request) | ||
| recvErr = make(chan error, 1) | ||
| shutdown = c.shutdown | ||
|
|
@@ -412,6 +413,36 @@ func (c *serverConn) run(sctx context.Context) { | |
| } | ||
| }(recvErr) | ||
|
|
||
| go func(responseErr chan error) { | ||
| for { | ||
| select { | ||
| // We don't want a case for c.shutdown here, as that would cause us to exit | ||
| // immediately when it is signaled, rather than waiting for any active requests | ||
| // to complete first. Instead, once all the active requests have completed, | ||
| // the main loop will return and close done, which will cause us to exit as well. | ||
| case <-done: | ||
| return | ||
| case response := <-responses: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be slightly clearer to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, maybe that's not practical since
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, generally you don't want to close from the read side. |
||
| p, err := c.server.codec.Marshal(response.resp) | ||
| if err != nil { | ||
| logrus.WithError(err).Error("failed marshaling response") | ||
| responseErr <- err | ||
| return | ||
| } | ||
|
|
||
| if err := ch.send(response.id, messageTypeResponse, p); err != nil { | ||
| logrus.WithError(err).Error("failed sending message on channel") | ||
| responseErr <- err | ||
| return | ||
| } | ||
|
|
||
| // Send a nil error so that the main loop knows an active request has | ||
| // completed successfully. | ||
| responseErr <- nil | ||
| } | ||
| } | ||
| }(responseErr) | ||
|
|
||
| for { | ||
| newstate := state | ||
| switch { | ||
|
|
@@ -449,18 +480,12 @@ func (c *serverConn) run(sctx context.Context) { | |
| case <-done: | ||
| } | ||
| }(request.id) | ||
| case response := <-responses: | ||
| p, err := c.server.codec.Marshal(response.resp) | ||
| case err := <-responseErr: | ||
| // responseErr sends nil if no error occurred in sending the response. | ||
| // In that case we just decrement the active count and continue. | ||
| if err != nil { | ||
| logrus.WithError(err).Error("failed marshaling response") | ||
| return | ||
| } | ||
|
|
||
| if err := ch.send(response.id, messageTypeResponse, p); err != nil { | ||
| logrus.WithError(err).Error("failed sending message on channel") | ||
| return | ||
| } | ||
|
|
||
| active-- | ||
| case err := <-recvErr: | ||
| // TODO(stevvooe): Not wildly clear what we should do in this | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.