2021-04-17 15:04:43 +08:00
|
|
|
// Package config ...
|
|
|
|
//
|
|
|
|
// Description : WS-Server 相关配置
|
|
|
|
//
|
2021-06-05 22:13:09 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-04-17 15:04:43 +08:00
|
|
|
//
|
|
|
|
// Date : 2021-04-17 2:32 下午
|
|
|
|
package config
|
2021-04-17 19:41:02 +08:00
|
|
|
|
|
|
|
import (
|
2021-04-22 22:03:17 +08:00
|
|
|
"time"
|
|
|
|
|
2021-04-17 21:41:40 +08:00
|
|
|
"github.com/go-developer/gopkg/logger"
|
2021-04-17 23:34:46 +08:00
|
|
|
"github.com/go-developer/websocket/storage"
|
2021-04-17 19:41:02 +08:00
|
|
|
"go.uber.org/zap/zapcore"
|
2021-04-22 22:03:17 +08:00
|
|
|
"gopkg.in/olahol/melody.v1"
|
2021-04-17 19:41:02 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// RunModeProduct 生产环境
|
|
|
|
RunModeProduct = "product"
|
|
|
|
// RunModeDebug debug环境
|
|
|
|
RunModeDebug = "debug"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// LogSplitIntervalHour 按小时切割日志
|
|
|
|
LogSplitIntervalHour = "hour"
|
|
|
|
// LogSplitIntervalDay 按天切割日志
|
|
|
|
LogSplitIntervalDay = "day"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 定义相关默认值
|
|
|
|
const (
|
|
|
|
// DefaultLogEnable 默认关闭日志
|
|
|
|
DefaultLogEnable = false
|
|
|
|
// DefaultLogConsole 默认开启控制台输出
|
2021-06-05 22:13:09 +08:00
|
|
|
DefaultLogConsole = false
|
2021-04-17 19:41:02 +08:00
|
|
|
// DefaultMode 默认为Debug模式
|
|
|
|
DefaultMode = RunModeDebug
|
|
|
|
// DefaultLogLevel 默认的日志级别
|
|
|
|
DefaultLogLevel = zapcore.DebugLevel
|
2021-04-17 21:41:40 +08:00
|
|
|
// DefaultLogSplitInterval 默认的日志切割时间
|
|
|
|
DefaultLogSplitInterval = logger.TimeIntervalTypeHour
|
2021-04-17 23:14:54 +08:00
|
|
|
// DefaultStoreConnection 默认存储连接
|
|
|
|
DefaultStoreConnection = true
|
2021-04-19 00:03:25 +08:00
|
|
|
// DefaultEnablePprof 默认关闭pprof
|
|
|
|
DefaultEnablePprof = false
|
2021-04-22 22:03:17 +08:00
|
|
|
// DefaultMaxMessageSize 默认的最大消息大小
|
|
|
|
DefaultMaxMessageSize = 4096
|
|
|
|
// DefaultMaxMessageBufferSize 默认的消息缓冲区大小
|
|
|
|
DefaultMaxMessageBufferSize = 8192
|
|
|
|
// DefaultWriteWait 默认的消息写入等待时间
|
|
|
|
DefaultWriteWait = 10
|
|
|
|
// DefaultPongWait 默认的等待响应时间
|
|
|
|
DefaultPongWait = 10
|
|
|
|
// DefaultPingPeriod 多久探活一次
|
|
|
|
DefaultPingPeriod = 10
|
2021-04-17 19:41:02 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// WSServerConfig WS-Server的配置
|
|
|
|
//
|
2021-06-05 22:13:09 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-04-17 19:41:02 +08:00
|
|
|
//
|
|
|
|
// Date : 7:02 下午 2021/4/17
|
|
|
|
type WSServerConfig struct {
|
2021-04-17 23:34:46 +08:00
|
|
|
Mode string // 运行模式
|
|
|
|
LogEnable bool // 开启日志
|
|
|
|
LogConsole bool // 开启控制台日志输出
|
|
|
|
LogPath string // 日志路径
|
|
|
|
LogFile string // 日志文件名
|
|
|
|
LogLevel zapcore.Level // 日志等级
|
|
|
|
LogSplitInterval logger.TimeIntervalType // 日至切割的时间间隔
|
|
|
|
StoreConnection bool // 存储连接
|
|
|
|
ConnectionManager storage.IConnection // 连接管理实例
|
2021-04-19 00:03:25 +08:00
|
|
|
EnablePprof bool // 开启pprof, 默认关闭
|
|
|
|
PprofPort int // pprof监听的端口
|
2021-04-22 22:03:17 +08:00
|
|
|
melody.Config // 长连接配置
|
2021-04-17 19:41:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetWSServerConfig 设置WS-Server的配置
|
|
|
|
//
|
2021-06-05 22:13:09 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-04-17 19:41:02 +08:00
|
|
|
//
|
|
|
|
// Date : 7:03 下午 2021/4/17
|
|
|
|
type SetWSServerConfig func(wsc *WSServerConfig)
|
|
|
|
|
|
|
|
// SetWSServerLogEnable 开启日志记录
|
|
|
|
//
|
2021-06-05 22:13:09 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-04-17 19:41:02 +08:00
|
|
|
//
|
|
|
|
// Date : 7:25 下午 2021/4/17
|
2021-04-17 21:41:40 +08:00
|
|
|
func SetWSServerLogEnable(logPath string, logFile string, logLevel zapcore.Level, splitInterval logger.TimeIntervalType) SetWSServerConfig {
|
2021-04-17 19:41:02 +08:00
|
|
|
return func(wsc *WSServerConfig) {
|
2021-04-17 21:41:40 +08:00
|
|
|
if splitInterval != logger.TimeIntervalTypeMinute &&
|
|
|
|
splitInterval != logger.TimeIntervalTypeHour &&
|
|
|
|
splitInterval != logger.TimeIntervalTypeDay &&
|
|
|
|
splitInterval != logger.TimeIntervalTypeMonth &&
|
|
|
|
splitInterval != logger.TimeIntervalTypeYear {
|
2021-04-17 19:41:02 +08:00
|
|
|
// 传入非法值,默认按小时切割日志
|
2021-04-17 21:41:40 +08:00
|
|
|
splitInterval = DefaultLogSplitInterval
|
2021-04-17 19:41:02 +08:00
|
|
|
}
|
2021-04-17 23:18:58 +08:00
|
|
|
if len(logPath) == 0 {
|
|
|
|
logPath = "./logs"
|
|
|
|
}
|
|
|
|
|
2021-04-17 19:41:02 +08:00
|
|
|
wsc.LogEnable = true
|
|
|
|
wsc.LogPath = logPath
|
2021-04-17 21:41:40 +08:00
|
|
|
wsc.LogFile = logFile
|
2021-04-17 19:41:02 +08:00
|
|
|
wsc.LogLevel = logLevel
|
|
|
|
wsc.LogSplitInterval = splitInterval
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-05 22:13:09 +08:00
|
|
|
// EnableConsoleLog 开启控制台日志输出
|
|
|
|
//
|
|
|
|
// Author : zhangdeman001@ke.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 11:25 下午 2021/4/22
|
|
|
|
func EnableConsoleLog() SetWSServerConfig {
|
|
|
|
return func(wsc *WSServerConfig) {
|
|
|
|
wsc.LogConsole = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 00:03:25 +08:00
|
|
|
// EnablePprof 开启PProf,由于多模块共享一个进程,任意一个模块开启,就认为是开启
|
|
|
|
//
|
2021-06-05 22:13:09 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-04-19 00:03:25 +08:00
|
|
|
//
|
|
|
|
// Date : 11:24 下午 2021/4/18
|
|
|
|
func EnablePprof(pprofPort int) SetWSServerConfig {
|
|
|
|
return func(wsc *WSServerConfig) {
|
|
|
|
wsc.EnablePprof = true
|
|
|
|
wsc.PprofPort = pprofPort
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-17 23:14:54 +08:00
|
|
|
// DisableStoreConnection 禁用连接存储
|
|
|
|
//
|
2021-06-05 22:13:09 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-04-17 23:14:54 +08:00
|
|
|
//
|
|
|
|
// Date : 11:10 下午 2021/4/17
|
|
|
|
func DisableStoreConnection() SetWSServerConfig {
|
|
|
|
return func(wsc *WSServerConfig) {
|
|
|
|
wsc.StoreConnection = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 22:49:38 +08:00
|
|
|
// SetConnectionManager 连接管理实例
|
|
|
|
//
|
2021-06-05 22:13:09 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-04-22 22:49:38 +08:00
|
|
|
//
|
|
|
|
// Date : 10:48 下午 2021/4/22
|
|
|
|
func SetConnectionManager(manager storage.IConnection) SetWSServerConfig {
|
|
|
|
return func(wsc *WSServerConfig) {
|
|
|
|
if nil == manager {
|
|
|
|
manager = storage.NewDefaultConnectionManager()
|
|
|
|
}
|
|
|
|
wsc.ConnectionManager = manager
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 22:03:17 +08:00
|
|
|
// SetMaxMessageSize 限制消息大小
|
|
|
|
//
|
2021-06-05 22:13:09 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-04-22 22:03:17 +08:00
|
|
|
//
|
|
|
|
// Date : 9:29 下午 2021/4/22
|
|
|
|
func SetMaxMessageSize(maxMessageSize int64) SetWSServerConfig {
|
|
|
|
return func(wsc *WSServerConfig) {
|
|
|
|
if maxMessageSize <= 0 {
|
|
|
|
maxMessageSize = DefaultMaxMessageSize
|
|
|
|
}
|
|
|
|
wsc.MaxMessageSize = maxMessageSize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMaxMessageBufferSize 设置消息缓冲区
|
|
|
|
//
|
2021-06-05 22:13:09 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-04-22 22:03:17 +08:00
|
|
|
//
|
|
|
|
// Date : 9:39 下午 2021/4/22
|
|
|
|
func SetMaxMessageBufferSize(maxMessageBufferSize int) SetWSServerConfig {
|
|
|
|
return func(wsc *WSServerConfig) {
|
|
|
|
if maxMessageBufferSize <= 0 {
|
|
|
|
maxMessageBufferSize = DefaultMaxMessageSize
|
|
|
|
}
|
|
|
|
wsc.MessageBufferSize = maxMessageBufferSize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetWriteWait 设置消息写入的等待时间, 单位 : 秒
|
|
|
|
//
|
2021-06-05 22:13:09 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-04-22 22:03:17 +08:00
|
|
|
//
|
|
|
|
// Date : 9:44 下午 2021/4/22
|
|
|
|
func SetWriteWait(writeWaitTime int) SetWSServerConfig {
|
|
|
|
return func(wsc *WSServerConfig) {
|
|
|
|
if writeWaitTime <= 0 {
|
|
|
|
writeWaitTime = DefaultWriteWait
|
|
|
|
}
|
|
|
|
wsc.WriteWait = time.Duration(writeWaitTime) * time.Second
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPongWait 设置探活时,等待响应时间,单位 : 秒
|
|
|
|
//
|
2021-06-05 22:13:09 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-04-22 22:03:17 +08:00
|
|
|
//
|
|
|
|
// Date : 9:55 下午 2021/4/22
|
|
|
|
func SetPongWait(pongWait int) SetWSServerConfig {
|
|
|
|
return func(wsc *WSServerConfig) {
|
|
|
|
if pongWait <= 0 {
|
|
|
|
pongWait = DefaultPongWait
|
|
|
|
}
|
|
|
|
wsc.PongWait = time.Duration(pongWait) * time.Second
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPingPeriod 设置探活时间间隔
|
|
|
|
//
|
2021-06-05 22:13:09 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-04-22 22:03:17 +08:00
|
|
|
//
|
|
|
|
// Date : 10:00 下午 2021/4/22
|
|
|
|
func SetPingPeriod(pingPeriod int) SetWSServerConfig {
|
|
|
|
return func(wsc *WSServerConfig) {
|
|
|
|
if pingPeriod <= 0 {
|
|
|
|
pingPeriod = DefaultPingPeriod
|
|
|
|
}
|
|
|
|
wsc.PingPeriod = time.Duration(pingPeriod) * time.Second
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-17 19:41:02 +08:00
|
|
|
// NewWSServerConfig 生成新的WS-Server配置
|
|
|
|
//
|
2021-06-05 22:13:09 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-04-17 19:41:02 +08:00
|
|
|
//
|
|
|
|
// Date : 7:21 下午 2021/4/17
|
|
|
|
func NewWSServerConfig(optionList ...SetWSServerConfig) *WSServerConfig {
|
|
|
|
c := &WSServerConfig{
|
2021-04-17 23:34:46 +08:00
|
|
|
Mode: DefaultMode,
|
|
|
|
LogEnable: DefaultLogEnable,
|
|
|
|
LogConsole: DefaultLogConsole,
|
|
|
|
LogPath: "",
|
|
|
|
LogLevel: DefaultLogLevel,
|
|
|
|
LogSplitInterval: DefaultLogSplitInterval,
|
|
|
|
StoreConnection: DefaultStoreConnection,
|
|
|
|
ConnectionManager: storage.NewDefaultConnectionManager(),
|
2021-04-22 22:03:17 +08:00
|
|
|
EnablePprof: DefaultEnablePprof,
|
2021-04-17 19:41:02 +08:00
|
|
|
}
|
2021-04-22 22:03:17 +08:00
|
|
|
c.MaxMessageSize = DefaultMaxMessageSize
|
|
|
|
c.MessageBufferSize = DefaultMaxMessageBufferSize
|
2021-04-22 22:04:56 +08:00
|
|
|
c.PingPeriod = time.Duration(DefaultPingPeriod) * time.Second
|
|
|
|
c.WriteWait = time.Duration(DefaultWriteWait) * time.Second
|
|
|
|
c.PongWait = time.Duration(DefaultPongWait) * time.Second
|
2021-04-17 19:41:02 +08:00
|
|
|
for _, o := range optionList {
|
|
|
|
o(c)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|