https://github.com/corverroos/rredis
Reflex stream client for redis streams
https://github.com/corverroos/rredis
go golang golang-library golang-package redis reflex stream
Last synced: 8 months ago
JSON representation
Reflex stream client for redis streams
- Host: GitHub
- URL: https://github.com/corverroos/rredis
- Owner: corverroos
- Created: 2021-09-29T11:16:21.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2021-10-05T06:56:15.000Z (about 4 years ago)
- Last Synced: 2025-01-03T16:39:35.167Z (9 months ago)
- Topics: go, golang, golang-library, golang-package, redis, reflex, stream
- Language: Go
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# rredis
A [reflex](https://github.com/luno/reflex) stream client for a [redis streams](https://redis.io/topics/streams-intro)
using the [radix](https://github.com/mediocregopher/radix) client implementation.It provides an API for inserting data into a stream and for consuming data from a stream with at-least-once semantics.
It also provides a reflex.CursorStore implementation for storing cursors in redis.### Usage
```
// Define your consumer business logic
fn := func(ctx context.Context, fate fate.Fate, e *reflex.Event) error {
fmt.Print("Consuming redis stream event", e)
return fate.Tempt() // Fate injects application errors at runtime, enforcing idempotent logic.
}// Define some more variables
var namespace, streamName, redisAddr, consumerName string
var ctx context.Context// Connect to redis
con, _ := radix.Dial(ctx, "tcp", redisAddr)// Setup rredis and reflex
stream := rredis.NewStream(con, namespace, stream)
cstore := rredis.NewCursorStore(con, namespace)consumer := reflex.NewConsumer(consumerName, fn)
spec := reflex.NewSpec(stream.Stream, cstore, consumer)// Insert some data concurrently
go func() {
for {
_ = stream.Insert(ctx, []byte(fmt.Sprint(time.Now())))
time.Sleep(time.Second)
}
}()// Stream forever!
// Progress is stored in the cursor store, so restarts or any error continue where it left off.
for {
err := reflex.Run(context.Backend(), spec)
if err != nil { // Note Run always returns non-nil error
log.Printf("stream error: %v", err)
}
}
```## Notes
- Since reflex events have specific fields (type, foreignID, timestamp, data) the redis stream entries need to adhere to a specific format. It is therefore advised to use Stream.Insert to ensure the correct format.
- At-least-once semantics are also provided by redis consumer groups, but it doesn't provide strict ordering. rredis maintains strict ordering and can provide sharding using reflex/rpatterns.Parralel.