feat: tune websocket hub backpressure
This commit is contained in:
48
internal/transport/httpserver/hub_options.go
Normal file
48
internal/transport/httpserver/hub_options.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package httpserver
|
||||
|
||||
// HubOptions 是节点内 Worker/Event WebSocket Hub 的有界运行参数。
|
||||
// 这些参数只控制本节点内存和领取并发,不改变 MySQL 中的授权、租约或配额事实。
|
||||
type HubOptions struct {
|
||||
// ConnectionLimit 是该 Hub 允许的同时连接数;超过后返回 429。
|
||||
ConnectionLimit int
|
||||
// DispatchWorkers 是本 Hub 同时执行数据库领取调用的 Go 协程上限。
|
||||
DispatchWorkers int
|
||||
// DispatchCapacity 是领取调用的有界内存排队槽位;满额时连接等待下一轮扫描。
|
||||
DispatchCapacity int
|
||||
}
|
||||
|
||||
const (
|
||||
defaultHubConnectionLimit = 128
|
||||
defaultHubDispatchWorkers = 4
|
||||
defaultHubDispatchCapacity = 128
|
||||
maxHubConnectionLimit = 4096
|
||||
maxHubDispatchWorkers = 256
|
||||
maxHubDispatchCapacity = 65536
|
||||
)
|
||||
|
||||
func resolveHubOptions(options []HubOptions) HubOptions {
|
||||
out := HubOptions{
|
||||
ConnectionLimit: defaultHubConnectionLimit,
|
||||
DispatchWorkers: defaultHubDispatchWorkers,
|
||||
DispatchCapacity: defaultHubDispatchCapacity,
|
||||
}
|
||||
if len(options) > 1 {
|
||||
panic("at most one HubOptions value is allowed")
|
||||
}
|
||||
if len(options) == 1 {
|
||||
in := options[0]
|
||||
if in.ConnectionLimit != 0 {
|
||||
out.ConnectionLimit = in.ConnectionLimit
|
||||
}
|
||||
if in.DispatchWorkers != 0 {
|
||||
out.DispatchWorkers = in.DispatchWorkers
|
||||
}
|
||||
if in.DispatchCapacity != 0 {
|
||||
out.DispatchCapacity = in.DispatchCapacity
|
||||
}
|
||||
}
|
||||
if out.ConnectionLimit < 1 || out.ConnectionLimit > maxHubConnectionLimit || out.DispatchWorkers < 1 || out.DispatchWorkers > maxHubDispatchWorkers || out.DispatchCapacity < 1 || out.DispatchCapacity > maxHubDispatchCapacity {
|
||||
panic("HubOptions values exceed bounded range")
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user