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,共享引用以提升性能) // 如果设置为true,request和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 }