From 61218004c029da38f985dfc2988619ac93e4bcc5 Mon Sep 17 00:00:00 2001 From: Gyuho Lee Date: Wed, 2 Jan 2019 23:34:51 -0800 Subject: [PATCH] clientv3: remove "JitterUp" imports Signed-off-by: Gyuho Lee --- clientv3/client.go | 5 +---- clientv3/retry_interceptor.go | 3 +-- clientv3/utils.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 clientv3/utils.go diff --git a/clientv3/client.go b/clientv3/client.go index ed908b83b..3cc770e08 100644 --- a/clientv3/client.go +++ b/clientv3/client.go @@ -28,14 +28,11 @@ import ( "time" "github.com/google/uuid" - - "github.com/grpc-ecosystem/go-grpc-middleware/util/backoffutils" "go.etcd.io/etcd/clientv3/balancer" "go.etcd.io/etcd/clientv3/balancer/picker" "go.etcd.io/etcd/clientv3/balancer/resolver/endpoint" "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes" "go.uber.org/zap" - "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/credentials" @@ -530,7 +527,7 @@ func (c *Client) roundRobinQuorumBackoff(waitBetween time.Duration, jitterFracti quorum := (n/2 + 1) if attempt%quorum == 0 { c.lg.Info("backoff", zap.Uint("attempt", attempt), zap.Uint("quorum", quorum), zap.Duration("waitBetween", waitBetween), zap.Float64("jitterFraction", jitterFraction)) - return backoffutils.JitterUp(waitBetween, jitterFraction) + return jitterUp(waitBetween, jitterFraction) } c.lg.Info("backoff skipped", zap.Uint("attempt", attempt), zap.Uint("quorum", quorum)) return 0 diff --git a/clientv3/retry_interceptor.go b/clientv3/retry_interceptor.go index f8ebe44e4..088d86ae7 100644 --- a/clientv3/retry_interceptor.go +++ b/clientv3/retry_interceptor.go @@ -23,7 +23,6 @@ import ( "sync" "time" - "github.com/grpc-ecosystem/go-grpc-middleware/util/backoffutils" "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes" "go.uber.org/zap" "google.golang.org/grpc" @@ -377,6 +376,6 @@ func filterCallOptions(callOptions []grpc.CallOption) (grpcOptions []grpc.CallOp // For example waitBetween=1s and jitter=0.10 can generate waits between 900ms and 1100ms. func backoffLinearWithJitter(waitBetween time.Duration, jitterFraction float64) backoffFunc { return func(attempt uint) time.Duration { - return backoffutils.JitterUp(waitBetween, jitterFraction) + return jitterUp(waitBetween, jitterFraction) } } diff --git a/clientv3/utils.go b/clientv3/utils.go new file mode 100644 index 000000000..850275877 --- /dev/null +++ b/clientv3/utils.go @@ -0,0 +1,31 @@ +// Copyright 2018 The etcd Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package clientv3 + +import ( + "math/rand" + "time" +) + +// jitterUp adds random jitter to the duration. +// +// This adds or subtracts time from the duration within a given jitter fraction. +// For example for 10s and jitter 0.1, it will return a time within [9s, 11s]) +// +// Reference: https://godoc.org/github.com/grpc-ecosystem/go-grpc-middleware/util/backoffutils +func jitterUp(duration time.Duration, jitter float64) time.Duration { + multiplier := jitter * (rand.Float64()*2 - 1) + return time.Duration(float64(duration) * (1 + multiplier)) +}