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 <[email protected]>
  • Loading branch information
smallhive committed Oct 22, 2024
1 parent 23424c7 commit d29fdfe
Show file tree
Hide file tree
Showing 5 changed files with 68 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.EpochUpdateInterval = time.Duration(int64(ni.EpochDuration())/2*ni.MsPerBlock()) * time.Millisecond

return handlers.NewAPI(&apiPrm)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22
require (
github.com/getkin/kin-openapi v0.127.0
github.com/google/uuid v1.6.0
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/labstack/echo/v4 v4.12.0
github.com/nspcc-dev/neo-go v0.106.3
github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.12
Expand Down Expand Up @@ -33,7 +34,6 @@ require (
github.com/go-openapi/swag v0.23.0 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/invopop/yaml v0.3.1 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/labstack/gommon v0.4.2 // indirect
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
46 changes: 46 additions & 0 deletions internal/cache/networkinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cache

import (
"context"
"fmt"
"time"

"github.com/hashicorp/golang-lru/v2/expirable"
"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
cache *expirable.LRU[string, netmap.NetworkInfo]
}
)

const (
networkInfoKey = "networkInfoKey"
)

func NewNetworkInfoCache(p *pool.Pool, ttl time.Duration) *NetworkInfo {
return &NetworkInfo{
p: p,
cache: expirable.NewLRU[string, netmap.NetworkInfo](1, nil, ttl),
}
}

func (n *NetworkInfo) NetworkInfo(ctx context.Context) (netmap.NetworkInfo, error) {
v, ok := n.cache.Get(networkInfoKey)
if ok {
return v, nil
}

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 42 in internal/cache/networkinfo.go

View check run for this annotation

Codecov / codecov/patch

internal/cache/networkinfo.go#L41-L42

Added lines #L41 - L42 were not covered by tests

n.cache.Add(networkInfoKey, ni)
return ni, nil
}

0 comments on commit d29fdfe

Please sign in to comment.