-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathstore.go
131 lines (112 loc) · 2.89 KB
/
store.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package store
import (
"context"
"fmt"
"log/slog"
"net"
"net/url"
"strconv"
"time"
"github.com/ulule/gokvstores"
)
type Store gokvstores.KVStore
const (
cacheKVStoreType = "cache"
dummyKVStoreType = "dummy"
redisClusterKVStoreType = "redis-cluster"
redisKVStoreType = "redis"
redisRoundRobinType = "redis-roundrobin"
)
func parseRedisURL(redisURL string) (*gokvstores.RedisClientOptions, error) {
opts := &gokvstores.RedisClientOptions{}
u, err := url.Parse(redisURL)
if err != nil {
return nil, err
}
h, p := getHostPortWithDefaults(u)
opts.Addr = net.JoinHostPort(h, p)
q := u.Query()
opts.Password = q.Get("password")
rawdb := q.Get("db")
if rawdb != "" {
db, err := strconv.Atoi(rawdb)
if err != nil {
return nil, err
}
opts.DB = db
} else {
opts.DB = 0
}
return opts, nil
}
func getHostPortWithDefaults(u *url.URL) (string, string) {
host, port, err := net.SplitHostPort(u.Host)
if err != nil {
host = u.Host
}
if host == "" {
host = "localhost"
}
if port == "" {
port = "6379"
}
return host, port
}
// New returns a KVStore from config
func New(ctx context.Context, log *slog.Logger, cfg *Config) (gokvstores.KVStore, error) {
if cfg == nil {
return gokvstores.DummyStore{}, nil
}
log.InfoContext(ctx, "KVStore configured",
slog.String("type", cfg.Type))
switch cfg.Type {
case dummyKVStoreType:
return gokvstores.DummyStore{}, nil
case redisRoundRobinType:
redis := cfg.RedisRoundRobin
kvstores := make([]gokvstores.KVStore, len(redis.Addrs))
for i := range redis.Addrs {
redisOptions, err := parseRedisURL(redis.Addrs[i])
if err != nil {
return nil, err
}
s, err := gokvstores.NewRedisClientStore(ctx, redisOptions, time.Duration(redis.Expiration)*time.Second)
if err != nil {
return nil, err
}
kvstores[i] = s
}
return &kvstoreWrapper{&redisRoundRobinStore{kvstores}, cfg.Prefix}, nil
case redisClusterKVStoreType:
redis := cfg.RedisCluster
s, err := gokvstores.NewRedisClusterStore(ctx, &gokvstores.RedisClusterOptions{
Addrs: redis.Addrs,
Password: redis.Password,
}, time.Duration(redis.Expiration)*time.Second)
if err != nil {
return nil, err
}
return &kvstoreWrapper{s, cfg.Prefix}, nil
case redisKVStoreType:
redis := cfg.Redis
s, err := gokvstores.NewRedisClientStore(ctx, &gokvstores.RedisClientOptions{
Addr: redis.Addr(),
DB: redis.DB,
Password: redis.Password,
}, time.Duration(redis.Expiration)*time.Second)
if err != nil {
return nil, err
}
return &kvstoreWrapper{s, cfg.Prefix}, nil
case cacheKVStoreType:
cache := cfg.Cache
s, err := gokvstores.NewMemoryStore(
time.Duration(cache.Expiration)*time.Second,
time.Duration(cache.CleanupInterval)*time.Second)
if err != nil {
return nil, err
}
return &kvstoreWrapper{s, cfg.Prefix}, nil
}
return nil, fmt.Errorf("kvstore %s does not exist", cfg.Type)
}