Skip to content

Commit

Permalink
Merge pull request #43 from symsimmy/ban/ws
Browse files Browse the repository at this point in the history
ban ws server
  • Loading branch information
symsimmy authored Oct 25, 2024
2 parents 923b4f6 + f54d425 commit 38b4e49
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 40 deletions.
8 changes: 6 additions & 2 deletions cluster/gate/gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ func (g *Gate) Init() {
func (g *Gate) Start() {
g.startNetworkServer()

g.startWsNetworkServer()
if g.opts.wsServerEnable {
g.startWsNetworkServer()
}

g.startRPCServer()

Expand All @@ -107,7 +109,9 @@ func (g *Gate) Destroy() {

g.stopNetworkServer()

g.stopWsNetworkServer()
if g.opts.wsServerEnable {
g.stopWsNetworkServer()
}

g.stopRPCServer()

Expand Down
84 changes: 46 additions & 38 deletions cluster/gate/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,54 +25,58 @@ import (
)

const (
defaultName = "gate" // 默认名称
defaultTimeout = 30 * time.Second // 默认超时时间
defaultCodec = "proto" // 默认编解码器名称
defaultWorkloadStat = 500
defaultNamespace = ""
defaultName = "gate" // 默认名称
defaultTimeout = 30 * time.Second // 默认超时时间
defaultCodec = "proto" // 默认编解码器名称
defaultWorkloadStat = 500
defaultNamespace = ""
defaultWsServerEnable = false
)

const (
defaultIDKey = "config.cluster.gate.id"
defaultNameKey = "config.cluster.gate.name"
defaultTimeoutKey = "config.cluster.gate.timeout"
defaultCodecKey = "config.cluster.gate.codec"
defaultDecryptorKey = "config.cluster.gate.decryptor"
defaultEncryptorKey = "config.cluster.node.encryptor"
defaultWorkloadStatKey = "config.workload.stat"
defaultNamespaceKey = "config.cluster.gate.namespace"
defaultIDKey = "config.cluster.gate.id"
defaultNameKey = "config.cluster.gate.name"
defaultTimeoutKey = "config.cluster.gate.timeout"
defaultCodecKey = "config.cluster.gate.codec"
defaultDecryptorKey = "config.cluster.gate.decryptor"
defaultEncryptorKey = "config.cluster.node.encryptor"
defaultWorkloadStatKey = "config.workload.stat"
defaultNamespaceKey = "config.cluster.gate.namespace"
defaultWsServerEnableKey = "config.network.ws.enable"
)

type Option func(o *options)

type options struct {
id string // 实例ID
name string // 实例名称
ctx context.Context // 上下文
timeout time.Duration // RPC调用超时时间
server network.Server // 网关服务器
wsServer network.Server // websocket 网关服务器
locator locate.Locator // 用户定位器
registry registry.Registry // 服务注册器
codec encoding.Codec // 编解码器
transporter transport.Transporter // 消息传输器
promServer prometheus.PromServer // 埋点采集服务器
catServer *cat.Server
receiveHook []hook.ReceiveHook // 接受消息hook
encryptor crypto.Encryptor // 消息加密器
decryptor crypto.Decryptor
workloadStat int32
namespace string
id string // 实例ID
name string // 实例名称
ctx context.Context // 上下文
timeout time.Duration // RPC调用超时时间
server network.Server // 网关服务器
wsServer network.Server // websocket 网关服务器
locator locate.Locator // 用户定位器
registry registry.Registry // 服务注册器
codec encoding.Codec // 编解码器
transporter transport.Transporter // 消息传输器
promServer prometheus.PromServer // 埋点采集服务器
catServer *cat.Server
receiveHook []hook.ReceiveHook // 接受消息hook
encryptor crypto.Encryptor // 消息加密器
decryptor crypto.Decryptor
workloadStat int32
namespace string
wsServerEnable bool
}

func defaultOptions() *options {
opts := &options{
ctx: context.Background(),
name: defaultName,
timeout: defaultTimeout,
codec: encoding.Invoke(defaultCodec),
workloadStat: config.Get(defaultWorkloadStatKey, defaultWorkloadStat).Int32(),
namespace: config.Get(defaultNamespaceKey, defaultNamespace).String(),
ctx: context.Background(),
name: defaultName,
timeout: defaultTimeout,
codec: encoding.Invoke(defaultCodec),
workloadStat: config.Get(defaultWorkloadStatKey, defaultWorkloadStat).Int32(),
namespace: config.Get(defaultNamespaceKey, defaultNamespace).String(),
wsServerEnable: config.Get(defaultWsServerEnableKey, defaultWsServerEnable).Bool(),
}

if id := config.Get(defaultIDKey).String(); id != "" {
Expand Down Expand Up @@ -126,9 +130,13 @@ func WithServer(server network.Server) Option {

// WithWsServer 设置websocket 服务器
func WithWsServer(server network.Server) Option {
return func(o *options) {
o.wsServer = server
wsServerEnable := config.Get(defaultWsServerEnableKey, defaultWsServerEnable).Bool()
if wsServerEnable {
return func(o *options) {
o.wsServer = server
}
}
return func(o *options) {}
}

// WithTimeout 设置RPC调用超时时间
Expand Down

0 comments on commit 38b4e49

Please sign in to comment.