@@ -3,6 +3,7 @@ package sessionstore
33import (
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) {
121122func 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+ }
0 commit comments