|
1 | 1 | # redisqueue
|
2 |
| -A Golang queue worker using Redis streams |
| 2 | + |
| 3 | +[](https://godoc.org/github.com/robinjoseph08/redisqueue) |
| 4 | +[](https://travis-ci.org/robinjoseph08/redisqueue) |
| 5 | +[](https://coveralls.io/github/robinjoseph08/redisqueue?branch=master) |
| 6 | +[](https://goreportcard.com/report/github.com/robinjoseph08/redisqueue) |
| 7 | + |
| 8 | + |
| 9 | +`redisqueue` provides a producer and consumer of a queue that uses [Redis |
| 10 | +streams](https://redis.io/topics/streams-intro). |
| 11 | + |
| 12 | +## Features |
| 13 | + |
| 14 | +- A `Producer` struct to make enqueuing messages easy. |
| 15 | +- A `Consumer` struct to make processing messages concurrenly. |
| 16 | +- Claiming and acknowledging messages if there's no error, so that if a consumer |
| 17 | + dies while processing, the message it was working on isn't lost. This |
| 18 | + guarantees at least once delivery. |
| 19 | +- A "visibility timeout" so that if a message isn't processed in a designated |
| 20 | + time frame, it will be be processed by another consumer. |
| 21 | +- A max length on the stream so that it doesn't store the messages indefinitely |
| 22 | + and run out of memory. |
| 23 | +- Graceful handling of Unix signals (`SIGINT` and `SIGTERM`) to let in-flight |
| 24 | + messages complete. |
| 25 | +- A channel that will surface any errors so you can handle them centrally. |
| 26 | +- Graceful handling of panics to avoid crashing the whole process. |
| 27 | +- A concurrency setting to control how many goroutines are spawned to process |
| 28 | + messages. |
| 29 | +- A batch size setting to limit the total messages in flight. |
| 30 | +- Support for multiple streams. |
| 31 | + |
| 32 | +## Example |
| 33 | + |
| 34 | +Here's an example of a producer that inserts 1000 messages into a queue: |
| 35 | + |
| 36 | +```go |
| 37 | +package main |
| 38 | + |
| 39 | +import ( |
| 40 | + "fmt" |
| 41 | + |
| 42 | + "github.com/robinjoseph08/redisqueue" |
| 43 | +) |
| 44 | + |
| 45 | +func main() { |
| 46 | + p, err := redisqueue.NewProducerWithOptions(&redisqueue.ProducerOptions{ |
| 47 | + StreamMaxLength: 10000, |
| 48 | + ApproximateMaxLength: true, |
| 49 | + }) |
| 50 | + if err != nil { |
| 51 | + panic(err) |
| 52 | + } |
| 53 | + |
| 54 | + for i := 0; i < 1000; i++ { |
| 55 | + err := p.Enqueue(&redisqueue.Message{ |
| 56 | + Stream: "redisqueue:test", |
| 57 | + Values: map[string]interface{}{ |
| 58 | + "index": i, |
| 59 | + }, |
| 60 | + }) |
| 61 | + if err != nil { |
| 62 | + panic(err) |
| 63 | + } |
| 64 | + |
| 65 | + if i%100 == 0 { |
| 66 | + fmt.Printf("enqueued %d\n", i) |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +And here's an example of a consumer that reads the messages off of that queue: |
| 73 | + |
| 74 | +```go |
| 75 | +package main |
| 76 | + |
| 77 | +import ( |
| 78 | + "fmt" |
| 79 | + "time" |
| 80 | + |
| 81 | + "github.com/robinjoseph08/redisqueue" |
| 82 | +) |
| 83 | + |
| 84 | +func main() { |
| 85 | + c, err := redisqueue.NewConsumerWithOptions(&redisqueue.ConsumerOptions{ |
| 86 | + VisibilityTimeout: 60 * time.Second, |
| 87 | + BlockingTimeout: 5 * time.Second, |
| 88 | + ReclaimInterval: 1 * time.Second, |
| 89 | + BufferSize: 100, |
| 90 | + Concurrency: 10, |
| 91 | + }) |
| 92 | + if err != nil { |
| 93 | + panic(err) |
| 94 | + } |
| 95 | + |
| 96 | + c.Register("redisqueue:test", process) |
| 97 | + |
| 98 | + go func() { |
| 99 | + for err := range c.Errors { |
| 100 | + // handle errors accordingly |
| 101 | + fmt.Printf("err: %+v\n", err) |
| 102 | + } |
| 103 | + }() |
| 104 | + |
| 105 | + fmt.Println("starting") |
| 106 | + c.Run() |
| 107 | + fmt.Println("stopped") |
| 108 | +} |
| 109 | + |
| 110 | +func process(msg *redisqueue.Message) error { |
| 111 | + fmt.Printf("processing message: %v\n", msg.Values["index"]) |
| 112 | + return nil |
| 113 | +} |
| 114 | +``` |
0 commit comments