Skip to content

Commit

Permalink
Update rdb pkg structure (#1529)
Browse files Browse the repository at this point in the history
* feat: update rdb init pkg

* fix: add logs based on feedback

* feat: close connection on ping failure
  • Loading branch information
nick-bisonai authored May 29, 2024
1 parent 29ebbe0 commit c5b14f2
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions node/pkg/db/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

errorSentinel "bisonai.com/orakl/node/pkg/error"
"bisonai.com/orakl/node/pkg/utils/retrier"
"github.com/redis/go-redis/v9"
"github.com/rs/zerolog/log"
)
Expand All @@ -21,27 +22,42 @@ type RedisConnectionInfo struct {
}

var (
initRdbOnce sync.Once
rdb *redis.Conn
rdbErr error
rdbMutex sync.Mutex
rdb *redis.Conn
)

func GetRedisConn(ctx context.Context) (*redis.Conn, error) {
return getRedisConn(ctx, &initRdbOnce)
conn, err := getRedisConn(ctx)
if err != nil {
return nil, err
}
return conn, err
}

func getRedisConn(ctx context.Context, once *sync.Once) (*redis.Conn, error) {
func getRedisConn(ctx context.Context) (*redis.Conn, error) {
rdbMutex.Lock()
defer rdbMutex.Unlock()

once.Do(func() {
connectionInfo, err := loadRedisConnectionString()
if rdb != nil {
log.Debug().Msg("Attempting to ping Redis")
_, err := rdb.Ping(ctx).Result()
if err == nil {
return rdb, nil
}
_ = rdb.Close()
}

reconnectJob := func() error {
err := reconnectRedis(ctx)
if err != nil {
rdbErr = err
return
return err
}
return nil
}

rdb, rdbErr = connectToRedis(ctx, connectionInfo)
})
return rdb, rdbErr
log.Debug().Msg("Reconnecting to Redis")
err := retrier.Retry(reconnectJob, 3, 500*time.Millisecond, 2*time.Second)
return rdb, err
}

func MSet(ctx context.Context, values map[string]string) error {
Expand Down Expand Up @@ -295,6 +311,21 @@ func loadRedisConnectionString() (RedisConnectionInfo, error) {
return RedisConnectionInfo{Host: host, Port: port}, nil
}

func reconnectRedis(ctx context.Context) error {
connectionInfo, err := loadRedisConnectionString()
if err != nil {
log.Error().Err(err).Msg("Failed to load Redis connection string")
return err
}

rdb, err = connectToRedis(ctx, connectionInfo)
if err != nil {
log.Error().Err(err).Msg("Failed to connect to Redis")
return err
}
return nil
}

func setRedis(ctx context.Context, rdb *redis.Conn, key string, value string, exp time.Duration) error {
return rdb.Set(ctx, key, value, exp).Err()
}
Expand Down

0 comments on commit c5b14f2

Please sign in to comment.