-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_service.go
42 lines (34 loc) · 864 Bytes
/
queue_service.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
package worker
// response represents a queue response.
type response struct {
Msg Message
Err error
}
// queueService represents a Queue wrapper which allows
// polling the queue without blocking.
type queueService struct {
queue Queue
}
// newQueueService returns a queueService instance.
func newQueueService(q Queue) *queueService {
return &queueService{queue: q}
}
// get service peeks a message from queue and
// returns it through a channel without blocking
// the caller.
func (qs *queueService) get() <-chan *response {
// Buffered channel to avoid goroutine leaking.
out := make(chan *response, 1)
// Perform the request in a separate
// goroutine to avoid blocking.
go func() {
defer close(out)
msg, err := qs.queue.Get()
if err != nil {
out <- &response{Err: err}
return
}
out <- &response{Msg: msg}
}()
return out
}