Skip to content

Commit

Permalink
*: Add timeout for SDK waiter operations
Browse files Browse the repository at this point in the history
Closes #246.

Signed-off-by: Evgenii Baidakov <[email protected]>
  • Loading branch information
smallhive committed Oct 23, 2024
1 parent 23424c7 commit c183286
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
1 change: 1 addition & 0 deletions cmd/neofs-rest-gw/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ func newNeofsAPI(ctx context.Context, logger *zap.Logger, v *viper.Viper) (*hand
}

apiPrm.DefaultTimestamp = v.GetBool(cfgPoolDefaultTimestamp)
apiPrm.WaiterOperationTimeout = time.Duration(uint64(ni.MsPerBlock())*4) * time.Millisecond

return handlers.NewAPI(&apiPrm)
}
Expand Down
3 changes: 3 additions & 0 deletions handlers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type PrmAPI struct {
PrometheusService *metrics.Service
PprofService *metrics.Service
ServiceShutdownTimeout time.Duration
WaiterOperationTimeout time.Duration
}

type BearerToken struct {
Expand Down Expand Up @@ -75,6 +76,7 @@ func NewAPI(prm *PrmAPI) (*RestAPI, error) {
pprofService: prm.PprofService,
gateMetric: prm.GateMetric,
serviceShutdownTimeout: prm.ServiceShutdownTimeout,
waiterOperationTimeout: prm.WaiterOperationTimeout,
}, nil
}

Expand Down Expand Up @@ -141,6 +143,7 @@ type RestAPI struct {
prometheusService *metrics.Service
pprofService *metrics.Service
serviceShutdownTimeout time.Duration
waiterOperationTimeout time.Duration
}

func (a *RestAPI) StartCallback() {
Expand Down
21 changes: 15 additions & 6 deletions handlers/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ func (a *RestAPI) PutContainer(ctx echo.Context, params apiserver.PutContainerPa
return ctx.JSON(http.StatusBadRequest, resp)
}

cnrID, err := createContainer(ctx.Request().Context(), a.pool, stoken, body, params, a.signer)
wCtx, cancel := context.WithTimeout(ctx.Request().Context(), a.waiterOperationTimeout)
defer cancel()

cnrID, err := createContainer(wCtx, a.pool, stoken, body, params, a.signer)
if err != nil {
resp := a.logAndGetErrorResponse("create container", err)
return ctx.JSON(http.StatusBadRequest, resp)
return ctx.JSON(getResponseCodeFromStatus(err), resp)
}

resp := apiserver.PutContainerOK{
Expand Down Expand Up @@ -136,9 +139,12 @@ func (a *RestAPI) PutContainerEACL(ctx echo.Context, containerID apiserver.Conta
return ctx.JSON(http.StatusBadRequest, resp)
}

if err = setContainerEACL(ctx.Request().Context(), a.pool, cnrID, stoken, body, a.signer); err != nil {
wCtx, cancel := context.WithTimeout(ctx.Request().Context(), a.waiterOperationTimeout)
defer cancel()

if err = setContainerEACL(wCtx, a.pool, cnrID, stoken, body, a.signer); err != nil {
resp := a.logAndGetErrorResponse("failed set container eacl", err)
return ctx.JSON(http.StatusBadRequest, resp)
return ctx.JSON(getResponseCodeFromStatus(err), resp)
}

ctx.Response().Header().Set(accessControlAllowOriginHeader, "*")
Expand Down Expand Up @@ -255,9 +261,12 @@ func (a *RestAPI) DeleteContainer(ctx echo.Context, containerID apiserver.Contai
prm.WithinSession(stoken)

wait := waiter.NewContainerDeleteWaiter(a.pool, waiter.DefaultPollInterval)
if err = wait.ContainerDelete(ctx.Request().Context(), cnrID, a.signer, prm); err != nil {
wCtx, cancel := context.WithTimeout(ctx.Request().Context(), a.waiterOperationTimeout)
defer cancel()

if err = wait.ContainerDelete(wCtx, cnrID, a.signer, prm); err != nil {
resp := a.logAndGetErrorResponse("delete container", err, zap.String("container", containerID))
return ctx.JSON(http.StatusBadRequest, resp)
return ctx.JSON(getResponseCodeFromStatus(err), resp)
}

ctx.Response().Header().Set(accessControlAllowOriginHeader, "*")
Expand Down
50 changes: 50 additions & 0 deletions handlers/conv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package handlers

import (
"errors"
"net/http"

apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
)

func getResponseCodeFromStatus(err error) int {
if err == nil {
return http.StatusOK
}

switch {
case errors.Is(err, apistatus.ErrEACLNotFound):
return http.StatusNotFound
case errors.Is(err, apistatus.ErrContainerNotFound):
return http.StatusNotFound
case errors.Is(err, apistatus.ErrServerInternal):
return http.StatusInternalServerError
case errors.Is(err, apistatus.ErrSessionTokenNotFound):
return http.StatusNotFound
case errors.Is(err, apistatus.ErrSessionTokenExpired):
return http.StatusInternalServerError
case errors.Is(err, apistatus.ErrObjectLocked):
return http.StatusForbidden
case errors.Is(err, apistatus.ErrObjectAlreadyRemoved):
return http.StatusInternalServerError
case errors.Is(err, apistatus.ErrLockNonRegularObject):
return http.StatusInternalServerError
case errors.Is(err, apistatus.ErrObjectAccessDenied):
return http.StatusForbidden
case errors.Is(err, apistatus.ErrObjectNotFound):
return http.StatusNotFound
case errors.Is(err, apistatus.ErrObjectOutOfRange):
return http.StatusNotFound
case errors.Is(err, apistatus.ErrServerInternal):
return http.StatusInternalServerError
case errors.Is(err, apistatus.ErrWrongMagicNumber):
return http.StatusNotFound
case errors.Is(err, apistatus.ErrSignatureVerification):
return http.StatusForbidden
case errors.Is(err, apistatus.ErrNodeUnderMaintenance):
return http.StatusInternalServerError

default:
return http.StatusInternalServerError
}
}

0 comments on commit c183286

Please sign in to comment.