-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoqueue.go
More file actions
123 lines (99 loc) · 2.49 KB
/
goqueue.go
File metadata and controls
123 lines (99 loc) · 2.49 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package goqueue
import (
"context"
"encoding/json"
"sync"
)
// GoQueue is the main queue manager that coordinates workers and the backend
type GoQueue struct {
backend Backend
config Config
handlers map[string]Handler
workers map[string]*workerPool
mu sync.RWMutex
stopped bool
}
// New creates a new GoQueue instance with the given backend and options
func New(backend Backend, opts ...Option) *GoQueue {
config := DefaultConfig()
for _, opt := range opts {
opt(&config)
}
return &GoQueue{
backend: backend,
config: config,
handlers: make(map[string]Handler),
workers: make(map[string]*workerPool),
stopped: false,
}
}
// Publish publishes a message to its designated queue.
// The message declares which queue it belongs to via QueueName().
// The message will be automatically serialized to JSON.
func (gq *GoQueue) Publish(ctx context.Context, message QueueMessage) error {
jsonData, err := json.Marshal(message)
if err != nil {
return err
}
queue := message.QueueName()
return gq.PublishBytes(ctx, queue, jsonData)
}
// PublishBytes publishes raw bytes to the specified queue.
func (gq *GoQueue) PublishBytes(ctx context.Context, queue string, data []byte) error {
gq.mu.RLock()
if gq.stopped {
gq.mu.RUnlock()
return ErrQueueStopped
}
gq.mu.RUnlock()
env := NewEnvelope(queue, data)
return gq.backend.Publish(ctx, queue, env)
}
// Register registers a handler for its designated queue.
// Must be called before Start().
func (gq *GoQueue) Register(handler Handler) error {
gq.mu.Lock()
defer gq.mu.Unlock()
if gq.stopped {
return ErrQueueStopped
}
queue := handler.QueueName()
if _, exists := gq.handlers[queue]; exists {
return ErrHandlerAlreadyRegistered
}
gq.handlers[queue] = handler
return nil
}
// Start starts workers for all registered queues.
func (gq *GoQueue) Start(ctx context.Context) error {
gq.mu.Lock()
defer gq.mu.Unlock()
if gq.stopped {
return ErrQueueStopped
}
if len(gq.handlers) == 0 {
return ErrNoHandlersRegistered
}
for queue, handler := range gq.handlers {
if _, exists := gq.workers[queue]; exists {
continue
}
wp := newWorkerPool(gq.backend, gq.config, queue, handler)
gq.workers[queue] = wp
go wp.start(ctx)
}
return nil
}
// Stop gracefully stops all workers and closes the backend connection
func (gq *GoQueue) Stop() error {
gq.mu.Lock()
defer gq.mu.Unlock()
if gq.stopped {
return nil
}
gq.stopped = true
for _, wp := range gq.workers {
wp.stop()
}
return gq.backend.Close()
}