TUN-2703: Muxer.Serve terminates when its context is Done

This commit is contained in:
Adam Chalmers 2020-02-19 18:57:29 -06:00
parent a83b6a2155
commit 269351bbea
1 changed files with 39 additions and 12 deletions

View File

@ -321,24 +321,51 @@ func joinErrorsWithTimeout(errChan <-chan error, receiveCount int, timeout time.
func (m *Muxer) Serve(ctx context.Context) error { func (m *Muxer) Serve(ctx context.Context) error {
errGroup, _ := errgroup.WithContext(ctx) errGroup, _ := errgroup.WithContext(ctx)
errGroup.Go(func() error { errGroup.Go(func() error {
ch := make(chan error)
go func() {
err := m.muxReader.run(m.config.Logger) err := m.muxReader.run(m.config.Logger)
m.explicitShutdown.Fuse(false) m.explicitShutdown.Fuse(false)
m.r.Close() m.r.Close()
m.abort() m.abort()
ch <- err
}()
select {
case err := <-ch:
return err return err
case <-ctx.Done():
return ctx.Err()
}
}) })
errGroup.Go(func() error { errGroup.Go(func() error {
ch := make(chan error)
go func() {
err := m.muxWriter.run(m.config.Logger) err := m.muxWriter.run(m.config.Logger)
m.explicitShutdown.Fuse(false) m.explicitShutdown.Fuse(false)
m.w.Close() m.w.Close()
m.abort() m.abort()
ch <- err
}()
select {
case err := <-ch:
return err return err
case <-ctx.Done():
return ctx.Err()
}
}) })
errGroup.Go(func() error { errGroup.Go(func() error {
ch := make(chan error)
go func() {
err := m.muxMetricsUpdater.run(m.config.Logger) err := m.muxMetricsUpdater.run(m.config.Logger)
ch <- err
}()
select {
case err := <-ch:
return err return err
case <-ctx.Done():
return ctx.Err()
}
}) })
err := errGroup.Wait() err := errGroup.Wait()