Skip to content

Commit

Permalink
FailFast(false), return helpful error message when context is time out
Browse files Browse the repository at this point in the history
  • Loading branch information
yuqitao committed Apr 23, 2019
1 parent 3fc7430 commit 6727225
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion picker_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,15 @@ func doneChannelzWrapper(acw *acBalancerWrapper, done func(balancer.DoneInfo)) f
// - the subConn returned by the current picker is not READY
// When one of these situations happens, pick blocks until the picker gets updated.
func (bp *pickerWrapper) pick(ctx context.Context, failfast bool, opts balancer.PickOptions) (transport.ClientTransport, func(balancer.DoneInfo), error) {
var ch chan struct{}
var (
ch chan struct{}

// when the current picker returns balancer.ErrTransientFailure and failfast is false,
// this function will block until context is done.
// To return some helpful connection messages, like tls config error message,
// use pickErr to save balancer.ErrTransientFailure which occurred.
pickErr error
)

for {
bp.mu.Lock()
Expand All @@ -120,6 +128,14 @@ func (bp *pickerWrapper) pick(ctx context.Context, failfast bool, opts balancer.
bp.mu.Unlock()
select {
case <-ctx.Done():
if pickErr != nil {
switch ctx.Err() {
case context.DeadlineExceeded:
return nil, nil, status.Errorf(codes.DeadlineExceeded, "latest connection error: %v", bp.connectionError())
case context.Canceled:
return nil, nil, status.Errorf(codes.Canceled, "latest connection error: %v", bp.connectionError())
}
}
return nil, nil, ctx.Err()
case <-ch:
}
Expand All @@ -138,6 +154,7 @@ func (bp *pickerWrapper) pick(ctx context.Context, failfast bool, opts balancer.
continue
case balancer.ErrTransientFailure:
if !failfast {
pickErr = err
continue
}
return nil, nil, status.Errorf(codes.Unavailable, "%v, latest connection error: %v", err, bp.connectionError())
Expand Down

0 comments on commit 6727225

Please sign in to comment.