WIP: Cache head results in the request context #974

Closed
fyrchik wants to merge 6 commits from fyrchik/frostfs-node:head-cache into master
3 changed files with 31 additions and 30 deletions
Showing only changes of commit 0858886d2a - Show all commits

View file

@ -112,7 +112,7 @@ type wrappedGetObjectStream struct {
}
func (w *wrappedGetObjectStream) Context() context.Context {
return context.WithValue(w.GetObjectStream.Context(), object.RequestContextKey, &object.RequestContext{
return object.NewRequestContext(w.GetObjectStream.Context(), &object.RequestContext{
Namespace: w.requestInfo.ContainerNamespace(),
ContainerOwner: w.requestInfo.ContainerOwner(),
SenderKey: w.requestInfo.SenderKey(),
@ -137,7 +137,7 @@ type wrappedRangeStream struct {
}
func (w *wrappedRangeStream) Context() context.Context {
return context.WithValue(w.GetObjectRangeStream.Context(), object.RequestContextKey, &object.RequestContext{
return object.NewRequestContext(w.GetObjectRangeStream.Context(), &object.RequestContext{
Namespace: w.requestInfo.ContainerNamespace(),
ContainerOwner: w.requestInfo.ContainerOwner(),
SenderKey: w.requestInfo.SenderKey(),
@ -162,7 +162,7 @@ type wrappedSearchStream struct {
}
func (w *wrappedSearchStream) Context() context.Context {
return context.WithValue(w.SearchStream.Context(), object.RequestContextKey, &object.RequestContext{
return object.NewRequestContext(w.SearchStream.Context(), &object.RequestContext{
Namespace: w.requestInfo.ContainerNamespace(),
ContainerOwner: w.requestInfo.ContainerOwner(),
SenderKey: w.requestInfo.SenderKey(),
@ -473,7 +473,7 @@ func (b Service) GetRange(request *objectV2.GetRangeRequest, stream object.GetOb
}
func requestContext(ctx context.Context, reqInfo RequestInfo) context.Context {
return context.WithValue(ctx, object.RequestContextKey, &object.RequestContext{
return object.NewRequestContext(ctx, &object.RequestContext{
Namespace: reqInfo.ContainerNamespace(),
ContainerOwner: reqInfo.ContainerOwner(),
SenderKey: reqInfo.SenderKey(),

View file

@ -3,8 +3,6 @@ package ape
import (
"context"
"encoding/hex"
"errors"
"fmt"
objectV2 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
@ -18,8 +16,6 @@ import (
nativeschema "git.frostfs.info/TrueCloudLab/policy-engine/schema/native"
)
var errFailedToCastToRequestContext = errors.New("failed cast to RequestContext")
type Service struct {
log *logger.Logger
@ -101,25 +97,13 @@ func (g *getStreamBasicChecker) Send(resp *objectV2.GetResponse) error {
return g.GetObjectStream.Send(resp)
}
func requestContext(ctx context.Context) (*objectSvc.RequestContext, error) {
untyped := ctx.Value(objectSvc.RequestContextKey)
if untyped == nil {
return nil, fmt.Errorf("no key %s in context", objectSvc.RequestContextKey)
}
rc, ok := untyped.(*objectSvc.RequestContext)
if !ok {
return nil, errFailedToCastToRequestContext
}
return rc, nil
}
func (c *Service) Get(request *objectV2.GetRequest, stream objectSvc.GetObjectStream) error {
cnrID, objID, err := getAddressParamsSDK(request.GetBody().GetAddress().GetContainerID(), request.GetBody().GetAddress().GetObjectID())
if err != nil {
return toStatusErr(err)
}
reqCtx, err := requestContext(stream.Context())
reqCtx, err := objectSvc.FromRequestContext(stream.Context())
if err != nil {
return toStatusErr(err)
}
@ -156,7 +140,7 @@ type putStreamBasicChecker struct {
func (p *putStreamBasicChecker) Send(ctx context.Context, request *objectV2.PutRequest) error {
if partInit, ok := request.GetBody().GetObjectPart().(*objectV2.PutObjectPartInit); ok {
reqCtx, err := requestContext(ctx)
reqCtx, err := objectSvc.FromRequestContext(ctx)
if err != nil {
return toStatusErr(err)
}
@ -205,7 +189,7 @@ func (c *Service) Head(ctx context.Context, request *objectV2.HeadRequest) (*obj
return nil, err
}
reqCtx, err := requestContext(ctx)
reqCtx, err := objectSvc.FromRequestContext(ctx)
if err != nil {
return nil, err
}
@ -273,7 +257,7 @@ func (c *Service) Search(request *objectV2.SearchRequest, stream objectSvc.Searc
}
}
reqCtx, err := requestContext(stream.Context())
reqCtx, err := objectSvc.FromRequestContext(stream.Context())
if err != nil {
return toStatusErr(err)
}
@ -300,7 +284,7 @@ func (c *Service) Delete(ctx context.Context, request *objectV2.DeleteRequest) (
return nil, err
}
reqCtx, err := requestContext(ctx)
reqCtx, err := objectSvc.FromRequestContext(ctx)
if err != nil {
return nil, err
}
@ -333,7 +317,7 @@ func (c *Service) GetRange(request *objectV2.GetRangeRequest, stream objectSvc.G
return toStatusErr(err)
}
reqCtx, err := requestContext(stream.Context())
reqCtx, err := objectSvc.FromRequestContext(stream.Context())
if err != nil {
return toStatusErr(err)
}
@ -361,7 +345,7 @@ func (c *Service) GetRangeHash(ctx context.Context, request *objectV2.GetRangeHa
return nil, err
}
reqCtx, err := requestContext(ctx)
reqCtx, err := objectSvc.FromRequestContext(ctx)
if err != nil {
return nil, err
}
@ -398,7 +382,7 @@ func (c *Service) PutSingle(ctx context.Context, request *objectV2.PutSingleRequ
return nil, err
}
reqCtx, err := requestContext(ctx)
reqCtx, err := objectSvc.FromRequestContext(ctx)
if err != nil {
return nil, err
}

View file

@ -1,13 +1,16 @@
package object
import (
"context"
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/acl"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
)
type RequestContextKeyT struct{}
type requestContextKeyT struct{}
var RequestContextKey = RequestContextKeyT{}
var requestContextKey = requestContextKeyT{}
// RequestContext is a context passed between middleware handlers.
type RequestContext struct {
@ -21,3 +24,17 @@ type RequestContext struct {
SoftAPECheck bool
}
// NewRequestContext returns a copy of ctx which carries value.
func NewRequestContext(ctx context.Context, value *RequestContext) context.Context {
return context.WithValue(ctx, requestContextKey, value)
}
// FromRequestContext returns RequestContext value stored in ctx if any.
func FromRequestContext(ctx context.Context) (*RequestContext, error) {
reqCtx, ok := ctx.Value(requestContextKey).(*RequestContext)
if !ok {
return nil, fmt.Errorf("no key %s in context", requestContextKey)
}
return reqCtx, nil
}