Skip to content

Commit

Permalink
wip: Fix signal shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
shazow committed Nov 20, 2019
1 parent 667db73 commit 581e4d8
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,19 @@ type Client struct {
// Serve starts the async request and response goroutine consumers.
func (client *Client) Serve(ctx context.Context) error {
respCh := make(chan Response, chanBuffer)
defer close(respCh)

g, ctx := errgroup.WithContext(ctx)

g.Go(func() error {
for resp := range respCh {
client.Stats.Add(resp.Err, resp.Elapsed)
// TODO: Relay response to client.Out
for {
select {
case resp := <-respCh:
client.Stats.Add(resp.Err, resp.Elapsed)
// TODO: Relay response to client.Out
case <-ctx.Done():
return ctx.Err()
}
}
return nil
})
Expand All @@ -59,18 +65,16 @@ func (client *Client) Serve(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
logger.Debug().Str("endpoint", client.Endpoint).Msg("shutting down client")
return nil
case req := <-client.In:
respCh <- req.Do(t)
}
}
})
}

err := g.Wait()

close(respCh)
return err
return g.Wait()
}

var id int
Expand Down

0 comments on commit 581e4d8

Please sign in to comment.