forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
127 lines (112 loc) · 3.89 KB
/
config.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package comm
import (
"time"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
)
var (
// Is the configuration cached?
configurationCached = false
// Is TLS enabled
tlsEnabled bool
// Max send and receive bytes for grpc clients and servers
maxRecvMsgSize = 100 * 1024 * 1024
maxSendMsgSize = 100 * 1024 * 1024
// Default keepalive options
keepaliveOptions = KeepaliveOptions{
ClientKeepaliveTime: 300, // 5 min
ClientKeepaliveTimeout: 20, // 20 sec - gRPC default
ServerKeepaliveTime: 7200, // 2 hours - gRPC default
ServerKeepaliveTimeout: 20, // 20 sec - gRPC default
}
)
// KeepAliveOptions is used to set the gRPC keepalive settings for both
// clients and servers
type KeepaliveOptions struct {
// ClientKeepaliveTime is the duration in seconds after which if the client
// does not see any activity from the server it pings the server to see
// if it is alive
ClientKeepaliveTime int
// ClientKeepaliveTimeout is the duration the client waits for a response
// from the server after sending a ping before closing the connection
ClientKeepaliveTimeout int
// ServerKeepaliveTime is the duration in seconds after which if the server
// does not see any activity from the client it pings the client to see
// if it is alive
ServerKeepaliveTime int
// ServerKeepaliveTimeout is the duration the server waits for a response
// from the client after sending a ping before closing the connection
ServerKeepaliveTimeout int
}
// cacheConfiguration caches common package scoped variables
func cacheConfiguration() {
if !configurationCached {
tlsEnabled = viper.GetBool("peer.tls.enabled")
configurationCached = true
}
}
// TLSEnabled return cached value for "peer.tls.enabled" configuration value
func TLSEnabled() bool {
if !configurationCached {
cacheConfiguration()
}
return tlsEnabled
}
// MaxRecvMsgSize returns the maximum message size in bytes that gRPC clients
// and servers can receive
func MaxRecvMsgSize() int {
return maxRecvMsgSize
}
// SetMaxRecvMsgSize sets the maximum message size in bytes that gRPC clients
// and servers can receive
func SetMaxRecvMsgSize(size int) {
maxRecvMsgSize = size
}
// MaxSendMsgSize returns the maximum message size in bytes that gRPC clients
// and servers can send
func MaxSendMsgSize() int {
return maxSendMsgSize
}
// SetMaxSendMsgSize sets the maximum message size in bytes that gRPC clients
// and servers can send
func SetMaxSendMsgSize(size int) {
maxSendMsgSize = size
}
// SetKeepaliveOptions sets the gRPC keepalive options for both clients and
// servers
func SetKeepaliveOptions(ka KeepaliveOptions) {
keepaliveOptions = ka
}
// ServerKeepaliveOptions returns the gRPC keepalive options for servers
func ServerKeepaliveOptions() []grpc.ServerOption {
var serverOpts []grpc.ServerOption
kap := keepalive.ServerParameters{
Time: time.Duration(keepaliveOptions.ServerKeepaliveTime) * time.Second,
Timeout: time.Duration(keepaliveOptions.ServerKeepaliveTimeout) * time.Second,
}
serverOpts = append(serverOpts, grpc.KeepaliveParams(kap))
kep := keepalive.EnforcementPolicy{
// needs to match clientKeepalive
MinTime: time.Duration(keepaliveOptions.ClientKeepaliveTime) * time.Second,
// allow keepalive w/o rpc
PermitWithoutStream: true,
}
serverOpts = append(serverOpts, grpc.KeepaliveEnforcementPolicy(kep))
return serverOpts
}
// ClientKeepaliveOptions returns the gRPC keepalive options for clients
func ClientKeepaliveOptions() []grpc.DialOption {
var dialOpts []grpc.DialOption
kap := keepalive.ClientParameters{
Time: time.Duration(keepaliveOptions.ClientKeepaliveTime) * time.Second,
Timeout: time.Duration(keepaliveOptions.ClientKeepaliveTimeout) * time.Second,
PermitWithoutStream: true,
}
dialOpts = append(dialOpts, grpc.WithKeepaliveParams(kap))
return dialOpts
}