forked from robinjoseph08/redisqueue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis.go
More file actions
52 lines (44 loc) · 1.49 KB
/
Copy pathredis.go
File metadata and controls
52 lines (44 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package redisqueue
import (
"context"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/redis/go-redis/v9"
)
var redisVersionRE = regexp.MustCompile(`redis_version:(.+)`)
// RedisOptions is an alias to redis.Options so that users can this instead of
// having to import go-redis directly.
type RedisOptions = redis.Options
// newRedisClient creates a new Redis client with the given options. If options
// is nil, it will use default options.
func newRedisClient(options *RedisOptions) *redis.Client {
if options == nil {
options = &RedisOptions{}
}
return redis.NewClient(options)
}
// redisPreflightChecks makes sure the Redis instance backing the *redis.Client
// offers the functionality we need. Specifically, it also that it can connect
// to the actual instance and that the instance supports Redis streams (i.e.
// it's at least v5).
func redisPreflightChecks(client redis.UniversalClient) error {
_, err := client.Ping(context.TODO()).Result()
if err != nil {
return err
}
return nil
}
// incrementMessageID takes in a message ID (e.g. 1564886140363-0) and
// increments the index section (e.g. 1564886140363-1). This is the next valid
// ID value, and it can be used for paging through messages.
func incrementMessageID(id string) (string, error) {
parts := strings.Split(id, "-")
index := parts[1]
parsed, err := strconv.ParseInt(index, 10, 64)
if err != nil {
return "", fmt.Errorf("error parsing message ID %q: %w", id, err)
}
return fmt.Sprintf("%s-%d", parts[0], parsed+1), nil
}