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 <evgenii@nspcc.io>
  • Loading branch information
smallhive committed Oct 23, 2024
1 parent 23424c7 commit 335d18a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 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())*ni.EpochDuration()*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,

Check warning on line 79 in handlers/api.go

View check run for this annotation

Codecov / codecov/patch

handlers/api.go#L79

Added line #L79 was not covered by tests
}, 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
15 changes: 12 additions & 3 deletions handlers/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ 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)

Check warning on line 70 in handlers/containers.go

View check run for this annotation

Codecov / codecov/patch

handlers/containers.go#L67-L70

Added lines #L67 - L70 were not covered by tests
if err != nil {
resp := a.logAndGetErrorResponse("create container", err)
return ctx.JSON(http.StatusBadRequest, resp)
Expand Down Expand Up @@ -136,7 +139,10 @@ 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 {

Check warning on line 145 in handlers/containers.go

View check run for this annotation

Codecov / codecov/patch

handlers/containers.go#L142-L145

Added lines #L142 - L145 were not covered by tests
resp := a.logAndGetErrorResponse("failed set container eacl", err)
return ctx.JSON(http.StatusBadRequest, resp)
}
Expand Down Expand Up @@ -255,7 +261,10 @@ 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 {

Check warning on line 267 in handlers/containers.go

View check run for this annotation

Codecov / codecov/patch

handlers/containers.go#L264-L267

Added lines #L264 - L267 were not covered by tests
resp := a.logAndGetErrorResponse("delete container", err, zap.String("container", containerID))
return ctx.JSON(http.StatusBadRequest, resp)
}
Expand Down

0 comments on commit 335d18a

Please sign in to comment.