backoff: allow configuration of backoff strategy
The backoff function and parameters have been pulled up into an interface `backoffStrategy`. The default parameters are now part of a package variable `DefaultBackoffConfig`. The strategy is then plumbed through `dialOptions`. As a result of this PR, the maximum backoff delay can now be set using the `WithBackoffConfig` dial option. While the addition of strategy may seem premature, this allows one to simply export `BackoffStrategy` and `WithBackoff` to allow arbirarily configurable backoff strategies. Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
@ -75,6 +75,7 @@ type dialOptions struct {
|
||||
codec Codec
|
||||
cp Compressor
|
||||
dc Decompressor
|
||||
bs backoffStrategy
|
||||
picker Picker
|
||||
block bool
|
||||
insecure bool
|
||||
@ -114,6 +115,22 @@ func WithPicker(p Picker) DialOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WithBackoffConfig configures the dialer to use the provided backoff
|
||||
// parameters after connection failures.
|
||||
func WithBackoffConfig(b *BackoffConfig) DialOption {
|
||||
return withBackoff(b)
|
||||
}
|
||||
|
||||
// withBackoff sets the backoff strategy used for retries after a
|
||||
// failed connection attempt.
|
||||
//
|
||||
// This can be exported if arbitrary backoff strategies are allowed by GRPC.
|
||||
func withBackoff(bs backoffStrategy) DialOption {
|
||||
return func(o *dialOptions) {
|
||||
o.bs = bs
|
||||
}
|
||||
}
|
||||
|
||||
// WithBlock returns a DialOption which makes caller of Dial blocks until the underlying
|
||||
// connection is up. Without this, Dial returns immediately and connecting the server
|
||||
// happens in background.
|
||||
@ -180,6 +197,11 @@ func Dial(target string, opts ...DialOption) (*ClientConn, error) {
|
||||
// Set the default codec.
|
||||
cc.dopts.codec = protoCodec{}
|
||||
}
|
||||
|
||||
if cc.dopts.bs == nil {
|
||||
cc.dopts.bs = DefaultBackoffConfig
|
||||
}
|
||||
|
||||
if cc.dopts.picker == nil {
|
||||
cc.dopts.picker = &unicastPicker{
|
||||
target: target,
|
||||
@ -416,7 +438,7 @@ func (cc *Conn) resetTransport(closeTransport bool) error {
|
||||
return ErrClientConnTimeout
|
||||
}
|
||||
}
|
||||
sleepTime := backoff(retries)
|
||||
sleepTime := cc.dopts.bs.backoff(retries)
|
||||
timeout := sleepTime
|
||||
if timeout < minConnectTimeout {
|
||||
timeout = minConnectTimeout
|
||||
|
||||
Reference in New Issue
Block a user