fix: 仅向已握手连接推送并拆分调度循环
This commit is contained in:
@@ -7,7 +7,7 @@ import (
|
||||
"git.asio.asia/nixevol/NixMsg/internal/app/port"
|
||||
)
|
||||
|
||||
// OnHandshakeComplete 握手完成:写 online_since、清空不 keep 的 expire_at,并推送。
|
||||
// OnHandshakeComplete 握手完成:写 online_since、清空不 keep 的 expire_at,启动推送 worker。
|
||||
// 调用方须先把连接登记进 ConnRegistry(MemoryConns.Set)。
|
||||
func (a *App) OnHandshakeComplete(ctx context.Context, endpointID string, conn LiveConn) error {
|
||||
nowMs := a.now().UnixMilli()
|
||||
@@ -15,15 +15,23 @@ func (a *App) OnHandshakeComplete(ctx context.Context, endpointID string, conn L
|
||||
if _, err := tx.Exec(`UPDATE endpoints SET online_since = ? WHERE id = ?`, nowMs, endpointID); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := tx.Exec(`
|
||||
if _, err := tx.Exec(`
|
||||
UPDATE deliveries SET expire_at = NULL, updated_at = ?
|
||||
WHERE endpoint_id = ? AND state = 'pending' AND keep = 0`, nowMs, endpointID)
|
||||
WHERE endpoint_id = ? AND state = 'pending' AND keep = 0`, nowMs, endpointID); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := tx.Exec(`
|
||||
UPDATE deliveries SET pushed_conn = NULL, updated_at = ?
|
||||
WHERE endpoint_id = ? AND state = 'pending' AND pushed_conn = ?`, nowMs, endpointID, string(conn.ConnID))
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return a.PushPending(ctx, endpointID, conn.ConnID)
|
||||
a.markHandshook(endpointID, conn)
|
||||
a.startPushWorker(endpointID, conn.ConnID)
|
||||
a.WakePush(endpointID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// OnDisconnect 连接断开:当前连接则延长宽限;按代号清 pushed_conn。
|
||||
@@ -33,6 +41,10 @@ func (a *App) OnDisconnect(ctx context.Context, endpointID string, connID port.C
|
||||
graceMs := a.lim.GraceSeconds * 1000
|
||||
deadline := nowMs + graceMs
|
||||
|
||||
a.stopPushWorker(endpointID, connID)
|
||||
a.clearHandshook(endpointID, connID)
|
||||
a.clearReceiptInflight(string(connID))
|
||||
|
||||
err := a.db.Queue.Do(ctx, func(tx *sql.Tx) error {
|
||||
if isCurrent {
|
||||
if _, err := tx.Exec(`UPDATE endpoints SET offline_since = ? WHERE id = ?`, nowMs, endpointID); err != nil {
|
||||
@@ -62,7 +74,7 @@ WHERE endpoint_id = ? AND state = 'pending' AND keep = 1 AND pushed_conn = ?`,
|
||||
}
|
||||
_, err := tx.Exec(`
|
||||
UPDATE deliveries SET pushed_conn = NULL, updated_at = ?
|
||||
WHERE state = 'pending' AND pushed_conn = ?`, nowMs, string(connID))
|
||||
WHERE endpoint_id = ? AND state = 'pending' AND pushed_conn = ?`, nowMs, endpointID, string(connID))
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user