You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
5.1 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package config
import (
"time"
"github.com/noahlann/nnet/pkg/errors"
)
// Config 服务器配置
type Config struct {
// 服务器地址tcp://:6995, udp://:6995, unix:///tmp/nnet.sock, serial:///dev/ttyUSB0
Addr string
// 传输层协议tcp, udp, websocket, unix, serial等
// 如果指定,将优先使用指定的传输层协议
// 如果为空将根据Addr前缀自动识别tcp://, udp://, ws://, wss://, unix://, serial://
// 如果无法识别默认使用TCP
TransportProtocol string
// 应用层协议名称nnet、自定义协议等
// 注意TCP、UDP、WebSocket是传输层协议不是应用层协议
// 应用层协议在传输层协议之上运行,用于数据编码/解码
ApplicationProtocol string
// 网络类型tcp, tcp4, tcp6, udp, udp4, udp6, unix
Network string
// 多路复用Linux系统支持
Multicore bool
// 读取缓冲区大小
ReadBufferSize int
// 写入缓冲区大小
WriteBufferSize int
// 最大连接数
MaxConnections int
// 连接管理器分片数用于减少锁竞争0表示使用默认值16
// 分片数越大,锁竞争越小,但内存开销稍大
// 建议值16-64默认16
ConnectionManagerShards int
// 连接超时时间
ConnectionTimeout time.Duration
// 读取超时时间
ReadTimeout time.Duration
// 写入超时时间
WriteTimeout time.Duration
// 优雅关闭超时时间默认30秒
// 在关闭服务器时,等待现有连接完成处理的最大时间
// 如果超时,将强制关闭所有连接
ShutdownTimeout time.Duration
// 心跳间隔
HeartbeatInterval time.Duration
// 是否启用TLS
TLSEnabled bool
// TLS配置
TLS *TLSConfig
// 日志配置
Logger *LoggerConfig
// Metrics配置
Metrics *MetricsConfig
// Session配置
Session *SessionConfig
// 编解码器配置
Codec *CodecConfig
// Handler执行配置
// Handler在事件循环中的最大执行时间如果为0表示不限制。
HandlerTimeout time.Duration
// 串口配置仅当TransportProtocol为serial时使用
Serial *SerialConfig
}
// SerialConfig 串口配置
type SerialConfig struct {
// 波特率9600, 115200等
BaudRate int
// 数据位5, 6, 7, 8
DataBits int
// 停止位1, 2
StopBits int
// 校验位None, Odd, Even, Mark, Space
Parity string
// 读超时(毫秒)
ReadTimeout int
// 写超时(毫秒)
WriteTimeout int
}
// TLSConfig TLS配置
type TLSConfig struct {
CertFile string
KeyFile string
CAFile string
}
// LoggerConfig 日志配置
type LoggerConfig struct {
Level string // debug, info, warn, error
Format string // json, text
Output string // stdout, stderr, file path
}
// MetricsConfig Metrics配置
type MetricsConfig struct {
Enabled bool
Path string // metrics路径默认 /metrics
Port int // metrics端口0表示不单独监听
}
// SessionConfig Session配置
type SessionConfig struct {
Storage string // memory, file, redis
Expiration time.Duration // Session过期时间
Path string // 文件存储路径
}
// CodecConfig 编解码器配置
type CodecConfig struct {
// 默认编解码器名称json, binary, protobuf, msgpack等
DefaultCodec string
// 是否启用协议层编码如果启用会先使用协议Encode再使用编解码器Encode
EnableProtocolEncode bool
// CloneHeader 是否拷贝header默认false共享引用以提升性能
// 如果设置为truerequest和response的header会进行深拷贝避免相互影响
CloneHeader bool
}
// DefaultConfig 返回默认配置
func DefaultConfig() *Config {
return &Config{
Addr: "tcp://:6995",
TransportProtocol: "", // 默认根据地址前缀自动识别
ApplicationProtocol: "nnet", // 默认使用nnet应用层协议
Network: "tcp",
Multicore: true,
ReadBufferSize: 4096,
WriteBufferSize: 4096,
MaxConnections: 10000,
ConnectionTimeout: 30 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
ShutdownTimeout: 30 * time.Second,
HeartbeatInterval: 30 * time.Second,
TLSEnabled: false,
Logger: &LoggerConfig{
Level: "info",
Format: "text",
Output: "stdout",
},
Metrics: &MetricsConfig{
Enabled: false,
Path: "/metrics",
Port: 0,
},
Session: &SessionConfig{
Storage: "memory",
Expiration: 24 * time.Hour,
Path: "./sessions",
},
Codec: &CodecConfig{
DefaultCodec: "binary",
EnableProtocolEncode: false,
},
HandlerTimeout: 30 * time.Second,
}
}
// Validate 验证配置
func (c *Config) Validate() error {
if c.Addr == "" {
return errors.New("addr is required")
}
// ApplicationProtocol 可以为空(表示不使用应用层协议)
// 如果指定了应用层协议,会在协议管理器中查找
if c.Network == "" {
c.Network = "tcp"
}
if c.ReadBufferSize <= 0 {
c.ReadBufferSize = 4096
}
if c.WriteBufferSize <= 0 {
c.WriteBufferSize = 4096
}
if c.MaxConnections <= 0 {
c.MaxConnections = 10000
}
return nil
}