41 lines
1.2 KiB
Go
41 lines
1.2 KiB
Go
package redisstate
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// StreamOffset stores the persisted progress of one plain-XREAD consumer.
|
|
type StreamOffset struct {
|
|
// Stream stores the Redis Stream key the offset belongs to.
|
|
Stream string `json:"stream"`
|
|
|
|
// LastProcessedEntryID stores the most recently processed Stream entry id.
|
|
LastProcessedEntryID string `json:"last_processed_entry_id"`
|
|
|
|
// UpdatedAt stores when the offset was last persisted.
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
// MarshalStreamOffset returns the JSON encoding of the persisted offset.
|
|
func MarshalStreamOffset(offset StreamOffset) ([]byte, error) {
|
|
payload, err := json.Marshal(offset)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("marshal stream offset: %w", err)
|
|
}
|
|
return payload, nil
|
|
}
|
|
|
|
// UnmarshalStreamOffset parses one persisted offset payload.
|
|
func UnmarshalStreamOffset(payload []byte) (StreamOffset, error) {
|
|
var offset StreamOffset
|
|
if err := json.Unmarshal(payload, &offset); err != nil {
|
|
return StreamOffset{}, fmt.Errorf("unmarshal stream offset: %w", err)
|
|
}
|
|
if offset.UpdatedAt.IsZero() {
|
|
return StreamOffset{}, fmt.Errorf("unmarshal stream offset: updated_at must not be zero")
|
|
}
|
|
return offset, nil
|
|
}
|