-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_redis.go
79 lines (72 loc) · 2.56 KB
/
config_redis.go
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package gostream
import (
"fmt"
"github.com/pkg/errors"
"time"
)
// RedisConfig is to hold configuration options of the Redis data store
type RedisConfig struct {
Addrs []string // The addresses of Redis instances host:port.
Password string // The password to access server
DialTimeout time.Duration // DialTimeout is the timeout for establishing connection with Redis server
ConnRetryInterval time.Duration // ConnRetryInterval is an interval between connection retries
Namespace string // Namespace is the namespace for the project
}
// Validate is to validate Redis configuration
func (r *RedisConfig) Validate() error {
if len(r.Addrs) == 0 {
return errors.New("You need to specify at least one Redis instance address to connect")
}
if r.DialTimeout == 0 {
return errors.New("DialTimeout can not be zero")
}
if r.ConnRetryInterval == 0 {
return errors.New("ConnRetryInterval can not be zero")
}
if r.Namespace == "" {
return errors.New("Namespace is empty.")
}
return nil
}
// buildIDKey is to created key including namespace, prefix and ID
func (r *RedisConfig) BuildIDKey(prefix, id string) string {
return fmt.Sprintf("%s::%s::%s", r.Namespace, prefix, id)
}
// buildKey is to create key comprising of namespace and key
func (r *RedisConfig) BuildKey(key string) string {
return fmt.Sprintf("%s::%s", r.Namespace, key)
}
// RedisStreamConfig is the configuration of the data stream based on Redis
type RedisStreamConfig struct {
RedisConfig
MaxLength int64 // the maximum allowed length of the data stream. When reached the old messages will be evicted automatically.
MaxPendingLength int64 // the maximal number of pending messages allowed per group/consumer
ConsumerID string // the name of data consumer
GroupID string // the name of group of data consumers
StreamName string // the data stream name
}
// Validate is to validate Redis configuration
func (r *RedisStreamConfig) Validate() error {
if err := r.RedisConfig.Validate(); err != nil {
return err
}
if r.MaxLength == 0 {
return errors.New("MaxLength can not be zero")
}
if r.MaxPendingLength == 0 {
return errors.New("MaxPendingLength can not be zero")
}
if r.MaxPendingLength > r.MaxLength {
return errors.New("MaxPendingLength can not be greater than MaxLength")
}
if r.ConsumerID == "" {
return errors.New("ConsumerID is not specified")
}
if r.GroupID == "" {
return errors.New("GroupID is not specified")
}
if r.StreamName == "" {
return errors.New("StreamName is not specified")
}
return nil
}