-
Notifications
You must be signed in to change notification settings - Fork 661
/
client.go
105 lines (93 loc) · 3.94 KB
/
client.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package grpcutils
import (
"fmt"
"math"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive"
)
const (
// After a duration of this time if the client doesn't see any activity it
// pings the server to see if the transport is still alive.
// If set below 10s, a minimum value of 10s will be used instead.
// grpc-go default infinity
defaultClientKeepAliveTime = 30 * time.Second
// After having pinged for keepalive check, the client waits for a duration
// of Timeout and if no activity is seen even after that the connection is
// closed. grpc-go default 20s
defaultClientKeepAliveTimeOut = 10 * time.Second
// If true, client sends keepalive pings even with no active RPCs. If false,
// when there are no active RPCs, Time and Timeout will be ignored and no
// keepalive pings will be sent. grpc-go default false
defaultPermitWithoutStream = true
// WaitForReady configures the action to take when an RPC is attempted on
// broken connections or unreachable servers. If waitForReady is false and
// the connection is in the TRANSIENT_FAILURE state, the RPC will fail
// immediately. Otherwise, the RPC client will block the call until a
// connection is available (or the call is canceled or times out) and will
// retry the call if it fails due to a transient error. gRPC will not retry
// if data was written to the wire unless the server indicates it did not
// process the data. Please refer to
// https://github.com/grpc/grpc/blob/master/doc/wait-for-ready.md.
//
// gRPC default behavior is to NOT "wait for ready".
defaultWaitForReady = true
)
var DefaultDialOptions = []grpc.DialOption{
grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(math.MaxInt),
grpc.MaxCallSendMsgSize(math.MaxInt),
grpc.WaitForReady(defaultWaitForReady),
),
grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: defaultClientKeepAliveTime,
Timeout: defaultClientKeepAliveTimeOut,
PermitWithoutStream: defaultPermitWithoutStream,
}),
grpc.WithTransportCredentials(insecure.NewCredentials()),
}
// gRPC clients created from this ClientConn will wait forever for the Server to
// become Ready. If you desire a dial timeout ensure context is properly plumbed
// to the client and use context.WithTimeout.
//
// Dial returns a gRPC ClientConn with the dial options as defined by
// DefaultDialOptions. DialOption can also optionally be passed.
func Dial(addr string, opts ...DialOption) (*grpc.ClientConn, error) {
return grpc.Dial(fmt.Sprintf("passthrough:///%s", addr), newDialOpts(opts...)...)
}
// DialOptions are options which can be applied to a gRPC client in addition to
// the defaults set by DefaultDialOptions.
type DialOptions struct {
opts []grpc.DialOption
}
// append(DefaultDialOptions, ...) will always allocate a new slice and will
// not overwrite any potential data that may have previously been appended to
// DefaultServerOptions https://go.dev/ref/spec#Composite_literals
func newDialOpts(opts ...DialOption) []grpc.DialOption {
d := &DialOptions{opts: DefaultDialOptions}
d.applyOpts(opts)
return d.opts
}
func (d *DialOptions) applyOpts(opts []DialOption) {
for _, opt := range opts {
opt(d)
}
}
type DialOption func(*DialOptions)
// WithChainUnaryInterceptor takes a list of unary client interceptors which
// are added to the dial options.
func WithChainUnaryInterceptor(interceptors ...grpc.UnaryClientInterceptor) DialOption {
return func(d *DialOptions) {
d.opts = append(d.opts, grpc.WithChainUnaryInterceptor(interceptors...))
}
}
// WithChainStreamInterceptor takes a list of stream client interceptors which
// are added to the dial options.
func WithChainStreamInterceptor(interceptors ...grpc.StreamClientInterceptor) DialOption {
return func(d *DialOptions) {
d.opts = append(d.opts, grpc.WithChainStreamInterceptor(interceptors...))
}
}