Repository navigation
Expand file tree
/
Copy pathqueue.go
More file actions
55 lines (46 loc) · 2.1 KB
/
Copy pathqueue.go
File metadata and controls
55 lines (46 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Package queue is MailX's asynchronous work-scheduling boundary between
// message acceptance and delivery execution. It only schedules and owns
// opaque work references — no SMTP, MIME, or retry-policy knowledge here.
package queue
import (
"context"
"errors"
"time"
)
var (
ErrQueueClosed = errors.New("queue: closed")
ErrUnknownJob = errors.New("queue: unknown job")
ErrJobNotClaimed = errors.New("queue: job is not claimed by this token")
ErrInvalidCapacity = errors.New("queue: capacity must be positive")
)
// Claim is the ownership handle returned by Queue.Claim; Token guards
// Renew/Ack/Release against stale or reused ownership. Durable implementations
// may lease claims so abandoned work can be recovered.
type Claim struct {
Job Job
Token uint64
}
// Queue provides at-least-once, not exactly-once, job processing: nothing
// here makes a remote SMTP side effect and Ack atomic, so a crash between
// acceptance and Ack can still cause a duplicate delivery attempt. The
// in-memory implementation also loses all state, including in-flight
// claims, on process restart.
type Queue interface {
// Enqueue is a no-op if job.ID is already active; blocks at capacity.
Enqueue(ctx context.Context, job Job) error
// Claim blocks until a job is available, returning exclusive ownership.
Claim(ctx context.Context) (Claim, error)
// Renew extends ownership of a claimed job. Durable queues use it to
// prevent a live worker performing required bookkeeping from being
// mistaken for a dead worker; in-process queues validate ownership and
// otherwise have nothing to extend.
Renew(ctx context.Context, id string, token uint64) error
// Ack completes a claimed job and frees one unit of capacity.
Ack(ctx context.Context, id string, token uint64) error
// Release returns a claimed job to the pool, claimable again at
// availableAt. Capacity is not freed; the caller decides availableAt.
Release(ctx context.Context, id string, token uint64, availableAt time.Time) error
// Close stops new Enqueue/Claim calls; in-flight claims may still be
// Ack'd or Released. Idempotent.
Close() error
}