| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "log" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/jackc/pgx/v5" |
| 10 | "github.com/jackc/pgx/v5/pgxpool" |
| 11 | ) |
| 12 | |
| 13 | const ( |
| 14 | backOffBase = 10 * |
| 15 | backOffCap = time. |
| 16 | ) |
| 17 | |
| 18 | type Job struct { |
| 19 | ID, Kind, |
| 20 | Payload json. |
| 21 | EndpointID *string |
| 22 | Attempts int |
| 23 | } |
| 24 | |
| 25 | type sendFunc func(ctx |
| 26 | |
| 27 | const claimSQL = ` |
| 28 | UPDATE delivery_job |
| 29 | SET locked_until = now() + interval '60 seconds', |
| 30 | claim_token = gen_random_uuid(), |
| 31 | attempts = attempts + 1, |
| 32 | updated_at = now() |
| 33 | WHERE id IN ( |
| 34 | SELECT id FROM delivery_job |
| 35 | WHERE status = 'pending' AND next_attempt_at <= now() AND locked_until < now() |
| 36 | ORDER BY next_attempt_at |
| 37 | LIMIT $1 |
| 38 | FOR UPDATE SKIP LOCKED |
| 39 | ) |
| 40 | RETURNING id, kind, claim_token, payload, endpoint_id, attempts;` |
| 41 | |
| 42 | func claim(ctx context |
| 43 | rows, err := |
| 44 | if err != nil |
| 45 | return nil, err |
| 46 | } |
| 47 | return pgx.CollectRows( |
| 48 | } |
| 49 | |
| 50 | const completeSQL = ` |
| 51 | UPDATE delivery_job |
| 52 | SET status = 'succeeded', last_error = NULL, locked_until = now(), updated_at = now() |
| 53 | WHERE id = $1 AND claim_token = $2` |
| 54 | |
| 55 | func complete(ctx context |
| 56 | tag, err := |
| 57 | if err != nil |
| 58 | return err |
| 59 | } |
| 60 | if tag.RowsAffected() |
| 61 | return errLeaseLost |
| 62 | } |
| 63 | return nil |
| 64 | } |
| 65 | |
| 66 | const failSQL = ` |
| 67 | UPDATE delivery_job SET |
| 68 | status = CASE WHEN created_at < now() - interval '3 days' |
| 69 | THEN 'dead'::delivery_job_status ELSE 'pending' END, |
| 70 | next_attempt_at = now() + $3 * interval '1 millisecond', |
| 71 | last_error = $4, locked_until = now(), updated_at = now() |
| 72 | WHERE id = $1 AND claim_token = $2 |
| 73 | ` |
| 74 | |
| 75 | func fail(ctx context |
| 76 | tag, err := |
| 77 | if err != nil |
| 78 | return err |
| 79 | } |
| 80 | if tag.RowsAffected() |
| 81 | return errLeaseLost |
| 82 | } |
| 83 | return nil |
| 84 | } |
| 85 | |
| 86 | func backoff(attempt int |
| 87 | attempt = min( |
| 88 | d := min( |
| 89 | n := randN( |
| 90 | return time.Duration( |
| 91 | } |
| 92 | |
| 93 | // listen signals wake on every NOTIFY delivery until ctx is canceled, reconnecting when the connection drops. Polling in run covers any gap. |
| 94 | func listen(ctx context |
| 95 | for { |
| 96 | err := listenOnce( |
| 97 | if ctx.Err() |
| 98 | return |
| 99 | } |
| 100 | log.Printf("listen: |
| 101 | select { |
| 102 | case <-ctx.Done |
| 103 | return |
| 104 | case <-time.After |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // listenOnce holds one connection and returns when it breaks. |
| 110 | func listenOnce(ctx context |
| 111 | conn, err := |
| 112 | if err != nil |
| 113 | return err |
| 114 | } |
| 115 | defer conn.Release() |
| 116 | |
| 117 | if _, err |
| 118 | return err |
| 119 | } |
| 120 | // Jobs inserted while we were disconnected got no NOTIFY. |
| 121 | notify(wake) |
| 122 | |
| 123 | for { |
| 124 | if _, err |
| 125 | return err |
| 126 | } |
| 127 | notify(wake) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // notify sends on wake without blocking; a pending signal already covers this one. |
| 132 | func notify(wake chan |
| 133 | select { |
| 134 | case wake <- struct |
| 135 | default: |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | func run(ctx context |
| 140 | panic("todo") |
| 141 | } |