feat: tune websocket hub backpressure
This commit is contained in:
@@ -10,31 +10,39 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
HTTPAddr string
|
||||
MySQLDSN string
|
||||
MaxOpen int
|
||||
MaxIdle int
|
||||
MetricsAddr string
|
||||
WorkflowTickInterval time.Duration // WorkflowTickInterval Weave 持久化推进扫描间隔,范围100ms–1m。
|
||||
ScheduleTickInterval time.Duration // ScheduleTickInterval 计划扫描间隔,范围100ms–1m。
|
||||
RecoveryTickInterval time.Duration // RecoveryTickInterval 执行租约恢复扫描间隔,范围100ms–1m。
|
||||
RuntimeBatchLimit int // RuntimeBatchLimit 每个后台批次的候选上限,范围1–100。
|
||||
ShutdownTimeout time.Duration // ShutdownTimeout 优雅排空和关闭窗口,范围1s–5m。
|
||||
HTTPAddr string
|
||||
MySQLDSN string
|
||||
MaxOpen int
|
||||
MaxIdle int
|
||||
MetricsAddr string
|
||||
WorkflowTickInterval time.Duration // WorkflowTickInterval Weave 持久化推进扫描间隔,范围100ms–1m。
|
||||
ScheduleTickInterval time.Duration // ScheduleTickInterval 计划扫描间隔,范围100ms–1m。
|
||||
RecoveryTickInterval time.Duration // RecoveryTickInterval 执行租约恢复扫描间隔,范围100ms–1m。
|
||||
RuntimeBatchLimit int // RuntimeBatchLimit 每个后台批次的候选上限,范围1–100。
|
||||
ShutdownTimeout time.Duration // ShutdownTimeout 优雅排空和关闭窗口,范围1s–5m。
|
||||
WorkerConnectionLimit int // WorkerConnectionLimit 单节点 Worker WebSocket 连接上限,范围1–4096。
|
||||
EventConnectionLimit int // EventConnectionLimit 单节点 Event WebSocket 连接上限,范围1–4096。
|
||||
DispatchWorkers int // DispatchWorkers 单个 Hub 并发执行数据库领取调用的协程数,范围1–256。
|
||||
DispatchCapacity int // DispatchCapacity 单个 Hub 领取调用的有界内存排队槽位,范围1–65536。
|
||||
}
|
||||
|
||||
func Load() (Config, error) { return FromEnv(os.Getenv) }
|
||||
func FromEnv(get func(string) string) (Config, error) {
|
||||
c := Config{
|
||||
HTTPAddr: get("ORBIT_HTTP_ADDR"),
|
||||
MySQLDSN: get("ORBIT_MYSQL_DSN"),
|
||||
MaxOpen: 20,
|
||||
MaxIdle: 5,
|
||||
MetricsAddr: get("ORBIT_METRICS_ADDR"),
|
||||
WorkflowTickInterval: time.Second,
|
||||
ScheduleTickInterval: time.Second,
|
||||
RecoveryTickInterval: 2 * time.Second,
|
||||
RuntimeBatchLimit: 100,
|
||||
ShutdownTimeout: 10 * time.Second,
|
||||
HTTPAddr: get("ORBIT_HTTP_ADDR"),
|
||||
MySQLDSN: get("ORBIT_MYSQL_DSN"),
|
||||
MaxOpen: 20,
|
||||
MaxIdle: 5,
|
||||
MetricsAddr: get("ORBIT_METRICS_ADDR"),
|
||||
WorkflowTickInterval: time.Second,
|
||||
ScheduleTickInterval: time.Second,
|
||||
RecoveryTickInterval: 2 * time.Second,
|
||||
RuntimeBatchLimit: 100,
|
||||
ShutdownTimeout: 10 * time.Second,
|
||||
WorkerConnectionLimit: 128,
|
||||
EventConnectionLimit: 128,
|
||||
DispatchWorkers: 4,
|
||||
DispatchCapacity: 128,
|
||||
}
|
||||
if c.HTTPAddr == "" {
|
||||
c.HTTPAddr = "127.0.0.1:8080"
|
||||
@@ -86,6 +94,25 @@ func FromEnv(get func(string) string) (Config, error) {
|
||||
}
|
||||
c.ShutdownTimeout = d
|
||||
}
|
||||
for _, field := range []struct {
|
||||
name string
|
||||
dst *int
|
||||
min int
|
||||
max int
|
||||
}{
|
||||
{"ORBIT_WORKER_CONNECTION_LIMIT", &c.WorkerConnectionLimit, 1, 4096},
|
||||
{"ORBIT_EVENT_CONNECTION_LIMIT", &c.EventConnectionLimit, 1, 4096},
|
||||
{"ORBIT_DISPATCH_WORKERS", &c.DispatchWorkers, 1, 256},
|
||||
{"ORBIT_DISPATCH_CAPACITY", &c.DispatchCapacity, 1, 65536},
|
||||
} {
|
||||
if raw := get(field.name); raw != "" {
|
||||
n, err := strconv.Atoi(raw)
|
||||
if err != nil || n < field.min || n > field.max {
|
||||
return Config{}, fmt.Errorf("%s must be an integer from %d to %d", field.name, field.min, field.max)
|
||||
}
|
||||
*field.dst = n
|
||||
}
|
||||
}
|
||||
if c.MetricsAddr != "" {
|
||||
_, port, err := net.SplitHostPort(c.MetricsAddr)
|
||||
n, parseErr := strconv.Atoi(port)
|
||||
|
||||
@@ -9,7 +9,7 @@ func TestRuntimeDefaultsAndBoundedOverrides(t *testing.T) {
|
||||
base := map[string]string{"ORBIT_MYSQL_DSN": "private-dsn"}
|
||||
get := func(k string) string { return base[k] }
|
||||
c, err := FromEnv(get)
|
||||
if err != nil || c.WorkflowTickInterval != time.Second || c.ScheduleTickInterval != time.Second || c.RecoveryTickInterval != 2*time.Second || c.RuntimeBatchLimit != 100 || c.ShutdownTimeout != 10*time.Second {
|
||||
if err != nil || c.WorkflowTickInterval != time.Second || c.ScheduleTickInterval != time.Second || c.RecoveryTickInterval != 2*time.Second || c.RuntimeBatchLimit != 100 || c.ShutdownTimeout != 10*time.Second || c.WorkerConnectionLimit != 128 || c.EventConnectionLimit != 128 || c.DispatchWorkers != 4 || c.DispatchCapacity != 128 {
|
||||
t.Fatalf("unexpected runtime defaults: %+v err=%v", c, err)
|
||||
}
|
||||
base["ORBIT_WORKFLOW_TICK_INTERVAL"] = "250ms"
|
||||
@@ -17,16 +17,24 @@ func TestRuntimeDefaultsAndBoundedOverrides(t *testing.T) {
|
||||
base["ORBIT_RECOVERY_TICK_INTERVAL"] = "1m"
|
||||
base["ORBIT_RUNTIME_BATCH_LIMIT"] = "37"
|
||||
base["ORBIT_SHUTDOWN_TIMEOUT"] = "45s"
|
||||
base["ORBIT_WORKER_CONNECTION_LIMIT"] = "512"
|
||||
base["ORBIT_EVENT_CONNECTION_LIMIT"] = "256"
|
||||
base["ORBIT_DISPATCH_WORKERS"] = "16"
|
||||
base["ORBIT_DISPATCH_CAPACITY"] = "2048"
|
||||
c, err = FromEnv(get)
|
||||
if err != nil || c.WorkflowTickInterval != 250*time.Millisecond || c.ScheduleTickInterval != 1500*time.Millisecond || c.RecoveryTickInterval != time.Minute || c.RuntimeBatchLimit != 37 || c.ShutdownTimeout != 45*time.Second {
|
||||
if err != nil || c.WorkflowTickInterval != 250*time.Millisecond || c.ScheduleTickInterval != 1500*time.Millisecond || c.RecoveryTickInterval != time.Minute || c.RuntimeBatchLimit != 37 || c.ShutdownTimeout != 45*time.Second || c.WorkerConnectionLimit != 512 || c.EventConnectionLimit != 256 || c.DispatchWorkers != 16 || c.DispatchCapacity != 2048 {
|
||||
t.Fatalf("valid runtime override rejected: %+v err=%v", c, err)
|
||||
}
|
||||
for key, value := range map[string]string{
|
||||
"ORBIT_WORKFLOW_TICK_INTERVAL": "99ms",
|
||||
"ORBIT_SCHEDULE_TICK_INTERVAL": "2m",
|
||||
"ORBIT_RECOVERY_TICK_INTERVAL": "not-a-duration",
|
||||
"ORBIT_RUNTIME_BATCH_LIMIT": "101",
|
||||
"ORBIT_SHUTDOWN_TIMEOUT": "500ms",
|
||||
"ORBIT_WORKFLOW_TICK_INTERVAL": "99ms",
|
||||
"ORBIT_SCHEDULE_TICK_INTERVAL": "2m",
|
||||
"ORBIT_RECOVERY_TICK_INTERVAL": "not-a-duration",
|
||||
"ORBIT_RUNTIME_BATCH_LIMIT": "101",
|
||||
"ORBIT_SHUTDOWN_TIMEOUT": "500ms",
|
||||
"ORBIT_WORKER_CONNECTION_LIMIT": "0",
|
||||
"ORBIT_EVENT_CONNECTION_LIMIT": "4097",
|
||||
"ORBIT_DISPATCH_WORKERS": "257",
|
||||
"ORBIT_DISPATCH_CAPACITY": "65537",
|
||||
} {
|
||||
base[key] = value
|
||||
if _, err := FromEnv(get); err == nil {
|
||||
|
||||
Reference in New Issue
Block a user