mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 00:14:00 +00:00
feat: persist authenticated queue probe RTT
This commit is contained in:
@@ -137,6 +137,28 @@ type queueTicketRecord struct {
|
||||
|
||||
type PostgresQueue struct{ DB *sql.DB }
|
||||
|
||||
const QueueProbeRecordSQL = `UPDATE queue_tickets
|
||||
SET predicted_rtt = jsonb_set(COALESCE(predicted_rtt, '{}'::jsonb), ARRAY[$2], to_jsonb($3::double precision), true)
|
||||
WHERE player_id = $1 AND state IN ('QUEUED', 'PROPOSED') AND expires_at > $4`
|
||||
|
||||
func (q PostgresQueue) RecordProbe(ctx context.Context, playerID, region string, rtt time.Duration, now time.Time) error {
|
||||
if q.DB == nil || playerID == "" || (region != "EU" && region != "NA") || rtt < 0 || now.IsZero() {
|
||||
return fmt.Errorf("invalid probe recording")
|
||||
}
|
||||
result, err := q.DB.ExecContext(ctx, QueueProbeRecordSQL, playerID, region, float64(rtt)/float64(time.Millisecond), now)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
changed, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if changed == 0 {
|
||||
return domain.ErrTicketNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q PostgresQueue) Create(ctx context.Context, playerID, ticketID, idempotencyKey string, spec domain.QueueSpec, now time.Time) (domain.QueueTicket, error) {
|
||||
return CreateQueueTicket(ctx, q.DB, ticketID, playerID, idempotencyKey, spec, now)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user