dubbo.apache.org/dubbo-go/v3@v3.1.1/remoting/getty/config.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package getty 19 20 import ( 21 "time" 22 ) 23 24 import ( 25 getty "github.com/apache/dubbo-getty" 26 27 perrors "github.com/pkg/errors" 28 ) 29 30 import ( 31 "dubbo.apache.org/dubbo-go/v3/config" 32 ) 33 34 const ( 35 TCPReadWriteTimeoutMinValue = time.Second * 1 36 ) 37 38 type ( 39 // GettySessionParam is session configuration for getty 40 GettySessionParam struct { 41 CompressEncoding bool `default:"false" yaml:"compress-encoding" json:"compress-encoding,omitempty"` 42 TcpNoDelay bool `default:"true" yaml:"tcp-no-delay" json:"tcp-no-delay,omitempty"` 43 TcpKeepAlive bool `default:"true" yaml:"tcp-keep-alive" json:"tcp-keep-alive,omitempty"` 44 KeepAlivePeriod string `default:"180s" yaml:"keep-alive-period" json:"keep-alive-period,omitempty"` 45 keepAlivePeriod time.Duration 46 TcpRBufSize int `default:"262144" yaml:"tcp-r-buf-size" json:"tcp-r-buf-size,omitempty"` 47 TcpWBufSize int `default:"65536" yaml:"tcp-w-buf-size" json:"tcp-w-buf-size,omitempty"` 48 TcpReadTimeout string `default:"1s" yaml:"tcp-read-timeout" json:"tcp-read-timeout,omitempty"` 49 tcpReadTimeout time.Duration 50 TcpWriteTimeout string `default:"5s" yaml:"tcp-write-timeout" json:"tcp-write-timeout,omitempty"` 51 tcpWriteTimeout time.Duration 52 WaitTimeout string `default:"7s" yaml:"wait-timeout" json:"wait-timeout,omitempty"` 53 waitTimeout time.Duration 54 MaxMsgLen int `default:"1024" yaml:"max-msg-len" json:"max-msg-len,omitempty"` 55 SessionName string `default:"rpc" yaml:"session-name" json:"session-name,omitempty"` 56 } 57 58 // ServerConfig holds supported types by the multiconfig package 59 ServerConfig struct { 60 SSLEnabled bool 61 TLSBuilder getty.TlsConfigBuilder 62 63 // heartbeat 64 HeartbeatPeriod string `default:"60s" yaml:"heartbeat-period" json:"heartbeat-period,omitempty"` 65 heartbeatPeriod time.Duration 66 67 // heartbeat timeout 68 HeartbeatTimeout string `default:"5s" yaml:"heartbeat-timeout" json:"heartbeat-timeout,omitempty"` 69 heartbeatTimeout time.Duration 70 71 // session 72 SessionTimeout string `default:"60s" yaml:"session-timeout" json:"session-timeout,omitempty"` 73 sessionTimeout time.Duration 74 SessionNumber int `default:"1000" yaml:"session-number" json:"session-number,omitempty"` 75 76 // gr pool 77 GrPoolSize int `default:"0" yaml:"gr-pool-size" json:"gr-pool-size,omitempty"` 78 QueueLen int `default:"0" yaml:"queue-len" json:"queue-len,omitempty"` 79 QueueNumber int `default:"0" yaml:"queue-number" json:"queue-number,omitempty"` 80 81 // session tcp parameters 82 GettySessionParam GettySessionParam `required:"true" yaml:"getty-session-param" json:"getty-session-param,omitempty"` 83 } 84 85 // ClientConfig holds supported types by the multi config package 86 ClientConfig struct { 87 SSLEnabled bool 88 TLSBuilder getty.TlsConfigBuilder 89 90 ReconnectInterval int `default:"0" yaml:"reconnect-interval" json:"reconnect-interval,omitempty"` 91 92 // session pool 93 ConnectionNum int `default:"16" yaml:"connection-number" json:"connection-number,omitempty"` 94 95 // heartbeat 96 HeartbeatPeriod string `default:"60s" yaml:"heartbeat-period" json:"heartbeat-period,omitempty"` 97 heartbeatPeriod time.Duration 98 99 // heartbeat timeout 100 HeartbeatTimeout string `default:"5s" yaml:"heartbeat-timeout" json:"heartbeat-timeout,omitempty"` 101 heartbeatTimeout time.Duration 102 103 // session 104 SessionTimeout string `default:"60s" yaml:"session-timeout" json:"session-timeout,omitempty"` 105 sessionTimeout time.Duration 106 107 // gr pool 108 GrPoolSize int `default:"0" yaml:"gr-pool-size" json:"gr-pool-size,omitempty"` 109 QueueLen int `default:"0" yaml:"queue-len" json:"queue-len,omitempty"` 110 QueueNumber int `default:"0" yaml:"queue-number" json:"queue-number,omitempty"` 111 112 // session tcp parameters 113 GettySessionParam GettySessionParam `required:"true" yaml:"getty-session-param" json:"getty-session-param,omitempty"` 114 } 115 ) 116 117 // GetDefaultClientConfig gets client default configuration 118 func GetDefaultClientConfig() *ClientConfig { 119 defaultClientConfig := &ClientConfig{ 120 ReconnectInterval: 0, 121 ConnectionNum: 16, 122 HeartbeatPeriod: "30s", 123 SessionTimeout: "180s", 124 GrPoolSize: 200, 125 QueueLen: 64, 126 QueueNumber: 10, 127 GettySessionParam: GettySessionParam{ 128 CompressEncoding: false, 129 TcpNoDelay: true, 130 TcpKeepAlive: true, 131 KeepAlivePeriod: "180s", 132 TcpRBufSize: 262144, 133 TcpWBufSize: 65536, 134 TcpReadTimeout: "1s", 135 TcpWriteTimeout: "5s", 136 WaitTimeout: "1s", 137 MaxMsgLen: 102400, 138 SessionName: "client", 139 }, 140 } 141 _ = defaultClientConfig.CheckValidity() 142 return defaultClientConfig 143 } 144 145 // GetDefaultServerConfig gets server default configuration 146 func GetDefaultServerConfig() *ServerConfig { 147 defaultServerConfig := &ServerConfig{ 148 SessionTimeout: "180s", 149 SessionNumber: 700, 150 GrPoolSize: 120, 151 QueueNumber: 6, 152 QueueLen: 64, 153 GettySessionParam: GettySessionParam{ 154 CompressEncoding: false, 155 TcpNoDelay: true, 156 TcpKeepAlive: true, 157 KeepAlivePeriod: "180s", 158 TcpRBufSize: 262144, 159 TcpWBufSize: 65536, 160 TcpReadTimeout: "1s", 161 TcpWriteTimeout: "5s", 162 WaitTimeout: "1s", 163 MaxMsgLen: 102400, 164 SessionName: "server", 165 }, 166 } 167 _ = defaultServerConfig.CheckValidity() 168 return defaultServerConfig 169 } 170 171 // CheckValidity confirm getty session params 172 func (c *GettySessionParam) CheckValidity() error { 173 var err error 174 175 if c.keepAlivePeriod, err = time.ParseDuration(c.KeepAlivePeriod); err != nil { 176 return perrors.WithMessagef(err, "time.ParseDuration(KeepAlivePeriod{%#v})", c.KeepAlivePeriod) 177 } 178 179 if c.tcpReadTimeout, err = parseTcpTimeoutDuration(c.TcpReadTimeout); err != nil { 180 return perrors.WithMessagef(err, "time.ParseDuration(TcpReadTimeout{%#v})", c.TcpReadTimeout) 181 } 182 183 if c.tcpWriteTimeout, err = parseTcpTimeoutDuration(c.TcpWriteTimeout); err != nil { 184 return perrors.WithMessagef(err, "time.ParseDuration(TcpWriteTimeout{%#v})", c.TcpWriteTimeout) 185 } 186 187 if c.waitTimeout, err = time.ParseDuration(c.WaitTimeout); err != nil { 188 return perrors.WithMessagef(err, "time.ParseDuration(WaitTimeout{%#v})", c.WaitTimeout) 189 } 190 191 return nil 192 } 193 194 func parseTcpTimeoutDuration(timeStr string) (time.Duration, error) { 195 result, err := time.ParseDuration(timeStr) 196 if err != nil { 197 return 0, err 198 } 199 if result < TCPReadWriteTimeoutMinValue { 200 return TCPReadWriteTimeoutMinValue, nil 201 } 202 return result, nil 203 } 204 205 // CheckValidity confirm client params. 206 func (c *ClientConfig) CheckValidity() error { 207 var err error 208 209 c.ReconnectInterval = c.ReconnectInterval * 1e6 210 211 if c.heartbeatPeriod, err = time.ParseDuration(c.HeartbeatPeriod); err != nil { 212 return perrors.WithMessagef(err, "time.ParseDuration(HeartbeatPeroid{%#v})", c.HeartbeatPeriod) 213 } 214 215 if c.heartbeatPeriod >= time.Duration(config.MaxWheelTimeSpan) { 216 return perrors.WithMessagef(err, "heartbeat-period %s should be less than %s", 217 c.HeartbeatPeriod, time.Duration(config.MaxWheelTimeSpan)) 218 } 219 220 if len(c.HeartbeatTimeout) == 0 { 221 c.heartbeatTimeout = 60 * time.Second 222 } else if c.heartbeatTimeout, err = time.ParseDuration(c.HeartbeatTimeout); err != nil { 223 return perrors.WithMessagef(err, "time.ParseDuration(HeartbeatTimeout{%#v})", c.HeartbeatTimeout) 224 } 225 226 if c.sessionTimeout, err = time.ParseDuration(c.SessionTimeout); err != nil { 227 return perrors.WithMessagef(err, "time.ParseDuration(SessionTimeout{%#v})", c.SessionTimeout) 228 } 229 230 return perrors.WithStack(c.GettySessionParam.CheckValidity()) 231 } 232 233 // CheckValidity confirm server params 234 func (c *ServerConfig) CheckValidity() error { 235 var err error 236 237 if len(c.HeartbeatPeriod) == 0 { 238 c.heartbeatPeriod = 60 * time.Second 239 } else if c.heartbeatPeriod, err = time.ParseDuration(c.HeartbeatPeriod); err != nil { 240 return perrors.WithMessagef(err, "time.ParseDuration(HeartbeatPeroid{%#v})", c.HeartbeatPeriod) 241 } 242 243 if c.heartbeatPeriod >= time.Duration(config.MaxWheelTimeSpan) { 244 return perrors.WithMessagef(err, "heartbeat-period %s should be less than %s", 245 c.HeartbeatPeriod, time.Duration(config.MaxWheelTimeSpan)) 246 } 247 248 if len(c.HeartbeatTimeout) == 0 { 249 c.heartbeatTimeout = 60 * time.Second 250 } else if c.heartbeatTimeout, err = time.ParseDuration(c.HeartbeatTimeout); err != nil { 251 return perrors.WithMessagef(err, "time.ParseDuration(HeartbeatTimeout{%#v})", c.HeartbeatTimeout) 252 } 253 254 if c.sessionTimeout, err = time.ParseDuration(c.SessionTimeout); err != nil { 255 return perrors.WithMessagef(err, "time.ParseDuration(SessionTimeout{%#v})", c.SessionTimeout) 256 } 257 258 if c.sessionTimeout >= time.Duration(config.MaxWheelTimeSpan) { 259 return perrors.WithMessagef(err, "session-timeout %s should be less than %s", 260 c.SessionTimeout, time.Duration(config.MaxWheelTimeSpan)) 261 } 262 263 return perrors.WithStack(c.GettySessionParam.CheckValidity()) 264 }