mirror of
https://github.com/grassrootseconomics/cic-custodial.git
synced 2024-11-21 22:06: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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -11,7 +10,6 @@ import (
|
|||||||
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
|
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/labstack/echo/v4/middleware"
|
"github.com/labstack/echo/v4/middleware"
|
||||||
"github.com/redis/go-redis/v9"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -49,9 +47,7 @@ func initApiServer(custodialContainer *custodial.Custodial) *echo.Echo {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
apiRoute := server.Group("/api")
|
apiRoute := server.Group("/api", systemGlobalLock)
|
||||||
|
|
||||||
apiRoute.Use(systemGlobalLock)
|
|
||||||
|
|
||||||
apiRoute.POST("/account/create", api.HandleAccountCreate)
|
apiRoute.POST("/account/create", api.HandleAccountCreate)
|
||||||
apiRoute.POST("/sign/transfer", api.HandleSignTransfer)
|
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()
|
locked, err := cu.RedisClient.Get(c.Request().Context(), systemGlobalLockKey).Bool()
|
||||||
if !errors.Is(err, redis.Nil) {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if locked {
|
if locked {
|
||||||
return c.JSON(http.StatusOK, api.ErrResp{
|
return c.JSON(http.StatusServiceUnavailable, api.ErrResp{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "System manually locked.",
|
Message: "System manually locked.",
|
||||||
})
|
})
|
||||||
|
@ -7,14 +7,20 @@ import (
|
|||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RedisPoolOpts struct {
|
const (
|
||||||
|
systemGlobalLockKey = "system:global_lock"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
RedisPoolOpts struct {
|
||||||
DSN string
|
DSN string
|
||||||
MinIdleConns int
|
MinIdleConns int
|
||||||
}
|
}
|
||||||
|
|
||||||
type RedisPool struct {
|
RedisPool struct {
|
||||||
Client *redis.Client
|
Client *redis.Client
|
||||||
}
|
}
|
||||||
|
)
|
||||||
|
|
||||||
// NewRedisPool creates a reusable connection across the cic-custodial componenent.
|
// NewRedisPool creates a reusable connection across the cic-custodial componenent.
|
||||||
// Note: Each db namespace requires its own connection pool.
|
// 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)
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
_, err = redisClient.Ping(ctx).Result()
|
if err := redisClient.SetNX(ctx, systemGlobalLockKey, false, 0).Err(); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user