mirror of
https://github.com/grassrootseconomics/cic-custodial.git
synced 2024-11-21 13:56:47 +01:00
fix: system global lock
* add middleware to entire API group * setNX system lock key
This commit is contained in:
parent
ee907dddbc
commit
5679a675f3
@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
@ -11,7 +10,6 @@ import (
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -49,9 +47,7 @@ func initApiServer(custodialContainer *custodial.Custodial) *echo.Echo {
|
||||
})
|
||||
}
|
||||
|
||||
apiRoute := server.Group("/api")
|
||||
|
||||
apiRoute.Use(systemGlobalLock)
|
||||
apiRoute := server.Group("/api", systemGlobalLock)
|
||||
|
||||
apiRoute.POST("/account/create", api.HandleAccountCreate)
|
||||
apiRoute.POST("/sign/transfer", api.HandleSignTransfer)
|
||||
@ -95,12 +91,12 @@ func systemGlobalLock(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
)
|
||||
|
||||
locked, err := cu.RedisClient.Get(c.Request().Context(), systemGlobalLockKey).Bool()
|
||||
if !errors.Is(err, redis.Nil) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if locked {
|
||||
return c.JSON(http.StatusOK, api.ErrResp{
|
||||
return c.JSON(http.StatusServiceUnavailable, api.ErrResp{
|
||||
Ok: false,
|
||||
Message: "System manually locked.",
|
||||
})
|
||||
|
@ -7,14 +7,20 @@ import (
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type RedisPoolOpts struct {
|
||||
const (
|
||||
systemGlobalLockKey = "system:global_lock"
|
||||
)
|
||||
|
||||
type (
|
||||
RedisPoolOpts struct {
|
||||
DSN string
|
||||
MinIdleConns int
|
||||
}
|
||||
}
|
||||
|
||||
type RedisPool struct {
|
||||
RedisPool struct {
|
||||
Client *redis.Client
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
// NewRedisPool creates a reusable connection across the cic-custodial componenent.
|
||||
// Note: Each db namespace requires its own connection pool.
|
||||
@ -31,8 +37,7 @@ func NewRedisPool(ctx context.Context, o RedisPoolOpts) (*RedisPool, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err = redisClient.Ping(ctx).Result()
|
||||
if err != nil {
|
||||
if err := redisClient.SetNX(ctx, systemGlobalLockKey, false, 0).Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user