Repository navigation
Expand file tree
/
Copy pathqueue.go
More file actions
154 lines (137 loc) · 5.14 KB
/
Copy pathqueue.go
File metadata and controls
154 lines (137 loc) · 5.14 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) 2025, The GoKit Authors
// MIT License
// All rights reserved.
package bunmq
import (
"fmt"
"time"
)
// QueueDefinition represents the configuration of a RabbitMQ queue.
// It encapsulates properties such as name, durability, auto-delete behavior,
// exclusivity, TTL, DLQ (Dead Letter Queue), and retry mechanisms.
type QueueDefinition struct {
name string
quorum bool
durable bool
delete bool
exclusive bool
withTTL bool
ttl time.Duration
withMaxLength bool
maxLength int64
withDLQ bool
dqlName string
withDLQMaxLength bool
dlqMaxLength int64
withRetry bool
retryTTL time.Duration
retries int64
}
// NewQueue creates a new queue definition with the given name.
// By default, queues are durable, not auto-deleted, and not exclusive.
// You can chain methods to configure additional properties such as TTL, DLQ, and retry behavior.
//
// Example usage:
//
// queueDef := bunmq.NewQueue("my-queue").Durable(true).WithRetry(time.Second*10, 3).WithDLQ()
//
// This creates a durable queue with retry and dead letter queue enabled.
//
// Note: The queue name must be unique within the RabbitMQ broker.
// It is used to declare the queue and bind it to exchanges.
// If the queue already exists with different properties, an error will be returned when trying to declare
func NewQueue(name string) *QueueDefinition {
return &QueueDefinition{name: name, durable: true, delete: false, exclusive: false}
}
// Durable sets the durability flag for the queue.
// Durable queues survive broker restarts.
func (q *QueueDefinition) Durable(d bool) *QueueDefinition {
q.durable = d
return q
}
// Delete sets the auto-delete flag for the queue.
// Auto-deleted queues are removed when no longer in use.
func (q *QueueDefinition) Delete(d bool) *QueueDefinition {
q.delete = d
return q
}
// Exclusive sets the exclusive flag for the queue.
// Exclusive queues can only be used by the connection that created them
// and are deleted when that connection closes.
func (q *QueueDefinition) Exclusive(e bool) *QueueDefinition {
q.exclusive = e
return q
}
// WithMaxLength sets a maximum length for the queue.
func (q *QueueDefinition) WithMaxLength(length int64) *QueueDefinition {
q.withMaxLength = true
q.maxLength = length
return q
}
// WithTTL sets a Time-To-Live (TTL) for messages in the queue.
// Messages that remain in the queue longer than the TTL will be automatically removed.
func (q *QueueDefinition) WithTTL(ttl time.Duration) *QueueDefinition {
q.withTTL = true
q.ttl = ttl
return q
}
// WithDLQ enables a Dead Letter Queue (DLQ) for this queue.
// Messages that are rejected, expired, or exceed max length will be routed to the DLQ.
// The DLQ name is automatically generated as "<queue-name>-dlq".
func (q *QueueDefinition) WithDLQ() *QueueDefinition {
q.withDLQ = true
q.dqlName = fmt.Sprintf("%s-dlq", q.name)
return q
}
// WithDLQMaxLength sets a maximum length for the Dead Letter Queue.
func (q *QueueDefinition) WithDLQMaxLength(length int64) *QueueDefinition {
q.withDLQMaxLength = true
q.dlqMaxLength = length
return q
}
// WithRetry enables a retry mechanism for this queue.
// Failed messages will be moved to a retry queue for the specified TTL duration
// and then requeued up to the specified number of retries.
func (q *QueueDefinition) WithRetry(ttl time.Duration, retries int64) *QueueDefinition {
q.withRetry = true
q.retryTTL = ttl
q.retries = retries
return q
}
// Quorum marks the queue as a quorum queue. It sets the internal quorum flag
// on the receiver so the queue will use quorum-style semantics (replicated
// storage and stronger availability guarantees) instead of a classic queue.
// This method modifies the receiver in place and returns the same
// *QueueDefinition to allow fluent chaining of configuration calls.
func (q *QueueDefinition) Quorum() *QueueDefinition {
q.quorum = true
return q
}
// queueType returns the canonical string identifier for the queue represented by
// the receiver. If the QueueDefinition is configured as a quorum queue
// (q.quorum == true), it returns "quorum"; otherwise it returns "classic".
// This helper is intended for use in configuration generation, serialization,
// logging, and other places where a human- or API-facing name for the queue
// kind is required. It does not modify the receiver.
func (q *QueueDefinition) queueType() string {
if q.quorum {
return "quorum"
}
return "classic"
}
// Name returns the name of the queue.
// This is the identifier used to declare and bind the queue in RabbitMQ.
// It is also used to create the DLQ and retry queue names.
func (q *QueueDefinition) Name() string {
return q.name
}
// DLQName returns the name of the Dead Letter Queue associated with this queue.
// The DLQ name follows the pattern "<queue-name>-dlq".
func (q *QueueDefinition) DLQName() string {
return fmt.Sprintf("%s-dlq", q.name)
}
// RetryName returns the name of the Retry Queue associated with this queue.
// The Retry Queue name follows the pattern "<queue-name>-retry".
func (q *QueueDefinition) RetryName() string {
return fmt.Sprintf("%s-retry", q.name)
}