| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/jackc/pgx/v5" |
| 9 | "github.com/jackc/pgx/v5/pgxpool" |
| 10 | ) |
| 11 | |
| 12 | const ( |
| 13 | backOffBase = 10 * |
| 14 | backOffCap = time. |
| 15 | ) |
| 16 | |
| 17 | type Job struct { |
| 18 | ID, Kind, |
| 19 | Payload json. |
| 20 | EndpointID *string |
| 21 | Attempts int |
| 22 | } |
| 23 | |
| 24 | type sendFunc func(ctx |
| 25 | |
| 26 | const claimSQL = ` |
| 27 | UPDATE delivery_job |
| 28 | SET locked_until = now() + interval '60 seconds', |
| 29 | claim_token = gen_random_uuid(), |
| 30 | attempts = attempts + 1, |
| 31 | updated_at = now() |
| 32 | WHERE id IN ( |
| 33 | SELECT id FROM delivery_job |
| 34 | WHERE status = 'pending' AND next_attempt_at <= now() AND locked_until < now() |
| 35 | ORDER BY next_attempt_at |
| 36 | LIMIT $1 |
| 37 | FOR UPDATE SKIP LOCKED |
| 38 | ) |
| 39 | RETURNING id, kind, claim_token, payload, endpoint_id, attempts;` |
| 40 | |
| 41 | func claim(ctx context |
| 42 | rows, err := |
| 43 | if err != nil |
| 44 | return nil, err |
| 45 | } |
| 46 | return pgx.CollectRows( |
| 47 | } |
| 48 | |
| 49 | func complete(ctx context |
| 50 | panic("todo") |
| 51 | } |
| 52 | |
| 53 | func fail(ctx context |
| 54 | panic("todo") |
| 55 | } |
| 56 | |
| 57 | func backoff(attempt int |
| 58 | attempt = min( |
| 59 | d := min( |
| 60 | n := randN( |
| 61 | return time.Duration( |
| 62 | } |
| 63 | |
| 64 | func listen(ctx context |
| 65 | panic("todo") |
| 66 | } |
| 67 | |
| 68 | func run(ctx context |
| 69 | panic("todo") |
| 70 | } |