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

Server should accept a context #36

Closed
drewwells opened this issue Oct 3, 2018 · 1 comment
Closed

Server should accept a context #36

drewwells opened this issue Oct 3, 2018 · 1 comment

Comments

@drewwells
Copy link

If I wanted to be notified via a channel that the server has shutdown, I can't currently doing this. Accepting a context and exposing a listening context would make this possible. This way I could write the following code

client code using smux

// Session will be closed by server after 10 seconds
ctx := context.WithTimeout(context.TODO(), 10 * time.Second)
session, err := smux.Server(conn, &smux.Config{Ctx: ctx})

go func() {
  // Get a reference to the server's context, to be notified that the server is complete
  <-session.Context().Done()
  // server has shutdown

  // The convention for returning errors is to expose .Err() method
  err := session.Err()
  if err != nil {
    log.Error(session.Err())
  }
}()

Inside server code, you would create a WithCancel context. Call this cancel when the server shuts down after updating Err()'s return.
smux/server

ctx, cancel := context.WithCancel(config.Ctx)
// cancel() when the server shuts down, notifying those listening to server context
session.ctx = ctx
session.cancel = cancel

func (*s session) Close() {
  session.cancel()
}
@xtaci
Copy link
Owner

xtaci commented Apr 19, 2019

there are 2 ways to shutdown the server

  1. to Close() the session manually
  2. the underlying connection passively closed, then AcceptStream() will return error

@xtaci xtaci closed this as completed Apr 24, 2019
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

No branches or pull requests

2 participants