/
queue.go
44 lines (37 loc) · 1.11 KB
/
queue.go
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
package natsrouter
import (
"github.com/nats-io/nats.go"
)
// Queue group that can be subscribed to subjects
type Queue struct {
n *NatsRouter
queue string
}
// Returns a new `Queue` object with additional middleware functions
func (q *Queue) WithMiddleware(fns ...NatsMiddlewareFunc) *Queue {
return &Queue{
n: q.n.Use(fns...),
queue: q.queue,
}
}
// Alias for `WithMiddleware`
func (q *Queue) Use(fns ...NatsMiddlewareFunc) *Queue {
return q.WithMiddleware(fns...)
}
// Subscribe to a subject as a part of this queue group with the specified
// handler function
func (q *Queue) Subscribe(subject string, handler NatsCtxHandler) (*nats.Subscription, error) {
return q.n.QueueSubscribe(subject, q.queue, handler)
}
// Same as Subscribe, with channel support
func (q *Queue) ChanSubscribe(subject string, ch chan *NatsMsg) (*nats.Subscription, error) {
return q.n.ChanQueueSubscribe(subject, q.queue, ch)
}
// Create a new `Subject` object that is part of this `Queue` group
func (q *Queue) Subject(subjects ...string) *Subject {
return &Subject{
n: q.n,
queue: q,
subjects: subjects,
}
}