diff --git a/backend/go.mod b/backend/go.mod index 857dd1c..284eab4 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -3,6 +3,7 @@ module github.com/wigit-ng/webapp/backend go 1.20 require ( + github.com/cenkalti/backoff v2.2.1+incompatible github.com/cristalhq/jwt/v5 v5.1.0 github.com/gin-contrib/cors v1.4.0 github.com/gin-gonic/gin v1.9.1 diff --git a/backend/go.sum b/backend/go.sum index 7e72cfb..e87fbbf 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -5,6 +5,8 @@ github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s= github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= +github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= +github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= diff --git a/backend/internal/api/v1/middlewares/redis.go b/backend/internal/api/v1/middlewares/redis.go index d769c9d..db1c52c 100644 --- a/backend/internal/api/v1/middlewares/redis.go +++ b/backend/internal/api/v1/middlewares/redis.go @@ -6,6 +6,7 @@ import ( "net/http" "strconv" + "github.com/cenkalti/backoff" "github.com/gin-gonic/gin" "github.com/redis/go-redis/v9" "github.com/wigit-ng/webapp/backend/internal/config" @@ -25,20 +26,24 @@ func NewRedis(cnf config.Config) error { return err } - client := redis.NewClient( - &redis.Options{ - Addr: fmt.Sprintf("%s:%s", cnf.RedisHost, cnf.RedisPort), - Password: cnf.RedisPass, - DB: db, - }, - ) + operation := func() error { + client := redis.NewClient( + &redis.Options{ + Addr: fmt.Sprintf("%s:%s", cnf.RedisHost, cnf.RedisPort), + Password: cnf.RedisPass, + DB: db, + }, + ) - if client == nil { - return fmt.Errorf("failed to initialize redis client") + if client == nil { + return fmt.Errorf("failed to initialize redis client") + } + + RedisClient = client + return nil } - RedisClient = client - return nil + return backoff.Retry(operation, backoff.NewExponentialBackOff()) } // Redis is a Gin middleware that checks if the requested URL is diff --git a/backend/internal/db/database.go b/backend/internal/db/database.go index 108fc88..185bf74 100644 --- a/backend/internal/db/database.go +++ b/backend/internal/db/database.go @@ -4,6 +4,7 @@ import ( "fmt" // _ "time/tzdata" if we make use of select time zone we may have to uncomment this and move to main + "github.com/cenkalti/backoff" "github.com/rs/zerolog/log" "github.com/wigit-ng/webapp/backend/internal/config" "github.com/wigit-ng/webapp/backend/internal/logger" @@ -90,7 +91,19 @@ func createDBConnection(dsn string) (*gorm.DB, error) { // GetConnector returns a ready connector to the database. func GetConnector(conf config.Config) *DB { - dbConnector, err := NewDB(NewDatabaseDSN(conf)) + var dbConnector *DB + var err error + + operation := func() error { + dbConnector, err = NewDB(NewDatabaseDSN(conf)) + if err != nil { + return err + } + + return nil + } + + err = backoff.Retry(operation, backoff.NewExponentialBackOff()) if err != nil { log.Panic().Err(err).Msg("failed to get db connector") }