github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/core/comm/config.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package comm 8 9 import ( 10 "time" 11 12 "github.com/spf13/viper" 13 "google.golang.org/grpc" 14 "google.golang.org/grpc/keepalive" 15 ) 16 17 var ( 18 // Is the configuration cached? 19 configurationCached = false 20 // Is TLS enabled 21 tlsEnabled bool 22 // Max send and receive bytes for grpc clients and servers 23 maxRecvMsgSize = 100 * 1024 * 1024 24 maxSendMsgSize = 100 * 1024 * 1024 25 // Default keepalive options 26 keepaliveOptions = KeepaliveOptions{ 27 ClientKeepaliveTime: 300, // 5 min 28 ClientKeepaliveTimeout: 20, // 20 sec - gRPC default 29 ServerKeepaliveTime: 7200, // 2 hours - gRPC default 30 ServerKeepaliveTimeout: 20, // 20 sec - gRPC default 31 } 32 ) 33 34 // KeepAliveOptions is used to set the gRPC keepalive settings for both 35 // clients and servers 36 type KeepaliveOptions struct { 37 // ClientKeepaliveTime is the duration in seconds after which if the client 38 // does not see any activity from the server it pings the server to see 39 // if it is alive 40 ClientKeepaliveTime int 41 // ClientKeepaliveTimeout is the duration the client waits for a response 42 // from the server after sending a ping before closing the connection 43 ClientKeepaliveTimeout int 44 // ServerKeepaliveTime is the duration in seconds after which if the server 45 // does not see any activity from the client it pings the client to see 46 // if it is alive 47 ServerKeepaliveTime int 48 // ServerKeepaliveTimeout is the duration the server waits for a response 49 // from the client after sending a ping before closing the connection 50 ServerKeepaliveTimeout int 51 } 52 53 // cacheConfiguration caches common package scoped variables 54 func cacheConfiguration() { 55 if !configurationCached { 56 tlsEnabled = viper.GetBool("peer.tls.enabled") 57 configurationCached = true 58 } 59 } 60 61 // TLSEnabled return cached value for "peer.tls.enabled" configuration value 62 func TLSEnabled() bool { 63 if !configurationCached { 64 cacheConfiguration() 65 } 66 return tlsEnabled 67 } 68 69 // MaxRecvMsgSize returns the maximum message size in bytes that gRPC clients 70 // and servers can receive 71 func MaxRecvMsgSize() int { 72 return maxRecvMsgSize 73 } 74 75 // SetMaxRecvMsgSize sets the maximum message size in bytes that gRPC clients 76 // and servers can receive 77 func SetMaxRecvMsgSize(size int) { 78 maxRecvMsgSize = size 79 } 80 81 // MaxSendMsgSize returns the maximum message size in bytes that gRPC clients 82 // and servers can send 83 func MaxSendMsgSize() int { 84 return maxSendMsgSize 85 } 86 87 // SetMaxSendMsgSize sets the maximum message size in bytes that gRPC clients 88 // and servers can send 89 func SetMaxSendMsgSize(size int) { 90 maxSendMsgSize = size 91 } 92 93 // SetKeepaliveOptions sets the gRPC keepalive options for both clients and 94 // servers 95 func SetKeepaliveOptions(ka KeepaliveOptions) { 96 keepaliveOptions = ka 97 } 98 99 // ServerKeepaliveOptions returns the gRPC keepalive options for servers 100 func ServerKeepaliveOptions() []grpc.ServerOption { 101 var serverOpts []grpc.ServerOption 102 kap := keepalive.ServerParameters{ 103 Time: time.Duration(keepaliveOptions.ServerKeepaliveTime) * time.Second, 104 Timeout: time.Duration(keepaliveOptions.ServerKeepaliveTimeout) * time.Second, 105 } 106 serverOpts = append(serverOpts, grpc.KeepaliveParams(kap)) 107 kep := keepalive.EnforcementPolicy{ 108 // needs to match clientKeepalive 109 MinTime: time.Duration(keepaliveOptions.ClientKeepaliveTime) * time.Second, 110 // allow keepalive w/o rpc 111 PermitWithoutStream: true, 112 } 113 serverOpts = append(serverOpts, grpc.KeepaliveEnforcementPolicy(kep)) 114 return serverOpts 115 } 116 117 // ClientKeepaliveOptions returns the gRPC keepalive options for clients 118 func ClientKeepaliveOptions() []grpc.DialOption { 119 var dialOpts []grpc.DialOption 120 kap := keepalive.ClientParameters{ 121 Time: time.Duration(keepaliveOptions.ClientKeepaliveTime) * time.Second, 122 Timeout: time.Duration(keepaliveOptions.ClientKeepaliveTimeout) * time.Second, 123 PermitWithoutStream: true, 124 } 125 dialOpts = append(dialOpts, grpc.WithKeepaliveParams(kap)) 126 return dialOpts 127 }