Merge pull request #128 from agarwal-nitesh/feat/redis_cluster_client

Add redis cluster client as a session store.
This commit is contained in:
Lakhan Samani 2022-02-18 09:19:24 +05:30 committed by GitHub
commit ce9a115a14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 4 deletions

3
.gitignore vendored
View File

@ -10,4 +10,5 @@ build
data.db data.db
.DS_Store .DS_Store
.env.local .env.local
*.tar.gz *.tar.gz
.vscode/

View File

@ -4,13 +4,11 @@ import (
"context" "context"
"fmt" "fmt"
"log" "log"
"github.com/go-redis/redis/v8"
) )
type RedisStore struct { type RedisStore struct {
ctx context.Context ctx context.Context
store *redis.Client store RedisSessionClient
} }
// AddUserSession adds the user session to redis // AddUserSession adds the user session to redis

View File

@ -3,6 +3,7 @@ package sessionstore
import ( import (
"context" "context"
"log" "log"
"strings"
"github.com/authorizerdev/authorizer/server/constants" "github.com/authorizerdev/authorizer/server/constants"
"github.com/authorizerdev/authorizer/server/envstore" "github.com/authorizerdev/authorizer/server/envstore"
@ -121,6 +122,23 @@ func RemoveSocialLoginState(key string) {
func InitSession() { func InitSession() {
if envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyRedisURL) != "" { if envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyRedisURL) != "" {
log.Println("using redis store to save sessions") log.Println("using redis store to save sessions")
if isCluster(envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyRedisURL)) {
clusterOpt, err := getClusterOptions(envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyRedisURL))
if err != nil {
log.Fatalln("Error parsing redis url:", err)
}
rdb := redis.NewClusterClient(clusterOpt)
ctx := context.Background()
_, err = rdb.Ping(ctx).Result()
if err != nil {
log.Fatalln("Error connecting to redis cluster server", err)
}
SessionStoreObj.RedisMemoryStoreObj = &RedisStore{
ctx: ctx,
store: rdb,
}
return
}
opt, err := redis.ParseURL(envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyRedisURL)) opt, err := redis.ParseURL(envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyRedisURL))
if err != nil { if err != nil {
log.Fatalln("Error parsing redis url:", err) log.Fatalln("Error parsing redis url:", err)
@ -144,3 +162,19 @@ func InitSession() {
} }
} }
} }
func isCluster(url string) bool {
return len(strings.Split(url, ",")) > 1
}
func getClusterOptions(url string) (*redis.ClusterOptions, error) {
hostPortsList := strings.Split(url, ",")
opt, err := redis.ParseURL(hostPortsList[0])
if err != nil {
return nil, err
}
urls := []string{opt.Addr}
urlList := hostPortsList[1:]
urls = append(urls, urlList...)
return &redis.ClusterOptions{Addrs: urls}, nil
}

View File

@ -0,0 +1,18 @@
package sessionstore
import (
"context"
"time"
"github.com/go-redis/redis/v8"
)
type RedisSessionClient interface {
HMSet(ctx context.Context, key string, values ...interface{}) *redis.BoolCmd
Del(ctx context.Context, keys ...string) *redis.IntCmd
HDel(ctx context.Context, key string, fields ...string) *redis.IntCmd
HMGet(ctx context.Context, key string, fields ...string) *redis.SliceCmd
HGetAll(ctx context.Context, key string) *redis.StringStringMapCmd
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd
Get(ctx context.Context, key string) *redis.StringCmd
}