github.com/matrixorigin/matrixone@v0.7.0/pkg/txn/rpc/cfg.go (about)

     1  // Copyright 2021 - 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package rpc
    16  
    17  import (
    18  	"time"
    19  
    20  	"github.com/fagongzi/goetty/v2"
    21  	"github.com/matrixorigin/matrixone/pkg/common/morpc"
    22  	"github.com/matrixorigin/matrixone/pkg/util/toml"
    23  	"go.uber.org/zap"
    24  )
    25  
    26  var (
    27  	defaultMaxConnections  = 400
    28  	defaultMaxIdleDuration = time.Minute
    29  	defaultSendQueueSize   = 10240
    30  	defaultBufferSize      = 1024
    31  )
    32  
    33  // Config txn sender config
    34  type Config struct {
    35  	// MaxConnections maximum number of connections to communicate with each DNStore.
    36  	// Default is 400.
    37  	MaxConnections int `toml:"max-connections"`
    38  	// MaxIdleDuration maximum connection idle time, connection will be closed automatically
    39  	// if this value is exceeded. Default is 1 min.
    40  	MaxIdleDuration toml.Duration `toml:"max-idle-duration"`
    41  	// SendQueueSize maximum capacity of the send request queue per connection, when the
    42  	// queue is full, the send request will be blocked. Default is 10240.
    43  	SendQueueSize int `toml:"send-queue-size"`
    44  	// BusyQueueSize when the length of the send queue reaches the currently set value, the
    45  	// current connection is busy with high load. When any connection with Busy status exists,
    46  	// a new connection will be created until the value set by MaxConnections is reached.
    47  	// Default is 3/4 of SendQueueSize.
    48  	BusyQueueSize int `toml:"busy-queue-size"`
    49  	// WriteBufferSize buffer size for write messages per connection. Default is 1kb
    50  	WriteBufferSize toml.ByteSize `toml:"write-buffer-size"`
    51  	// ReadBufferSize buffer size for read messages per connection. Default is 1kb
    52  	ReadBufferSize toml.ByteSize `toml:"read-buffer-size"`
    53  	// MaxMessageSize max size for read messages from dn. Default is 10M
    54  	MaxMessageSize toml.ByteSize `toml:"max-message-size"`
    55  	// EnableCompress enable compress message
    56  	EnableCompress bool `toml:"enable-compress"`
    57  }
    58  
    59  func (c *Config) adjust() {
    60  	if c.MaxConnections == 0 {
    61  		c.MaxConnections = defaultMaxConnections
    62  	}
    63  	if c.SendQueueSize == 0 {
    64  		c.SendQueueSize = defaultSendQueueSize
    65  	}
    66  	if c.BusyQueueSize == 0 {
    67  		c.BusyQueueSize = c.SendQueueSize * 3 / 4
    68  	}
    69  	if c.WriteBufferSize == 0 {
    70  		c.WriteBufferSize = toml.ByteSize(defaultBufferSize)
    71  	}
    72  	if c.ReadBufferSize == 0 {
    73  		c.ReadBufferSize = toml.ByteSize(defaultBufferSize)
    74  	}
    75  	if c.MaxIdleDuration.Duration == 0 {
    76  		c.MaxIdleDuration.Duration = defaultMaxIdleDuration
    77  	}
    78  }
    79  
    80  func (c Config) getBackendOptions(logger *zap.Logger) []morpc.BackendOption {
    81  	return []morpc.BackendOption{
    82  		morpc.WithBackendLogger(logger),
    83  		morpc.WithBackendBusyBufferSize(c.BusyQueueSize),
    84  		morpc.WithBackendBufferSize(c.SendQueueSize),
    85  		morpc.WithBackendGoettyOptions(goetty.WithSessionRWBUfferSize(int(c.ReadBufferSize),
    86  			int(c.WriteBufferSize))),
    87  	}
    88  }
    89  
    90  func (c Config) getClientOptions(logger *zap.Logger) []morpc.ClientOption {
    91  	return []morpc.ClientOption{
    92  		morpc.WithClientLogger(logger),
    93  		morpc.WithClientMaxBackendPerHost(c.MaxConnections),
    94  		morpc.WithClientMaxBackendMaxIdleDuration(c.MaxIdleDuration.Duration),
    95  		morpc.WithClientTag("txn-rpc-sender"),
    96  	}
    97  }