fix: keep load test websocket sessions alive

This commit is contained in:
richjohnrambo
2026-09-27 21:41:50 +08:00
parent bde02e8eac
commit ea8732e2b1
7 changed files with 111 additions and 1 deletions
+37
View File
@@ -116,6 +116,12 @@ func connectWSClient(ctx context.Context, cfg *Config, account AccountRecord, ap
return nil, fmt.Errorf("login %s: %w", account.Username, err)
}
client.applyLogin(ctrl)
pingInterval, err := configDuration(cfg.WSPingInterval)
if err != nil {
client.Close()
return nil, err
}
client.startKeepalive(pingInterval)
return client, nil
}
@@ -369,6 +375,37 @@ func (c *WSClient) readLoop() {
}
}
func (c *WSClient) startKeepalive(interval time.Duration) {
go func() {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
deadline := time.Now().Add(interval / 2)
if err := c.conn.WriteControl(websocket.PingMessage, nil, deadline); err != nil {
_ = c.conn.Close()
return
}
case <-c.done:
return
}
}
}()
}
func (c *WSClient) alive() bool {
if c == nil || c.closed.Load() {
return false
}
select {
case <-c.done:
return false
default:
return true
}
}
func (c *WSClient) Close() {
if c == nil || !c.closed.CompareAndSwap(false, true) {
return