/
grpc_dialer.go
166 lines (149 loc) · 6.76 KB
/
grpc_dialer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package internal
import (
"context"
"time"
"github.com/gogo/status"
grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
"github.com/uber-go/tally"
"go.temporal.io/api/serviceerror"
"go.temporal.io/sdk/internal/common/retry"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/metadata"
"go.temporal.io/sdk/internal/common/metrics"
)
type (
// dialParameters are passed to GRPCDialer and must be used to create gRPC connection.
dialParameters struct {
HostPort string
UserConnectionOptions ConnectionOptions
RequiredInterceptors []grpc.UnaryClientInterceptor
DefaultServiceConfig string
}
)
const (
// LocalHostPort is a default host:port for worker and client to connect to.
LocalHostPort = "localhost:7233"
// defaultServiceConfig is a default gRPC connection service config which enables DNS round-robin between IPs.
defaultServiceConfig = `{"loadBalancingConfig": [{"round_robin":{}}]}`
// minConnectTimeout is the minimum amount of time we are willing to give a connection to complete.
minConnectTimeout = 20 * time.Second
// attemptSuffix is a suffix added to the metric name for individual call attempts made to the server, which includes retries.
attemptSuffix = "_attempt"
)
func dial(params dialParameters) (*grpc.ClientConn, error) {
var securityOptions []grpc.DialOption
if params.UserConnectionOptions.TLS != nil {
securityOptions = []grpc.DialOption{
grpc.WithTransportCredentials(credentials.NewTLS(params.UserConnectionOptions.TLS)),
}
} else {
securityOptions = []grpc.DialOption{
grpc.WithInsecure(),
grpc.WithAuthority(params.UserConnectionOptions.Authority),
}
}
// gRPC maintains connection pool inside grpc.ClientConn.
// This connection pool has auto reconnect feature.
// If connection goes down, gRPC will try to reconnect using exponential backoff strategy:
// https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md.
// Default MaxDelay is 120 seconds which is too high.
// Setting it to retryPollOperationMaxInterval here will correlate with poll reconnect interval.
var cp = grpc.ConnectParams{
Backoff: backoff.DefaultConfig,
MinConnectTimeout: minConnectTimeout,
}
cp.Backoff.BaseDelay = retryPollOperationInitialInterval
cp.Backoff.MaxDelay = retryPollOperationMaxInterval
opts := []grpc.DialOption{
grpc.WithChainUnaryInterceptor(params.RequiredInterceptors...),
grpc.WithDefaultServiceConfig(params.DefaultServiceConfig),
grpc.WithConnectParams(cp),
}
opts = append(opts, securityOptions...)
if params.UserConnectionOptions.EnableKeepAliveCheck {
// gRPC utilizes keep alive mechanism to detect dead connections in case if server didn't close them
// gracefully. Client would ping the server periodically and expect replies withing the specified timeout.
// Learn more by reading https://github.com/grpc/grpc/blob/master/doc/keepalive.md
var kap = keepalive.ClientParameters{
Time: params.UserConnectionOptions.KeepAliveTime,
Timeout: params.UserConnectionOptions.KeepAliveTimeout,
PermitWithoutStream: params.UserConnectionOptions.KeepAlivePermitWithoutStream,
}
opts = append(opts, grpc.WithKeepaliveParams(kap))
}
return grpc.Dial(params.HostPort, opts...)
}
func requiredInterceptors(metricScope tally.Scope, headersProvider HeadersProvider, controller TrafficController) []grpc.UnaryClientInterceptor {
interceptors := []grpc.UnaryClientInterceptor{
errorInterceptor,
// Report aggregated metrics for the call, this is done outside of the retry loop.
metrics.NewGRPCMetricsInterceptor(metricScope, ""),
// By default the grpc retry interceptor *is disabled*, preventing accidental use of retries.
// We add call options for retry configuration based on the values present in the context.
retry.NewRetryOptionsInterceptor(),
// Performs retries *IF* retry options are set for the call.
grpc_retry.UnaryClientInterceptor(),
// Report metrics for every call made to the server.
metrics.NewGRPCMetricsInterceptor(metricScope, attemptSuffix),
}
if headersProvider != nil {
interceptors = append(interceptors, headersProviderInterceptor(headersProvider))
}
if controller != nil {
interceptors = append(interceptors, trafficControllerInterceptor(controller))
}
return interceptors
}
func trafficControllerInterceptor(controller TrafficController) grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
err := controller.CheckCallAllowed(ctx, method, req, reply)
// Break execution chain and return an error without sending actual request to the server.
if err != nil {
return err
}
return invoker(ctx, method, req, reply, cc, opts...)
}
}
func headersProviderInterceptor(headersProvider HeadersProvider) grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
headers, err := headersProvider.GetHeaders(ctx)
if err != nil {
return err
}
for k, v := range headers {
ctx = metadata.AppendToOutgoingContext(ctx, k, v)
}
return invoker(ctx, method, req, reply, cc, opts...)
}
}
func errorInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
err := invoker(ctx, method, req, reply, cc, opts...)
err = serviceerror.FromStatus(status.Convert(err))
return err
}