|
| 1 | +package redis |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/gob" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/go-redis/redis" |
| 10 | +) |
| 11 | + |
| 12 | +const defaultKey = "value" // a default key needed to use RedisStreams |
| 13 | + |
| 14 | +// RedisQueue implements the PublishSubscribe interface, but only publishes to |
| 15 | +// one subscriber. Implemented by having Publishers push into a queue, and |
| 16 | +// Subscribers read from the queue and pop off from the top. |
| 17 | +type RedisQueue struct { |
| 18 | + client *redis.Client |
| 19 | +} |
| 20 | + |
| 21 | +// NewRedisQueue creates a new queue and a group associated with that queue. |
| 22 | +// Underlying mecahnism uses Redis Streams. |
| 23 | +func NewRedisQueue(client *redis.Client, queueID, groupID string) *RedisQueue { |
| 24 | + _, err := client.XGroupCreateMkStream(queueID, groupID, "$").Result() |
| 25 | + if err != nil { |
| 26 | + fmt.Println("Error creating redis group stream") |
| 27 | + os.Exit(1) |
| 28 | + } |
| 29 | + return &RedisQueue{ |
| 30 | + client: client, |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// Subscribe creates a goroutine that subscribes to a RedisStream, based on |
| 35 | +// queueID, groupID, consumerID. Sends data values to a msg []byte channel. |
| 36 | +func (rq *RedisQueue) Subscribe(queueID, groupID, consumerID string, msg chan []byte) error { |
| 37 | + // Create Subscription |
| 38 | + |
| 39 | + // Read from stram (do in loop) |
| 40 | + // XREADGROUP GROUP queueGROUP ConsumerID COUNT 1 STREAMS queueID > |
| 41 | + |
| 42 | + // Acknowledge that message was processed |
| 43 | + // XACK queueID queueGROUP MSGID |
| 44 | + |
| 45 | + go func() { |
| 46 | + args := &redis.XReadGroupArgs{ |
| 47 | + Group: groupID, |
| 48 | + Consumer: consumerID, |
| 49 | + Streams: []string{queueID}, |
| 50 | + Count: 1, |
| 51 | + Block: 0, |
| 52 | + NoAck: false, |
| 53 | + } |
| 54 | + |
| 55 | + for { |
| 56 | + xstreams, err := rq.client.XReadGroup(args).Result() |
| 57 | + if err != nil { |
| 58 | + // handle error, prob by logging |
| 59 | + } |
| 60 | + xstream := xstreams[0] // only asking for one stream |
| 61 | + message := xstream.Messages[0] // asking for one message |
| 62 | + msgID := message.ID // retrieve message ID |
| 63 | + value, err := getBytes(message.Values["value"]) // retrieve value using defaultKey, transform to bytes |
| 64 | + if err != nil { |
| 65 | + // log gob decoding error |
| 66 | + } |
| 67 | + |
| 68 | + msg <- value // send the value |
| 69 | + |
| 70 | + // Ack the read |
| 71 | + _, err = rq.client.XAck(queueID, groupID, msgID).Result() |
| 72 | + if err != nil { |
| 73 | + // log ack error |
| 74 | + } |
| 75 | + } |
| 76 | + }() |
| 77 | + |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +// Publish value to a queue, based on queueID. |
| 82 | +func (rq *RedisQueue) Publish(queueID string, value []byte) error { |
| 83 | + // XADD queueID * field value |
| 84 | + var m map[string]interface{} |
| 85 | + m["value"] = value |
| 86 | + args := &redis.XAddArgs{ |
| 87 | + Stream: queueID, |
| 88 | + MaxLen: 1, // MAXLEN N |
| 89 | + MaxLenApprox: 1, // MAXLEN ~ N |
| 90 | + ID: "*", |
| 91 | + Values: m, |
| 92 | + } |
| 93 | + |
| 94 | + err := rq.client.XAdd(args).Err() |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +func getBytes(data interface{}) ([]byte, error) { |
| 102 | + var buf bytes.Buffer |
| 103 | + enc := gob.NewEncoder(&buf) |
| 104 | + err := enc.Encode(data) |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + return buf.Bytes(), nil |
| 109 | +} |
0 commit comments