package redis
import (
"context"
"strconv"
"strings"
"time"
"github.com/redis/go-redis/v9/internal/otel"
)
const XTrimLimitDisabled = -1
func appendXTrimLimit(args []interface {}, limit int64 ) []interface {} {
switch {
case limit > 0 :
return append (args , "limit" , limit )
case limit < 0 :
return append (args , "limit" , int64 (0 ))
default :
return args
}
}
type StreamCmdable interface {
XAdd (ctx context .Context , a *XAddArgs ) *StringCmd
XAckDel (ctx context .Context , stream string , group string , mode string , ids ...string ) *SliceCmd
XDel (ctx context .Context , stream string , ids ...string ) *IntCmd
XDelEx (ctx context .Context , stream string , mode string , ids ...string ) *SliceCmd
XLen (ctx context .Context , stream string ) *IntCmd
XRange (ctx context .Context , stream, start, stop string ) *XMessageSliceCmd
XRangeN (ctx context .Context , stream, start, stop string , count int64 ) *XMessageSliceCmd
XRevRange (ctx context .Context , stream string , start, stop string ) *XMessageSliceCmd
XRevRangeN (ctx context .Context , stream string , start, stop string , count int64 ) *XMessageSliceCmd
XRead (ctx context .Context , a *XReadArgs ) *XStreamSliceCmd
XReadStreams (ctx context .Context , streams ...string ) *XStreamSliceCmd
XGroupCreate (ctx context .Context , stream, group, start string ) *StatusCmd
XGroupCreateMkStream (ctx context .Context , stream, group, start string ) *StatusCmd
XGroupSetID (ctx context .Context , stream, group, start string ) *StatusCmd
XGroupDestroy (ctx context .Context , stream, group string ) *IntCmd
XGroupCreateConsumer (ctx context .Context , stream, group, consumer string ) *IntCmd
XGroupDelConsumer (ctx context .Context , stream, group, consumer string ) *IntCmd
XReadGroup (ctx context .Context , a *XReadGroupArgs ) *XStreamSliceCmd
XAck (ctx context .Context , stream, group string , ids ...string ) *IntCmd
XNack (ctx context .Context , a *XNackArgs ) *IntCmd
XPending (ctx context .Context , stream, group string ) *XPendingCmd
XPendingExt (ctx context .Context , a *XPendingExtArgs ) *XPendingExtCmd
XClaim (ctx context .Context , a *XClaimArgs ) *XMessageSliceCmd
XClaimJustID (ctx context .Context , a *XClaimArgs ) *StringSliceCmd
XAutoClaim (ctx context .Context , a *XAutoClaimArgs ) *XAutoClaimCmd
XAutoClaimWithDeleted (ctx context .Context , a *XAutoClaimArgs ) *XAutoClaimWithDeletedCmd
XAutoClaimJustID (ctx context .Context , a *XAutoClaimArgs ) *XAutoClaimJustIDCmd
XTrimMaxLen (ctx context .Context , key string , maxLen int64 ) *IntCmd
XTrimMaxLenApprox (ctx context .Context , key string , maxLen, limit int64 ) *IntCmd
XTrimMaxLenMode (ctx context .Context , key string , maxLen int64 , mode string ) *IntCmd
XTrimMaxLenApproxMode (ctx context .Context , key string , maxLen, limit int64 , mode string ) *IntCmd
XTrimMinID (ctx context .Context , key string , minID string ) *IntCmd
XTrimMinIDApprox (ctx context .Context , key string , minID string , limit int64 ) *IntCmd
XTrimMinIDMode (ctx context .Context , key string , minID string , mode string ) *IntCmd
XTrimMinIDApproxMode (ctx context .Context , key string , minID string , limit int64 , mode string ) *IntCmd
XInfoGroups (ctx context .Context , key string ) *XInfoGroupsCmd
XInfoStream (ctx context .Context , key string ) *XInfoStreamCmd
XInfoStreamFull (ctx context .Context , key string , count int ) *XInfoStreamFullCmd
XInfoConsumers (ctx context .Context , key string , group string ) *XInfoConsumersCmd
XCfgSet (ctx context .Context , a *XCfgSetArgs ) *StatusCmd
}
type XAddArgs struct {
Stream string
NoMkStream bool
MaxLen int64
MinID string
Approx bool
Limit int64
Mode string
ID string
Values interface {}
ProducerID string
IdempotentID string
IdempotentAuto bool
}
func (c cmdable ) XAdd (ctx context .Context , a *XAddArgs ) *StringCmd {
args := make ([]interface {}, 0 , 15 )
args = append (args , "xadd" , a .Stream )
if a .NoMkStream {
args = append (args , "nomkstream" )
}
if a .Mode != "" {
args = append (args , a .Mode )
}
if a .ProducerID != "" {
if a .IdempotentAuto {
args = append (args , "idmpauto" , a .ProducerID )
} else if a .IdempotentID != "" {
args = append (args , "idmp" , a .ProducerID , a .IdempotentID )
}
}
switch {
case a .MaxLen > 0 :
if a .Approx {
args = append (args , "maxlen" , "~" , a .MaxLen )
} else {
args = append (args , "maxlen" , "=" , a .MaxLen )
}
case a .MinID != "" :
if a .Approx {
args = append (args , "minid" , "~" , a .MinID )
} else {
args = append (args , "minid" , "=" , a .MinID )
}
}
args = appendXTrimLimit (args , a .Limit )
if a .ID != "" {
args = append (args , a .ID )
} else {
args = append (args , "*" )
}
args = appendArg (args , a .Values )
cmd := NewStringCmd (ctx , args ...)
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XAckDel (ctx context .Context , stream string , group string , mode string , ids ...string ) *SliceCmd {
args := []interface {}{"xackdel" , stream , group , mode , "ids" , len (ids )}
for _ , id := range ids {
args = append (args , id )
}
cmd := NewSliceCmd (ctx , args ...)
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XDel (ctx context .Context , stream string , ids ...string ) *IntCmd {
args := []interface {}{"xdel" , stream }
for _ , id := range ids {
args = append (args , id )
}
cmd := NewIntCmd (ctx , args ...)
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XDelEx (ctx context .Context , stream string , mode string , ids ...string ) *SliceCmd {
args := []interface {}{"xdelex" , stream , mode , "ids" , len (ids )}
for _ , id := range ids {
args = append (args , id )
}
cmd := NewSliceCmd (ctx , args ...)
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XLen (ctx context .Context , stream string ) *IntCmd {
cmd := NewIntCmd (ctx , "xlen" , stream )
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XRange (ctx context .Context , stream , start , stop string ) *XMessageSliceCmd {
cmd := NewXMessageSliceCmd (ctx , "xrange" , stream , start , stop )
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XRangeN (ctx context .Context , stream , start , stop string , count int64 ) *XMessageSliceCmd {
cmd := NewXMessageSliceCmd (ctx , "xrange" , stream , start , stop , "count" , count )
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XRevRange (ctx context .Context , stream , start , stop string ) *XMessageSliceCmd {
cmd := NewXMessageSliceCmd (ctx , "xrevrange" , stream , start , stop )
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XRevRangeN (ctx context .Context , stream , start , stop string , count int64 ) *XMessageSliceCmd {
cmd := NewXMessageSliceCmd (ctx , "xrevrange" , stream , start , stop , "count" , count )
_ = c (ctx , cmd )
return cmd
}
type XReadArgs struct {
Streams []string
Count int64
MaxCount int64
MaxSize int64
Block time .Duration
ID string
}
func (c cmdable ) XRead (ctx context .Context , a *XReadArgs ) *XStreamSliceCmd {
args := make ([]interface {}, 0 , 2 *len (a .Streams )+10 )
args = append (args , "xread" )
keyPos := int8 (1 )
if a .Count > 0 {
args = append (args , "count" )
args = append (args , a .Count )
keyPos += 2
}
if a .MaxCount > 0 {
args = append (args , "maxcount" , a .MaxCount )
keyPos += 2
}
if a .MaxSize > 0 {
args = append (args , "maxsize" , a .MaxSize )
keyPos += 2
}
if a .Block >= 0 {
args = append (args , "block" )
args = append (args , int64 (a .Block /time .Millisecond ))
keyPos += 2
}
args = append (args , "streams" )
keyPos ++
for _ , s := range a .Streams {
args = append (args , s )
}
if a .ID != "" {
for range a .Streams {
args = append (args , a .ID )
}
}
cmd := NewXStreamSliceCmd (ctx , args ...)
if a .Block >= 0 {
cmd .setReadTimeout (a .Block )
}
cmd .SetFirstKeyPos (keyPos )
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XReadStreams (ctx context .Context , streams ...string ) *XStreamSliceCmd {
return c .XRead (ctx , &XReadArgs {
Streams : streams ,
Block : -1 ,
})
}
func (c cmdable ) XGroupCreate (ctx context .Context , stream , group , start string ) *StatusCmd {
cmd := NewStatusCmd (ctx , "xgroup" , "create" , stream , group , start )
cmd .SetFirstKeyPos (2 )
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XGroupCreateMkStream (ctx context .Context , stream , group , start string ) *StatusCmd {
cmd := NewStatusCmd (ctx , "xgroup" , "create" , stream , group , start , "mkstream" )
cmd .SetFirstKeyPos (2 )
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XGroupSetID (ctx context .Context , stream , group , start string ) *StatusCmd {
cmd := NewStatusCmd (ctx , "xgroup" , "setid" , stream , group , start )
cmd .SetFirstKeyPos (2 )
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XGroupDestroy (ctx context .Context , stream , group string ) *IntCmd {
cmd := NewIntCmd (ctx , "xgroup" , "destroy" , stream , group )
cmd .SetFirstKeyPos (2 )
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XGroupCreateConsumer (ctx context .Context , stream , group , consumer string ) *IntCmd {
cmd := NewIntCmd (ctx , "xgroup" , "createconsumer" , stream , group , consumer )
cmd .SetFirstKeyPos (2 )
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XGroupDelConsumer (ctx context .Context , stream , group , consumer string ) *IntCmd {
cmd := NewIntCmd (ctx , "xgroup" , "delconsumer" , stream , group , consumer )
cmd .SetFirstKeyPos (2 )
_ = c (ctx , cmd )
return cmd
}
type XReadGroupArgs struct {
Group string
Consumer string
Streams []string
Count int64
MaxCount int64
MaxSize int64
Block time .Duration
NoAck bool
Claim time .Duration
}
func (c cmdable ) XReadGroup (ctx context .Context , a *XReadGroupArgs ) *XStreamSliceCmd {
args := make ([]interface {}, 0 , 14 +len (a .Streams ))
args = append (args , "xreadgroup" , "group" , a .Group , a .Consumer )
keyPos := int8 (4 )
if a .Count > 0 {
args = append (args , "count" , a .Count )
keyPos += 2
}
if a .MaxCount > 0 {
args = append (args , "maxcount" , a .MaxCount )
keyPos += 2
}
if a .MaxSize > 0 {
args = append (args , "maxsize" , a .MaxSize )
keyPos += 2
}
if a .Block >= 0 {
args = append (args , "block" , int64 (a .Block /time .Millisecond ))
keyPos += 2
}
if a .NoAck {
args = append (args , "noack" )
keyPos ++
}
if a .Claim > 0 {
args = append (args , "claim" , int64 (a .Claim /time .Millisecond ))
keyPos += 2
}
args = append (args , "streams" )
keyPos ++
for _ , s := range a .Streams {
args = append (args , s )
}
cmd := NewXStreamSliceCmd (ctx , args ...)
if a .Block >= 0 {
cmd .setReadTimeout (a .Block )
}
cmd .SetFirstKeyPos (keyPos )
_ = c (ctx , cmd )
if otel .Enabled () && cmd .resultReady () && cmd .rawErr () == nil {
streams := cmd .Val ()
for _ , stream := range streams {
for _ , msg := range stream .Messages {
if parts := strings .SplitN (msg .ID , "-" , 2 ); len (parts ) == 2 {
if timestampMs , err := strconv .ParseInt (parts [0 ], 10 , 64 ); err == nil {
messageTime := time .Unix (0 , timestampMs *int64 (time .Millisecond ))
lag := time .Since (messageTime )
otel .RecordStreamLag (ctx , lag , nil , stream .Stream , a .Group , a .Consumer )
}
}
}
}
}
return cmd
}
func (c cmdable ) XAck (ctx context .Context , stream , group string , ids ...string ) *IntCmd {
args := []interface {}{"xack" , stream , group }
for _ , id := range ids {
args = append (args , id )
}
cmd := NewIntCmd (ctx , args ...)
_ = c (ctx , cmd )
return cmd
}
const (
XNackModeSilent = "SILENT"
XNackModeFail = "FAIL"
XNackModeFatal = "FATAL"
)
type XNackArgs struct {
Stream string
Group string
Mode string
IDs []string
RetryCount *uint64
Force bool
}
func (c cmdable ) XNack (ctx context .Context , a *XNackArgs ) *IntCmd {
args := make ([]interface {}, 0 , 9 +len (a .IDs ))
args = append (args , "xnack" , a .Stream , a .Group , a .Mode , "ids" , len (a .IDs ))
for _ , id := range a .IDs {
args = append (args , id )
}
if a .RetryCount != nil {
args = append (args , "retrycount" , *a .RetryCount )
}
if a .Force {
args = append (args , "force" )
}
cmd := NewIntCmd (ctx , args ...)
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XPending (ctx context .Context , stream , group string ) *XPendingCmd {
cmd := NewXPendingCmd (ctx , "xpending" , stream , group )
_ = c (ctx , cmd )
return cmd
}
type XPendingExtArgs struct {
Stream string
Group string
Idle time .Duration
Start string
End string
Count int64
Consumer string
}
func (c cmdable ) XPendingExt (ctx context .Context , a *XPendingExtArgs ) *XPendingExtCmd {
args := make ([]interface {}, 0 , 9 )
args = append (args , "xpending" , a .Stream , a .Group )
if a .Idle != 0 {
args = append (args , "idle" , formatMs (ctx , a .Idle ))
}
args = append (args , a .Start , a .End , a .Count )
if a .Consumer != "" {
args = append (args , a .Consumer )
}
cmd := NewXPendingExtCmd (ctx , args ...)
_ = c (ctx , cmd )
return cmd
}
type XAutoClaimArgs struct {
Stream string
Group string
MinIdle time .Duration
Start string
Count int64
Consumer string
}
func (c cmdable ) XAutoClaim (ctx context .Context , a *XAutoClaimArgs ) *XAutoClaimCmd {
args := xAutoClaimArgs (ctx , a )
cmd := NewXAutoClaimCmd (ctx , args ...)
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XAutoClaimWithDeleted (ctx context .Context , a *XAutoClaimArgs ) *XAutoClaimWithDeletedCmd {
args := xAutoClaimArgs (ctx , a )
cmd := NewXAutoClaimWithDeletedCmd (ctx , args ...)
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XAutoClaimJustID (ctx context .Context , a *XAutoClaimArgs ) *XAutoClaimJustIDCmd {
args := xAutoClaimArgs (ctx , a )
args = append (args , "justid" )
cmd := NewXAutoClaimJustIDCmd (ctx , args ...)
_ = c (ctx , cmd )
return cmd
}
func xAutoClaimArgs(ctx context .Context , a *XAutoClaimArgs ) []interface {} {
args := make ([]interface {}, 0 , 8 )
args = append (args , "xautoclaim" , a .Stream , a .Group , a .Consumer , formatMs (ctx , a .MinIdle ), a .Start )
if a .Count > 0 {
args = append (args , "count" , a .Count )
}
return args
}
type XClaimArgs struct {
Stream string
Group string
Consumer string
MinIdle time .Duration
Messages []string
}
func (c cmdable ) XClaim (ctx context .Context , a *XClaimArgs ) *XMessageSliceCmd {
args := xClaimArgs (a )
cmd := NewXMessageSliceCmd (ctx , args ...)
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XClaimJustID (ctx context .Context , a *XClaimArgs ) *StringSliceCmd {
args := xClaimArgs (a )
args = append (args , "justid" )
cmd := NewStringSliceCmd (ctx , args ...)
_ = c (ctx , cmd )
return cmd
}
func xClaimArgs(a *XClaimArgs ) []interface {} {
args := make ([]interface {}, 0 , 5 +len (a .Messages ))
args = append (args ,
"xclaim" ,
a .Stream ,
a .Group , a .Consumer ,
int64 (a .MinIdle /time .Millisecond ))
for _ , id := range a .Messages {
args = append (args , id )
}
return args
}
func (c cmdable ) xTrim (
ctx context .Context , key , strategy string ,
approx bool , threshold interface {}, limit int64 ,
) *IntCmd {
args := make ([]interface {}, 0 , 7 )
args = append (args , "xtrim" , key , strategy )
if approx {
args = append (args , "~" )
} else {
args = append (args , "=" )
}
args = append (args , threshold )
args = appendXTrimLimit (args , limit )
cmd := NewIntCmd (ctx , args ...)
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XTrimMaxLen (ctx context .Context , key string , maxLen int64 ) *IntCmd {
return c .xTrim (ctx , key , "maxlen" , false , maxLen , 0 )
}
func (c cmdable ) XTrimMaxLenApprox (ctx context .Context , key string , maxLen , limit int64 ) *IntCmd {
return c .xTrim (ctx , key , "maxlen" , true , maxLen , limit )
}
func (c cmdable ) XTrimMinID (ctx context .Context , key string , minID string ) *IntCmd {
return c .xTrim (ctx , key , "minid" , false , minID , 0 )
}
func (c cmdable ) XTrimMinIDApprox (ctx context .Context , key string , minID string , limit int64 ) *IntCmd {
return c .xTrim (ctx , key , "minid" , true , minID , limit )
}
func (c cmdable ) xTrimMode (
ctx context .Context , key , strategy string ,
approx bool , threshold interface {}, limit int64 ,
mode string ,
) *IntCmd {
args := make ([]interface {}, 0 , 7 )
args = append (args , "xtrim" , key , strategy )
if approx {
args = append (args , "~" )
} else {
args = append (args , "=" )
}
args = append (args , threshold )
args = appendXTrimLimit (args , limit )
args = append (args , mode )
cmd := NewIntCmd (ctx , args ...)
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XTrimMaxLenMode (ctx context .Context , key string , maxLen int64 , mode string ) *IntCmd {
return c .xTrimMode (ctx , key , "maxlen" , false , maxLen , 0 , mode )
}
func (c cmdable ) XTrimMaxLenApproxMode (ctx context .Context , key string , maxLen , limit int64 , mode string ) *IntCmd {
return c .xTrimMode (ctx , key , "maxlen" , true , maxLen , limit , mode )
}
func (c cmdable ) XTrimMinIDMode (ctx context .Context , key string , minID string , mode string ) *IntCmd {
return c .xTrimMode (ctx , key , "minid" , false , minID , 0 , mode )
}
func (c cmdable ) XTrimMinIDApproxMode (ctx context .Context , key string , minID string , limit int64 , mode string ) *IntCmd {
return c .xTrimMode (ctx , key , "minid" , true , minID , limit , mode )
}
func (c cmdable ) XInfoConsumers (ctx context .Context , key string , group string ) *XInfoConsumersCmd {
cmd := NewXInfoConsumersCmd (ctx , key , group )
cmd .SetFirstKeyPos (2 )
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XInfoGroups (ctx context .Context , key string ) *XInfoGroupsCmd {
cmd := NewXInfoGroupsCmd (ctx , key )
cmd .SetFirstKeyPos (2 )
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XInfoStream (ctx context .Context , key string ) *XInfoStreamCmd {
cmd := NewXInfoStreamCmd (ctx , key )
cmd .SetFirstKeyPos (2 )
_ = c (ctx , cmd )
return cmd
}
func (c cmdable ) XInfoStreamFull (ctx context .Context , key string , count int ) *XInfoStreamFullCmd {
args := make ([]interface {}, 0 , 6 )
args = append (args , "xinfo" , "stream" , key , "full" )
if count > 0 {
args = append (args , "count" , count )
}
cmd := NewXInfoStreamFullCmd (ctx , args ...)
cmd .SetFirstKeyPos (2 )
_ = c (ctx , cmd )
return cmd
}
type XCfgSetArgs struct {
Stream string
Duration int64
MaxSize int64
}
func (c cmdable ) XCfgSet (ctx context .Context , a *XCfgSetArgs ) *StatusCmd {
args := make ([]interface {}, 0 , 6 )
args = append (args , "xcfgset" , a .Stream )
if a .Duration > 0 {
args = append (args , "idmp-duration" , a .Duration )
}
if a .MaxSize > 0 {
args = append (args , "idmp-maxsize" , a .MaxSize )
}
cmd := NewStatusCmd (ctx , args ...)
_ = c (ctx , cmd )
return cmd
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .