-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathoptions.go
109 lines (90 loc) · 3.99 KB
/
options.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
// Copyright 2019 shimingyah. All rights reserved.
//
// 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.
// ee the License for the specific language governing permissions and
// limitations under the License.
package pool
import (
"context"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
)
const (
// DialTimeout the timeout of create connection
DialTimeout = 5 * time.Second
// BackoffMaxDelay provided maximum delay when backing off after failed connection attempts.
BackoffMaxDelay = 3 * time.Second
// KeepAliveTime is the duration of time after which if the client doesn't see
// any activity it pings the server to see if the transport is still alive.
KeepAliveTime = time.Duration(10) * time.Second
// KeepAliveTimeout is the duration of time for which the client waits after having
// pinged for keepalive check and if no activity is seen even after that the connection
// is closed.
KeepAliveTimeout = time.Duration(3) * time.Second
// InitialWindowSize we set it 1GB is to provide system's throughput.
InitialWindowSize = 1 << 30
// InitialConnWindowSize we set it 1GB is to provide system's throughput.
InitialConnWindowSize = 1 << 30
// MaxSendMsgSize set max gRPC request message size sent to server.
// If any request message size is larger than current value, an error will be reported from gRPC.
MaxSendMsgSize = 4 << 30
// MaxRecvMsgSize set max gRPC receive message size received from server.
// If any message size is larger than current value, an error will be reported from gRPC.
MaxRecvMsgSize = 4 << 30
)
// Options are params for creating grpc connect pool.
type Options struct {
// Dial is an application supplied function for creating and configuring a connection.
Dial func(address string) (*grpc.ClientConn, error)
// Maximum number of idle connections in the pool.
MaxIdle int
// Maximum number of connections allocated by the pool at a given time.
// When zero, there is no limit on the number of connections in the pool.
MaxActive int
// MaxConcurrentStreams limit on the number of concurrent streams to each single connection
MaxConcurrentStreams int
// If Reuse is true and the pool is at the MaxActive limit, then Get() reuse
// the connection to return, If Reuse is false and the pool is at the MaxActive limit,
// create a one-time connection to return.
Reuse bool
}
// DefaultOptions sets a list of recommended options for good performance.
// Feel free to modify these to suit your needs.
var DefaultOptions = Options{
Dial: Dial,
MaxIdle: 8,
MaxActive: 64,
MaxConcurrentStreams: 64,
Reuse: true,
}
// Dial return a grpc connection with defined configurations.
func Dial(address string) (*grpc.ClientConn, error) {
ctx, cancel := context.WithTimeout(context.Background(), DialTimeout)
defer cancel()
return grpc.DialContext(ctx, address, grpc.WithInsecure(),
grpc.WithBackoffMaxDelay(BackoffMaxDelay),
grpc.WithInitialWindowSize(InitialWindowSize),
grpc.WithInitialConnWindowSize(InitialConnWindowSize),
grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(MaxSendMsgSize)),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(MaxRecvMsgSize)),
grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: KeepAliveTime,
Timeout: KeepAliveTimeout,
PermitWithoutStream: true,
}))
}
// DialTest return a simple grpc connection with defined configurations.
func DialTest(address string) (*grpc.ClientConn, error) {
ctx, cancel := context.WithTimeout(context.Background(), DialTimeout)
defer cancel()
return grpc.DialContext(ctx, address, grpc.WithInsecure())
}