-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfra.queues.go
59 lines (49 loc) · 1.33 KB
/
infra.queues.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"context"
"encoding/json"
"time"
"github.com/redis/go-redis/v9"
)
// Predefinied Queue IDs.
const (
CreateQueue = "creation"
UpdateQueue = "updating"
DeleteQueue = "deletion"
)
// Ensure *Queue implements Queuer.
var _ Queuer = (*redisQueue)(nil)
// Queuer describes a queue.
type Queuer interface {
Push(ctx context.Context, qid string, book Book) error
Pop(ctx context.Context, qids ...string) (string, Book, error)
}
// redisQueue represents a queue which implements the Queuer interface.
type redisQueue struct {
client *redis.Client
}
func NewRedisQueue(client *redis.Client) Queuer {
return &redisQueue{client: client}
}
// Push enqueues a book onto the queue identified by qid.
func (q *redisQueue) Push(ctx context.Context, qid string, book Book) error {
bookBytes, err := json.Marshal(book)
if err != nil {
return err
}
return q.client.RPush(ctx, qid, bookBytes).Err()
}
// Pop returns the first dequeued book from the list of queue ids.
func (q *redisQueue) Pop(ctx context.Context, qids ...string) (string, Book, error) {
var book Book
var qid string
infos, err := q.client.BLPop(ctx, 0*time.Second, qids...).Result()
if err != nil {
return qid, book, err
}
if err = json.Unmarshal([]byte(infos[1]), &book); err != nil {
return qid, book, err
}
qid = infos[0]
return qid, book, nil
}