~~~
package transport
import (
"time"
)
type Conf struct {
QueuePerInvoker int
TCPReadBuffer int
TCPWriteBuffer int
TCPNoDelay bool
AcceptTimeout time.Duration
Proto string
Address string
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
HandleTimeout time.Duration
SendChanSize int64
InvokerPerSession int
}
func NewServerConf(proto string, address string, opts ...ServerConfOption) *Conf {
conf := defaultServerConf
conf.Proto = proto
conf.Address = address
for _, o := range opts {
o(&conf)
}
return &conf
}
type ServerConfOption func(*Conf)
func WithSlavePerSession(r int) ServerConfOption {
return func(o *Conf) {
o.InvokerPerSession = r
}
}
func WithIdleTimeout(t time.Duration) ServerConfOption {
return func(o *Conf) {
o.IdleTimeout = t
}
}
func WithSendChanSize(n int64) ServerConfOption {
return func(o *Conf) {
o.SendChanSize = n
}
}
func WithTCPNoDelay(b bool) ServerConfOption {
return func(o *Conf) {
o.TCPNoDelay = b
}
}
const (
AcceptTimeout time.Duration = 500 * time.Millisecond
//ReadTimeout zero for not set read deadline for Conn (better performance)
ReadTimeout time.Duration = 0 * time.Millisecond
//WriteTimeout zero for not set write deadline for Conn (better performance)
WriteTimeout time.Duration = 0 * time.Millisecond
//HandleTimeout zero for not set deadline for invoke user interface (better performance)
HandleTimeout time.Duration = 0 * time.Millisecond
//IdleTimeout idle timeout
IdleTimeout time.Duration = 600000 * time.Millisecond
//QueuePerInvoker queue gap
QueueCap int = 10000000
// TCP
//TCPReadBuffer tcp read buffer length
TCPReadBuffer = 128 * 1024 * 1024
//TCPWriteBuffer tcp write buffer length
TCPWriteBuffer = 128 * 1024 * 1024
//TCPNoDelay set tcp no delay
TCPNoDelay = false
MaxInvokePerSession = -1
)
var defaultServerConf = Conf{
Proto: "tcp",
InvokerPerSession: MaxInvokePerSession,
QueuePerInvoker: QueueCap,
TCPReadBuffer: TCPReadBuffer,
TCPWriteBuffer: TCPWriteBuffer,
TCPNoDelay: TCPNoDelay,
AcceptTimeout: AcceptTimeout,
ReadTimeout: ReadTimeout,
WriteTimeout: WriteTimeout,
IdleTimeout: IdleTimeout,
SendChanSize: 1024,
HandleTimeout: HandleTimeout,
}
~~~