Skip to content

Commit

Permalink
shared_client: Use configured timeout also for response
Browse files Browse the repository at this point in the history
Use the configured read timeout to bound the time spent on receiving the
response, instead of waiting for a full minute.

Signed-off-by: Jarno Rajahalme <[email protected]>
  • Loading branch information
jrajahalme committed Apr 29, 2024
1 parent 8ff80cc commit b4c25b7
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions shared_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,19 @@ func (c *SharedClient) ExchangeSharedContext(ctx context.Context, m *Msg) (r *Ms
return nil, 0, ctx.Err()
}

// Since c.requests is unbuffered, the handler is guaranteed to eventually close 'respCh'
// Since c.requests is unbuffered, the handler is guaranteed to eventually close 'respCh',
// unless the response never arrives.
// Quit when ctx is done so that each request times out individually in case there is no
// response at all.
readTimeout := c.getTimeoutForRequest(c.Client.readTimeout())
ctx2, cancel2 := context.WithTimeout(ctx, readTimeout)
defer cancel2()
select {
case resp := <-respCh:
return resp.msg, resp.rtt, resp.err
// This is just fail-safe mechanism in case there is another similar issue
case <-time.After(time.Minute):
return nil, 0, fmt.Errorf("timeout waiting for response")
case <-ctx2.Done():
// TODO: handler now has a stale waiter that remains if the response never arrives.
return nil, 0, ctx2.Err()
}
}

Expand Down

0 comments on commit b4c25b7

Please sign in to comment.