-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
128 lines (105 loc) · 4.39 KB
/
options.go
File metadata and controls
128 lines (105 loc) · 4.39 KB
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
// 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.
//
// Modifications copyright (C) 2023 chengyayu.
package grpcpool
import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive"
"time"
)
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 如此时长后客户端未收到响应则会 ping 服务端。
KeepAliveTime = time.Duration(10) * time.Second
// KeepAliveTimeout 客户端 ping 服务端后,如此时长没响应会关闭连接。
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
)
const (
// DftMaxIdle see options.MaxIdle
DftMaxIdle = int(8)
// DftMaxActive see options.MaxActive
DftMaxActive = int(64)
// DftMaxConcurrentStreams see options.MaxConcurrentStreams
DftMaxConcurrentStreams = int(64)
)
// Option is an options setting function.
type Option func(o *options)
// 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)
// maxIdle is a maximum number of idle connections in the pool.
maxIdle int
// maxActive is a 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
}
// Dial with factory function for *grpc.ClientConn
func Dial(factoryFn func(address string) (*grpc.ClientConn, error)) Option {
return func(o *options) { o.dial = factoryFn }
}
// MaxIdle with pool maxIdle
func MaxIdle(maxIdle int) Option {
return func(o *options) { o.maxIdle = maxIdle }
}
// MaxActive with pool maxActive
func MaxActive(maxActive int) Option {
return func(o *options) { o.maxActive = maxActive }
}
// MaxConcurrentStreams with pool maxConcurrentStreams
func MaxConcurrentStreams(maxConcurrentStreams int) Option {
return func(o *options) { o.maxConcurrentStreams = maxConcurrentStreams }
}
// Reuse with pool reuse
func Reuse(reuse bool) Option {
return func(o *options) { o.reuse = reuse }
}
// DftDial return a grpc connection with defined configurations.
func DftDial(address string) (*grpc.ClientConn, error) {
ctx, cancel := context.WithTimeout(context.Background(), DialTimeout)
defer cancel()
return grpc.DialContext(ctx, address,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBackoffMaxDelay(BackoffMaxDelay),
grpc.WithInitialWindowSize(InitialWindowSize),
grpc.WithInitialConnWindowSize(InitialConnWindowSize),
grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(MaxSendMsgSize), grpc.MaxCallRecvMsgSize(MaxRecvMsgSize)),
grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: KeepAliveTime,
Timeout: KeepAliveTimeout,
PermitWithoutStream: true,
}))
}