Skip to content

Commit f2f4c72

Browse files
Add redis cluster client as a session store.
1 parent 9970eb1 commit f2f4c72

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ build
1010
data.db
1111
.DS_Store
1212
.env.local
13-
*.tar.gz
13+
*.tar.gz
14+
.vscode/

server/sessionstore/redis_store.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import (
44
"context"
55
"fmt"
66
"log"
7-
8-
"github.com/go-redis/redis/v8"
97
)
108

119
type RedisStore struct {
1210
ctx context.Context
13-
store *redis.Client
11+
store RedisSessionClient
1412
}
1513

1614
// AddUserSession adds the user session to redis

server/sessionstore/session.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sessionstore
33
import (
44
"context"
55
"log"
6+
"strings"
67

78
"github.com/authorizerdev/authorizer/server/constants"
89
"github.com/authorizerdev/authorizer/server/envstore"
@@ -121,6 +122,23 @@ func RemoveSocialLoginState(key string) {
121122
func InitSession() {
122123
if envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyRedisURL) != "" {
123124
log.Println("using redis store to save sessions")
125+
if isCluster(envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyRedisURL)) {
126+
clusterOpt, err := getClusterOptions(envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyRedisURL))
127+
if err != nil {
128+
log.Fatalln("Error parsing redis url:", err)
129+
}
130+
rdb := redis.NewClusterClient(clusterOpt)
131+
ctx := context.Background()
132+
_, err = rdb.Ping(ctx).Result()
133+
if err != nil {
134+
log.Fatalln("Error connecting to redis cluster server", err)
135+
}
136+
SessionStoreObj.RedisMemoryStoreObj = &RedisStore{
137+
ctx: ctx,
138+
store: rdb,
139+
}
140+
return
141+
}
124142
opt, err := redis.ParseURL(envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyRedisURL))
125143
if err != nil {
126144
log.Fatalln("Error parsing redis url:", err)
@@ -144,3 +162,19 @@ func InitSession() {
144162
}
145163
}
146164
}
165+
166+
func isCluster(url string) bool {
167+
return len(strings.Split(url, ",")) > 1
168+
}
169+
170+
func getClusterOptions(url string) (*redis.ClusterOptions, error) {
171+
hostPortsList := strings.Split(url, ",")
172+
opt, err := redis.ParseURL(hostPortsList[0])
173+
if err != nil {
174+
return nil, err
175+
}
176+
urls := []string{opt.Addr}
177+
urlList := hostPortsList[1:]
178+
urls = append(urls, urlList...)
179+
return &redis.ClusterOptions{Addrs: urls}, nil
180+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package sessionstore
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/go-redis/redis/v8"
8+
)
9+
10+
type RedisSessionClient interface {
11+
HMSet(ctx context.Context, key string, values ...interface{}) *redis.BoolCmd
12+
Del(ctx context.Context, keys ...string) *redis.IntCmd
13+
HDel(ctx context.Context, key string, fields ...string) *redis.IntCmd
14+
HMGet(ctx context.Context, key string, fields ...string) *redis.SliceCmd
15+
HGetAll(ctx context.Context, key string) *redis.StringStringMapCmd
16+
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd
17+
Get(ctx context.Context, key string) *redis.StringCmd
18+
}

0 commit comments

Comments
 (0)