Skip to content

Commit

Permalink
add backoff to client config
Browse files Browse the repository at this point in the history
Signed-off-by: Allen Ray <alray@redhat.com>
  • Loading branch information
dusk125 committed Jan 24, 2024
1 parent 15f95ec commit 0857def
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
19 changes: 17 additions & 2 deletions client/v3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,30 @@ func (c *Client) dialSetupOpts(creds grpccredentials.TransportCredentials, dopts
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}

unaryMaxRetries := defaultUnaryMaxRetries
if c.cfg.MaxUnaryRetries > 0 {
unaryMaxRetries = c.cfg.MaxUnaryRetries
}

backoffWaitBetween := defaultBackoffWaitBetween
if c.cfg.BackoffWaitBetween > 0 {
backoffWaitBetween = c.cfg.BackoffWaitBetween
}

backoffJitterFraction := defaultBackoffJitterFraction
if c.cfg.BackoffJitterFraction > 0 {
backoffJitterFraction = c.cfg.BackoffJitterFraction
}

// Interceptor retry and backoff.
// TODO: Replace all of clientv3/retry.go with RetryPolicy:
// https://github.com/grpc/grpc-proto/blob/cdd9ed5c3d3f87aef62f373b93361cf7bddc620d/grpc/service_config/service_config.proto#L130
rrBackoff := withBackoff(c.roundRobinQuorumBackoff(defaultBackoffWaitBetween, defaultBackoffJitterFraction))
rrBackoff := withBackoff(c.roundRobinQuorumBackoff(backoffWaitBetween, backoffJitterFraction))
opts = append(opts,
// Disable stream retry by default since go-grpc-middleware/retry does not support client streams.
// Streams that are safe to retry are enabled individually.
grpc.WithStreamInterceptor(c.streamClientInterceptor(withMax(0), rrBackoff)),
grpc.WithUnaryInterceptor(c.unaryClientInterceptor(withMax(defaultUnaryMaxRetries), rrBackoff)),
grpc.WithUnaryInterceptor(c.unaryClientInterceptor(withMax(unaryMaxRetries), rrBackoff)),
)

return opts
Expand Down
42 changes: 42 additions & 0 deletions client/v3/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,48 @@ func TestDialNoTimeout(t *testing.T) {
c.Close()
}

func TestMaxUnaryRetries(t *testing.T) {
maxUnaryRetries := uint(10)
cfg := Config{
Endpoints: []string{"127.0.0.1:12345"},
MaxUnaryRetries: maxUnaryRetries,
}
c, err := NewClient(t, cfg)
require.NoError(t, err)
require.NotNil(t, c)
defer c.Close()

require.Equal(t, maxUnaryRetries, c.cfg.MaxUnaryRetries)
}

func TestBackoff(t *testing.T) {
backoffWaitBetween := 100 * time.Millisecond
cfg := Config{
Endpoints: []string{"127.0.0.1:12345"},
BackoffWaitBetween: backoffWaitBetween,
}
c, err := NewClient(t, cfg)
require.NoError(t, err)
require.NotNil(t, c)
defer c.Close()

require.Equal(t, backoffWaitBetween, c.cfg.BackoffWaitBetween)
}

func TestBackoffJitterFraction(t *testing.T) {
backoffJitterFraction := float64(0.9)
cfg := Config{
Endpoints: []string{"127.0.0.1:12345"},
BackoffJitterFraction: backoffJitterFraction,
}
c, err := NewClient(t, cfg)
require.NoError(t, err)
require.NotNil(t, c)
defer c.Close()

require.Equal(t, backoffJitterFraction, c.cfg.BackoffJitterFraction)
}

func TestIsHaltErr(t *testing.T) {
assert.Equal(t,
isHaltErr(context.TODO(), errors.New("etcdserver: some etcdserver error")),
Expand Down
9 changes: 9 additions & 0 deletions client/v3/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ type Config struct {
// PermitWithoutStream when set will allow client to send keepalive pings to server without any active streams(RPCs).
PermitWithoutStream bool `json:"permit-without-stream"`

// MaxUnaryRetries is the maximum number of retries for unary RPCs.
MaxUnaryRetries uint `json:"max-unary-retries"`

// BackoffWaitBetween is the wait time before retrying an RPC.
BackoffWaitBetween time.Duration `json:"backoff-wait-between"`

// BackoffJitterFraction is the jitter fraction to randomize backoff wait time.
BackoffJitterFraction float64 `json:"backoff-jitter-fraction"`

// TODO: support custom balancer picker
}

Expand Down

0 comments on commit 0857def

Please sign in to comment.