增加对长连接消息体以及探活配置

This commit is contained in:
白茶清欢 2021-04-22 22:03:17 +08:00
parent 7c7d47a217
commit 8a19bed17b
2 changed files with 101 additions and 0 deletions

View File

@ -8,9 +8,12 @@
package config
import (
"time"
"github.com/go-developer/gopkg/logger"
"github.com/go-developer/websocket/storage"
"go.uber.org/zap/zapcore"
"gopkg.in/olahol/melody.v1"
)
const (
@ -43,6 +46,16 @@ const (
DefaultStoreConnection = true
// DefaultEnablePprof 默认关闭pprof
DefaultEnablePprof = false
// DefaultMaxMessageSize 默认的最大消息大小
DefaultMaxMessageSize = 4096
// DefaultMaxMessageBufferSize 默认的消息缓冲区大小
DefaultMaxMessageBufferSize = 8192
// DefaultWriteWait 默认的消息写入等待时间
DefaultWriteWait = 10
// DefaultPongWait 默认的等待响应时间
DefaultPongWait = 10
// DefaultPingPeriod 多久探活一次
DefaultPingPeriod = 10
)
// WSServerConfig WS-Server的配置
@ -62,6 +75,7 @@ type WSServerConfig struct {
ConnectionManager storage.IConnection // 连接管理实例
EnablePprof bool // 开启pprof, 默认关闭
PprofPort int // pprof监听的端口
melody.Config // 长连接配置
}
// SetWSServerConfig 设置WS-Server的配置
@ -121,6 +135,76 @@ func DisableStoreConnection() SetWSServerConfig {
}
}
// SetMaxMessageSize 限制消息大小
//
// Author : go_developer@163.com<张德满>
//
// Date : 9:29 下午 2021/4/22
func SetMaxMessageSize(maxMessageSize int64) SetWSServerConfig {
return func(wsc *WSServerConfig) {
if maxMessageSize <= 0 {
maxMessageSize = DefaultMaxMessageSize
}
wsc.MaxMessageSize = maxMessageSize
}
}
// SetMaxMessageBufferSize 设置消息缓冲区
//
// Author : go_developer@163.com<张德满>
//
// Date : 9:39 下午 2021/4/22
func SetMaxMessageBufferSize(maxMessageBufferSize int) SetWSServerConfig {
return func(wsc *WSServerConfig) {
if maxMessageBufferSize <= 0 {
maxMessageBufferSize = DefaultMaxMessageSize
}
wsc.MessageBufferSize = maxMessageBufferSize
}
}
// SetWriteWait 设置消息写入的等待时间, 单位 : 秒
//
// Author : go_developer@163.com<张德满>
//
// 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 设置探活时,等待响应时间,单位 : 秒
//
// Author : go_developer@163.com<张德满>
//
// 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 设置探活时间间隔
//
// Author : go_developer@163.com<张德满>
//
// 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
}
}
// NewWSServerConfig 生成新的WS-Server配置
//
// Author : go_developer@163.com<张德满>
@ -136,7 +220,10 @@ func NewWSServerConfig(optionList ...SetWSServerConfig) *WSServerConfig {
LogSplitInterval: DefaultLogSplitInterval,
StoreConnection: DefaultStoreConnection,
ConnectionManager: storage.NewDefaultConnectionManager(),
EnablePprof: DefaultEnablePprof,
}
c.MaxMessageSize = DefaultMaxMessageSize
c.MessageBufferSize = DefaultMaxMessageBufferSize
for _, o := range optionList {
o(c)
}

View File

@ -134,7 +134,21 @@ func initServer(wsInstance abstract.IWebsocket) {
loggerInstanceTable[wsInstance.GetModuleFlag()] = loggerInstance
}
}
// 对长连接进行配置
s.wsServer.Config.MaxMessageSize = s.conf.MaxMessageSize
s.wsServer.Config.MessageBufferSize = s.conf.MessageBufferSize
s.wsServer.Config.WriteWait = s.conf.WriteWait
s.wsServer.Config.PongWait = s.conf.PongWait
s.wsServer.Config.PingPeriod = s.conf.PingPeriod
wsServerTable[wsInstance.GetServerPort()][wsInstance.GetModuleFlag()] = s
/*melody.Config{
WriteWait: 0,
PongWait: 0,
PingPeriod: 0,
MaxMessageSize: 0,
MessageBufferSize: 0,
}*/
}
// 初始化指令存储表