fix: 仅向已握手连接推送并拆分调度循环
This commit is contained in:
@@ -19,6 +19,8 @@ func (a *App) Ack(ctx context.Context, endpointID string, req *protocol.Ack) (Ac
|
||||
var seq int64
|
||||
var ackLatencySec float64
|
||||
var observeAck bool
|
||||
var changed bool
|
||||
var wroteReceipt bool
|
||||
err := a.db.Queue.Do(ctx, func(tx *sql.Tx) error {
|
||||
err := tx.QueryRow(`SELECT seq FROM messages WHERE sender_id = ? AND id = ?`, req.From, req.ID).Scan(&seq)
|
||||
if err == sql.ErrNoRows {
|
||||
@@ -40,14 +42,17 @@ WHERE seq = ? AND endpoint_id = ? AND state = 'pending'`,
|
||||
}
|
||||
aff, _ := res.RowsAffected()
|
||||
if aff > 0 {
|
||||
changed = true
|
||||
out.Result = DeliveryAccepted
|
||||
if pushedAt.Valid && pushedAt.Int64 > 0 && nowMs >= pushedAt.Int64 {
|
||||
ackLatencySec = float64(nowMs-pushedAt.Int64) / 1000.0
|
||||
observeAck = true
|
||||
}
|
||||
if e := insertReceiptTx(tx, req.From, seq, endpointID, DeliveryAccepted, "", nowMs); e != nil {
|
||||
wrote, e := insertReceiptTx(tx, req.From, seq, endpointID, DeliveryAccepted, "", nowMs)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
wroteReceipt = wrote
|
||||
return TryFinalizeTx(tx, seq, nowMs, a.lim.RecordRetentionDays)
|
||||
}
|
||||
var state string
|
||||
@@ -68,11 +73,12 @@ SELECT state FROM deliveries WHERE seq = ? AND endpoint_id = ?`, seq, endpointID
|
||||
if observeAck && a.met != nil {
|
||||
a.met.AckSeconds.Observe(ackLatencySec)
|
||||
}
|
||||
if out.Result == DeliveryAccepted {
|
||||
a.releaseLarge(seq, endpointID)
|
||||
if changed {
|
||||
a.WakePush(endpointID)
|
||||
}
|
||||
if wroteReceipt {
|
||||
a.WakePush(req.From)
|
||||
}
|
||||
a.WakePush(endpointID)
|
||||
a.WakePush(req.From)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
@@ -294,8 +300,25 @@ func (a *App) ReceiptAck(ctx context.Context, endpointID string, req *protocol.R
|
||||
if err != nil {
|
||||
return errCode(protocol.CodeBadRequest, "invalid receipt_id")
|
||||
}
|
||||
return a.db.Queue.Do(ctx, func(tx *sql.Tx) error {
|
||||
_, err := tx.Exec(`UPDATE receipts SET acked = 1 WHERE receipt_id = ? AND sender_id = ?`, rid, endpointID)
|
||||
return err
|
||||
var acked bool
|
||||
err = a.db.Queue.Do(ctx, func(tx *sql.Tx) error {
|
||||
res, err := tx.Exec(`UPDATE receipts SET acked = 1 WHERE receipt_id = ? AND sender_id = ? AND acked = 0`, rid, endpointID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
aff, _ := res.RowsAffected()
|
||||
acked = aff > 0
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if acked {
|
||||
live, ok := a.lookupConn(endpointID)
|
||||
if ok {
|
||||
a.unmarkReceipt(string(live.ConnID), rid)
|
||||
}
|
||||
a.WakePush(endpointID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user