New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
client: add WithConnectParams to configure connection backoff and timeout #2960
Changes from 6 commits
32275fc
f8b4003
0f7db76
0115f00
dea6444
f5998d1
3fce38e
e1a1470
ebebd7d
cc7872c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,16 +23,36 @@ package grpc | |
|
||
import ( | ||
"time" | ||
|
||
grpcbackoff "google.golang.org/grpc/backoff" | ||
) | ||
|
||
// DefaultBackoffConfig uses values specified for backoff in | ||
// https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md. | ||
// | ||
// Deprecated: use ConnectParams instead. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please clarify that this will be supported throughout 1.x (copy/paste similar deprecation comments elsewhere in grpc package). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
var DefaultBackoffConfig = BackoffConfig{ | ||
MaxDelay: 120 * time.Second, | ||
} | ||
|
||
// BackoffConfig defines the parameters for the default gRPC backoff strategy. | ||
// | ||
// Deprecated: use ConnectParams instead. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
type BackoffConfig struct { | ||
// MaxDelay is the upper bound of backoff delay. | ||
MaxDelay time.Duration | ||
} | ||
|
||
// ConnectParams defines the parameters for connecting and retrying. Users are | ||
// encouraged to use this instead of the BackoffConfig type defined above. See | ||
// here for more details: | ||
// https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md. | ||
// | ||
// This API is EXPERIMENTAL. | ||
type ConnectParams struct { | ||
// Backoff specifies the configuration options for connection backoff. | ||
Backoff grpcbackoff.Config | ||
// MinConnectTimeout is the minimum amount of time we are willing to give a | ||
// connection to complete. | ||
MinConnectTimeout time.Duration | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* | ||
* Copyright 2019 gRPC 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 backoff provides configuration options for connection backoff. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/connection//? This can be (and is/will be) used for other things as well as connection backoffs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
package backoff | ||
|
||
import "time" | ||
|
||
// Config defines the configuration options for connection backoff. More | ||
// details can be found at | ||
// https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md. | ||
// | ||
// This API is EXPERIMENTAL. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move this comment to the whole package level instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was actually referring to the "EXPERIMENTAL" part. Please mark the whole file as experimental. Following the same wording as
|
||
type Config struct { | ||
// BaseDelay is the amount of time to backoff after the first failure. | ||
BaseDelay time.Duration | ||
// Multiplier is the factor with which to multiply backoffs after a | ||
// failed retry. Should ideally be greater than 1. | ||
Multiplier float64 | ||
// Jitter is the factor with which backoffs are randomized. | ||
Jitter float64 | ||
// MaxDelay is the upper bound of backoff delay. | ||
MaxDelay time.Duration | ||
} | ||
|
||
// DefaultConfig is a backoff configuration with the default values specfied | ||
// at https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md. | ||
// | ||
// This should be useful for callers who want to configure backoff with | ||
// non-default values only for a subset of the options. | ||
// | ||
// This API is EXPERIMENTAL. | ||
var DefaultConfig = Config{ | ||
BaseDelay: 1.0 * time.Second, | ||
Multiplier: 1.6, | ||
Jitter: 0.2, | ||
MaxDelay: 120 * time.Second, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ import ( | |
"time" | ||
|
||
"golang.org/x/net/http2" | ||
grpcbackoff "google.golang.org/grpc/backoff" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not to nit-pick, but I would prefer to rename There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
"google.golang.org/grpc/connectivity" | ||
"google.golang.org/grpc/credentials" | ||
"google.golang.org/grpc/internal/backoff" | ||
|
@@ -655,22 +656,39 @@ func (s) TestCredentialsMisuse(t *testing.T) { | |
} | ||
|
||
func (s) TestWithBackoffConfigDefault(t *testing.T) { | ||
testBackoffConfigSet(t, &DefaultBackoffConfig) | ||
testBackoffConfigSet(t, backoff.DefaultExponential) | ||
} | ||
|
||
func (s) TestWithBackoffConfig(t *testing.T) { | ||
b := BackoffConfig{MaxDelay: DefaultBackoffConfig.MaxDelay / 2} | ||
expected := b | ||
testBackoffConfigSet(t, &expected, WithBackoffConfig(b)) | ||
bc := grpcbackoff.DefaultConfig | ||
bc.MaxDelay = b.MaxDelay | ||
wantBackoff := backoff.Exponential{Config: bc} | ||
testBackoffConfigSet(t, wantBackoff, WithBackoffConfig(b)) | ||
} | ||
|
||
func (s) TestWithBackoffMaxDelay(t *testing.T) { | ||
md := DefaultBackoffConfig.MaxDelay / 2 | ||
expected := BackoffConfig{MaxDelay: md} | ||
testBackoffConfigSet(t, &expected, WithBackoffMaxDelay(md)) | ||
bc := grpcbackoff.DefaultConfig | ||
bc.MaxDelay = md | ||
wantBackoff := backoff.Exponential{Config: bc} | ||
testBackoffConfigSet(t, wantBackoff, WithBackoffMaxDelay(md)) | ||
} | ||
|
||
func testBackoffConfigSet(t *testing.T, expected *BackoffConfig, opts ...DialOption) { | ||
func (s) TestWithConnectParams(t *testing.T) { | ||
bd := 2 * time.Second | ||
mltpr := 2.0 | ||
jitter := 0.0 | ||
bc := grpcbackoff.Config{BaseDelay: bd, Multiplier: mltpr, Jitter: jitter} | ||
|
||
crt := ConnectParams{Backoff: bc} | ||
// MaxDelay is not set in the ConnectParams. So it should not be set on | ||
// backoff.Exponential as well. | ||
wantBackoff := backoff.Exponential{Config: bc} | ||
testBackoffConfigSet(t, wantBackoff, WithConnectParams(crt)) | ||
} | ||
|
||
func testBackoffConfigSet(t *testing.T, wantBackoff backoff.Exponential, opts ...DialOption) { | ||
opts = append(opts, WithInsecure()) | ||
conn, err := Dial("passthrough:///foo:80", opts...) | ||
if err != nil { | ||
|
@@ -682,16 +700,27 @@ func testBackoffConfigSet(t *testing.T, expected *BackoffConfig, opts ...DialOpt | |
t.Fatalf("backoff config not set") | ||
} | ||
|
||
actual, ok := conn.dopts.bs.(backoff.Exponential) | ||
gotBackoff, ok := conn.dopts.bs.(backoff.Exponential) | ||
if !ok { | ||
t.Fatalf("unexpected type of backoff config: %#v", conn.dopts.bs) | ||
} | ||
|
||
expectedValue := backoff.Exponential{ | ||
MaxDelay: expected.MaxDelay, | ||
if gotBackoff != wantBackoff { | ||
t.Fatalf("unexpected backoff config on connection: %v, want %v", gotBackoff, wantBackoff) | ||
} | ||
if actual != expectedValue { | ||
t.Fatalf("unexpected backoff config on connection: %v, want %v", actual, expected) | ||
} | ||
|
||
func (s) TestConnectParamsWithMinConnectTimeout(t *testing.T) { | ||
// Default value specified for minConnectTimeout in the spec is 20 seconds. | ||
mct := 1 * time.Minute | ||
conn, err := Dial("passthrough:///foo:80", WithInsecure(), WithConnectParams(ConnectParams{MinConnectTimeout: mct})) | ||
if err != nil { | ||
t.Fatalf("unexpected error dialing connection: %v", err) | ||
} | ||
defer conn.Close() | ||
|
||
if got := conn.dopts.minConnectTimeout(); got != mct { | ||
t.Errorf("unexpect minConnectTimeout on the connection: %v, want %v", got, mct) | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ import ( | |
"net" | ||
"time" | ||
|
||
grpcbackoff "google.golang.org/grpc/backoff" | ||
"google.golang.org/grpc/balancer" | ||
"google.golang.org/grpc/credentials" | ||
"google.golang.org/grpc/grpclog" | ||
|
@@ -246,21 +247,34 @@ func WithServiceConfig(c <-chan ServiceConfig) DialOption { | |
}) | ||
} | ||
|
||
// WithConnectParams configures the dialer to use the provided ConnectParams. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the best place to document our defaults? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made the comments a bit more verbose. |
||
// | ||
// This API is EXPERIMENTAL. | ||
func WithConnectParams(p ConnectParams) DialOption { | ||
return newFuncDialOption(func(o *dialOptions) { | ||
o.bs = backoff.Exponential{Config: p.Backoff} | ||
o.minConnectTimeout = func() time.Duration { | ||
return p.MinConnectTimeout | ||
} | ||
}) | ||
} | ||
|
||
// WithBackoffMaxDelay configures the dialer to use the provided maximum delay | ||
// when backing off after failed connection attempts. | ||
// | ||
//Deprecated: use WithConnectParams instead. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Strange character (between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see any strange character here :) |
||
func WithBackoffMaxDelay(md time.Duration) DialOption { | ||
return WithBackoffConfig(BackoffConfig{MaxDelay: md}) | ||
} | ||
|
||
// WithBackoffConfig configures the dialer to use the provided backoff | ||
// parameters after connection failures. | ||
// | ||
// Use WithBackoffMaxDelay until more parameters on BackoffConfig are opened up | ||
// for use. | ||
//Deprecated: use WithConnectParams instead. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
func WithBackoffConfig(b BackoffConfig) DialOption { | ||
return withBackoff(backoff.Exponential{ | ||
MaxDelay: b.MaxDelay, | ||
}) | ||
bc := grpcbackoff.DefaultConfig | ||
bc.MaxDelay = b.MaxDelay | ||
return withBackoff(backoff.Exponential{Config: bc}) | ||
} | ||
|
||
// withBackoff sets the backoff strategy used for connectRetryNum after a failed | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to rename in this file. I'm worried a renaming here might affect godoc for the ConnectParams field (? not sure about it though).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Removed unnecessary import renaming.
Hmm .... does import renaming affect godoc? I couldn't find any documentation to that effect.