Skip to content

Commit

Permalink
*: Cache network settings
Browse files Browse the repository at this point in the history
Closes #247.

Caching data for the half epoch duration time.

Signed-off-by: Evgenii Baidakov <evgenii@nspcc.io>
  • Loading branch information
smallhive committed Oct 22, 2024
1 parent 23424c7 commit 6a0bccb
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 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.EpochUpdateInterval = time.Duration(int64(ni.EpochDuration())/2*ni.MsPerBlock()) * time.Millisecond

return handlers.NewAPI(&apiPrm)
}
Expand Down
9 changes: 9 additions & 0 deletions handlers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"github.com/labstack/echo/v4"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neofs-rest-gw/handlers/apiserver"
"github.com/nspcc-dev/neofs-rest-gw/internal/cache"
"github.com/nspcc-dev/neofs-rest-gw/internal/util"
"github.com/nspcc-dev/neofs-rest-gw/metrics"
"github.com/nspcc-dev/neofs-sdk-go/netmap"
"github.com/nspcc-dev/neofs-sdk-go/pool"
"github.com/nspcc-dev/neofs-sdk-go/session"
"github.com/nspcc-dev/neofs-sdk-go/user"
Expand All @@ -32,6 +34,7 @@ type PrmAPI struct {
PrometheusService *metrics.Service
PprofService *metrics.Service
ServiceShutdownTimeout time.Duration
EpochUpdateInterval time.Duration
}

type BearerToken struct {
Expand All @@ -45,6 +48,10 @@ type SessionToken struct {
Verb session.ContainerVerb
}

type networkInfoGetter interface {
NetworkInfo(ctx context.Context) (netmap.NetworkInfo, error)
}

const (
// bearerCookieName is the name of the bearer cookie.
bearerCookieName = "Bearer"
Expand Down Expand Up @@ -75,6 +82,7 @@ func NewAPI(prm *PrmAPI) (*RestAPI, error) {
pprofService: prm.PprofService,
gateMetric: prm.GateMetric,
serviceShutdownTimeout: prm.ServiceShutdownTimeout,
networkInfoGetter: cache.NewNetworkInfoCache(prm.Pool, prm.EpochUpdateInterval),

Check warning on line 85 in handlers/api.go

View check run for this annotation

Codecov / codecov/patch

handlers/api.go#L85

Added line #L85 was not covered by tests
}, nil
}

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

func (a *RestAPI) StartCallback() {
Expand Down
13 changes: 11 additions & 2 deletions handlers/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ 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)
cnrID, err := createContainer(ctx.Request().Context(), a.pool, stoken, body, params, a.signer, a.networkInfoGetter)

Check warning on line 67 in handlers/containers.go

View check run for this annotation

Codecov / codecov/patch

handlers/containers.go#L67

Added line #L67 was not covered by tests
if err != nil {
resp := a.logAndGetErrorResponse("create container", err)
return ctx.JSON(http.StatusBadRequest, resp)
Expand Down Expand Up @@ -381,7 +381,7 @@ func getContainerEACL(ctx context.Context, p *pool.Pool, cnrID cid.ID) (*apiserv
return tableResp, nil
}

func createContainer(ctx context.Context, p *pool.Pool, stoken session.Container, request apiserver.ContainerPutInfo, params apiserver.PutContainerParams, signer user.Signer) (cid.ID, error) {
func createContainer(ctx context.Context, p *pool.Pool, stoken session.Container, request apiserver.ContainerPutInfo, params apiserver.PutContainerParams, signer user.Signer, networkInfoGetter networkInfoGetter) (cid.ID, error) {

Check warning on line 384 in handlers/containers.go

View check run for this annotation

Codecov / codecov/patch

handlers/containers.go#L384

Added line #L384 was not covered by tests
if request.PlacementPolicy == "" {
request.PlacementPolicy = defaultPlacementPolicy
}
Expand All @@ -406,6 +406,15 @@ func createContainer(ctx context.Context, p *pool.Pool, stoken session.Container
cnr.SetBasicACL(basicACL)
cnr.SetOwner(stoken.Issuer())

ni, err := networkInfoGetter.NetworkInfo(ctx)
if err != nil {
return cid.ID{}, fmt.Errorf("couldn't get network info: %w", err)
}

Check warning on line 412 in handlers/containers.go

View check run for this annotation

Codecov / codecov/patch

handlers/containers.go#L409-L412

Added lines #L409 - L412 were not covered by tests

if ni.HomomorphicHashingDisabled() {
cnr.DisableHomomorphicHashing()
}

Check warning on line 416 in handlers/containers.go

View check run for this annotation

Codecov / codecov/patch

handlers/containers.go#L414-L416

Added lines #L414 - L416 were not covered by tests

cnr.SetCreationTime(time.Now())

if request.ContainerName != "" {
Expand Down
54 changes: 54 additions & 0 deletions internal/cache/networkinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cache

import (
"context"
"fmt"
"sync"
"time"

"github.com/nspcc-dev/neofs-sdk-go/client"
"github.com/nspcc-dev/neofs-sdk-go/netmap"
"github.com/nspcc-dev/neofs-sdk-go/pool"
)

type (
// NetworkInfo is cache wrapper for the network info.
NetworkInfo struct {
p *pool.Pool
ttl time.Duration

mu *sync.RWMutex
validUntil time.Time
ni netmap.NetworkInfo
}
)

func NewNetworkInfoCache(p *pool.Pool, ttl time.Duration) *NetworkInfo {
return &NetworkInfo{
p: p,
ttl: ttl,
mu: &sync.RWMutex{},
}
}

func (n *NetworkInfo) NetworkInfo(ctx context.Context) (netmap.NetworkInfo, error) {
n.mu.RLock()
if n.validUntil.After(time.Now()) {
defer n.mu.RUnlock()

return n.ni, nil
}
n.mu.RUnlock()

ni, err := n.p.NetworkInfo(ctx, client.PrmNetworkInfo{})
if err != nil {
return netmap.NetworkInfo{}, fmt.Errorf("get network info: %w", err)
}

Check warning on line 46 in internal/cache/networkinfo.go

View check run for this annotation

Codecov / codecov/patch

internal/cache/networkinfo.go#L45-L46

Added lines #L45 - L46 were not covered by tests

n.mu.Lock()
n.validUntil = time.Now().Add(n.ttl)
n.ni = ni
n.mu.Unlock()

return ni, nil
}

0 comments on commit 6a0bccb

Please sign in to comment.