Skip to content

Commit

Permalink
backend: retry connections when connecting to redis and mysql (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
chee-zaram authored Oct 21, 2023
1 parent dcad94f commit ef7b4f8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
1 change: 1 addition & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
27 changes: 16 additions & 11 deletions backend/internal/api/v1/middlewares/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
15 changes: 14 additions & 1 deletion backend/internal/db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit ef7b4f8

Please sign in to comment.