Skip to content

Commit

Permalink
pool: Add option to disable auto session on object operations
Browse files Browse the repository at this point in the history
close #449

Signed-off-by: Evgenii Baidakov <[email protected]>
  • Loading branch information
smallhive committed Jul 6, 2023
1 parent 6acff6a commit da93d03
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 162 deletions.
34 changes: 1 addition & 33 deletions client/object_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import (
v2refs "github.com/nspcc-dev/neofs-api-go/v2/refs"
rpcapi "github.com/nspcc-dev/neofs-api-go/v2/rpc"
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
v2session "github.com/nspcc-dev/neofs-api-go/v2/session"
"github.com/nspcc-dev/neofs-sdk-go/bearer"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
"github.com/nspcc-dev/neofs-sdk-go/session"
"github.com/nspcc-dev/neofs-sdk-go/stat"
)

Expand All @@ -29,42 +27,12 @@ var (

// PrmObjectDelete groups optional parameters of ObjectDelete operation.
type PrmObjectDelete struct {
meta v2session.RequestMetaHeader
sessionContainer

keySet bool
signer neofscrypto.Signer
}

// WithinSession specifies session within which object should be read.
//
// Creator of the session acquires the authorship of the request.
// This may affect the execution of an operation (e.g. access control).
//
// Must be signed.
func (x *PrmObjectDelete) WithinSession(t session.Object) {
var tv2 v2session.Token
t.WriteToV2(&tv2)

x.meta.SetSessionToken(&tv2)
}

// GetSession returns session object.
//
// Returns ErrNoSession err if session wasn't set.
func (x PrmObjectDelete) GetSession() (*session.Object, error) {
token := x.meta.GetSessionToken()
if token == nil {
return nil, ErrNoSession
}

var sess session.Object
if err := sess.ReadFromV2(*token); err != nil {
return nil, err
}

return &sess, nil
}

// WithBearerToken attaches bearer token to be used for the operation.
//
// If set, underlying eACL rules will be used in access control.
Expand Down
33 changes: 1 addition & 32 deletions client/object_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ import (
v2refs "github.com/nspcc-dev/neofs-api-go/v2/refs"
rpcapi "github.com/nspcc-dev/neofs-api-go/v2/rpc"
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
v2session "github.com/nspcc-dev/neofs-api-go/v2/session"
"github.com/nspcc-dev/neofs-sdk-go/bearer"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
"github.com/nspcc-dev/neofs-sdk-go/object"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
"github.com/nspcc-dev/neofs-sdk-go/session"
"github.com/nspcc-dev/neofs-sdk-go/stat"
)

Expand All @@ -30,7 +28,7 @@ var (

// shared parameters of GET/HEAD/RANGE.
type prmObjectRead struct {
meta v2session.RequestMetaHeader
sessionContainer

raw bool
}
Expand All @@ -53,35 +51,6 @@ func (x *prmObjectRead) MarkLocal() {
x.meta.SetTTL(1)
}

// WithinSession specifies session within which object should be read.
//
// Creator of the session acquires the authorship of the request.
// This may affect the execution of an operation (e.g. access control).
//
// Must be signed.
func (x *prmObjectRead) WithinSession(t session.Object) {
var tokv2 v2session.Token
t.WriteToV2(&tokv2)
x.meta.SetSessionToken(&tokv2)
}

// GetSession returns session object.
//
// Returns ErrNoSession err if session wasn't set.
func (x prmObjectRead) GetSession() (*session.Object, error) {
token := x.meta.GetSessionToken()
if token == nil {
return nil, ErrNoSession
}

var sess session.Object
if err := sess.ReadFromV2(*token); err != nil {
return nil, err
}

return &sess, nil
}

// WithBearerToken attaches bearer token to be used for the operation.
//
// If set, underlying eACL rules will be used in access control.
Expand Down
34 changes: 1 addition & 33 deletions client/object_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import (
v2refs "github.com/nspcc-dev/neofs-api-go/v2/refs"
rpcapi "github.com/nspcc-dev/neofs-api-go/v2/rpc"
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
v2session "github.com/nspcc-dev/neofs-api-go/v2/session"
"github.com/nspcc-dev/neofs-sdk-go/bearer"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
"github.com/nspcc-dev/neofs-sdk-go/session"
"github.com/nspcc-dev/neofs-sdk-go/stat"
)

Expand All @@ -25,7 +23,7 @@ var (

// PrmObjectHash groups parameters of ObjectHash operation.
type PrmObjectHash struct {
meta v2session.RequestMetaHeader
sessionContainer

body v2object.GetRangeHashRequestBody

Expand All @@ -50,36 +48,6 @@ func (x *PrmObjectHash) MarkLocal() {
x.meta.SetTTL(1)
}

// WithinSession specifies session within which object should be read.
//
// Creator of the session acquires the authorship of the request.
// This may affect the execution of an operation (e.g. access control).
//
// Must be signed.
func (x *PrmObjectHash) WithinSession(t session.Object) {
var tv2 v2session.Token
t.WriteToV2(&tv2)

x.meta.SetSessionToken(&tv2)
}

// GetSession returns session object.
//
// Returns ErrNoSession err if session wasn't set.
func (x PrmObjectHash) GetSession() (*session.Object, error) {
token := x.meta.GetSessionToken()
if token == nil {
return nil, ErrNoSession
}

var sess session.Object
if err := sess.ReadFromV2(*token); err != nil {
return nil, err
}

return &sess, nil
}

// WithBearerToken attaches bearer token to be used for the operation.
//
// If set, underlying eACL rules will be used in access control.
Expand Down
36 changes: 7 additions & 29 deletions client/object_put.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@ import (
v2object "github.com/nspcc-dev/neofs-api-go/v2/object"
rpcapi "github.com/nspcc-dev/neofs-api-go/v2/rpc"
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
v2session "github.com/nspcc-dev/neofs-api-go/v2/session"
"github.com/nspcc-dev/neofs-sdk-go/bearer"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
"github.com/nspcc-dev/neofs-sdk-go/object"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
"github.com/nspcc-dev/neofs-sdk-go/object/slicer"
"github.com/nspcc-dev/neofs-sdk-go/session"
"github.com/nspcc-dev/neofs-sdk-go/stat"
"github.com/nspcc-dev/neofs-sdk-go/user"
)

var (
// ErrNoSessionExplicitly is a special error to show auto-session is disabled.
ErrNoSessionExplicitly = errors.New("session was removed explicitly")
)

var (
// special variable for test purposes only, to overwrite real RPC calls.
rpcAPIPutObject = rpcapi.PutObject
Expand All @@ -34,9 +37,10 @@ type shortStatisticCallback func(err error)

// PrmObjectPutInit groups parameters of ObjectPutInit operation.
type PrmObjectPutInit struct {
sessionContainer

copyNum uint32
signer neofscrypto.Signer
meta v2session.RequestMetaHeader
}

// SetCopiesNumber sets number of object copies that is enough to consider put successful.
Expand Down Expand Up @@ -100,32 +104,6 @@ func (x *PrmObjectPutInit) WithBearerToken(t bearer.Token) {
x.meta.SetBearerToken(&v2token)
}

// WithinSession specifies session within which object should be stored.
// Should be called once before any writing steps.
func (x *PrmObjectPutInit) WithinSession(t session.Object) {
var tv2 v2session.Token
t.WriteToV2(&tv2)

x.meta.SetSessionToken(&tv2)
}

// GetSession returns session object.
//
// Returns ErrNoSession err if session wasn't set.
func (x PrmObjectPutInit) GetSession() (*session.Object, error) {
token := x.meta.GetSessionToken()
if token == nil {
return nil, ErrNoSession
}

var sess session.Object
if err := sess.ReadFromV2(*token); err != nil {
return nil, err
}

return &sess, nil
}

// MarkLocal tells the server to execute the operation locally.
func (x *PrmObjectPutInit) MarkLocal() {
x.meta.SetTTL(1)
Expand Down
33 changes: 1 addition & 32 deletions client/object_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ import (
v2refs "github.com/nspcc-dev/neofs-api-go/v2/refs"
rpcapi "github.com/nspcc-dev/neofs-api-go/v2/rpc"
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
v2session "github.com/nspcc-dev/neofs-api-go/v2/session"
"github.com/nspcc-dev/neofs-sdk-go/bearer"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
"github.com/nspcc-dev/neofs-sdk-go/object"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
"github.com/nspcc-dev/neofs-sdk-go/session"
"github.com/nspcc-dev/neofs-sdk-go/stat"
)

Expand All @@ -28,7 +26,7 @@ var (

// PrmObjectSearch groups optional parameters of ObjectSearch operation.
type PrmObjectSearch struct {
meta v2session.RequestMetaHeader
sessionContainer

signer neofscrypto.Signer

Expand All @@ -40,35 +38,6 @@ func (x *PrmObjectSearch) MarkLocal() {
x.meta.SetTTL(1)
}

// WithinSession specifies session within which the search query must be executed.
//
// Creator of the session acquires the authorship of the request.
// This may affect the execution of an operation (e.g. access control).
//
// Must be signed.
func (x *PrmObjectSearch) WithinSession(t session.Object) {
var tokv2 v2session.Token
t.WriteToV2(&tokv2)
x.meta.SetSessionToken(&tokv2)
}

// GetSession returns session object.
//
// Returns ErrNoSession err if session wasn't set.
func (x PrmObjectSearch) GetSession() (*session.Object, error) {
token := x.meta.GetSessionToken()
if token == nil {
return nil, ErrNoSession
}

var sess session.Object
if err := sess.ReadFromV2(*token); err != nil {
return nil, err
}

return &sess, nil
}

// WithBearerToken attaches bearer token to be used for the operation.
//
// If set, underlying eACL rules will be used in access control.
Expand Down
59 changes: 59 additions & 0 deletions client/session_container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package client

import (
v2session "github.com/nspcc-dev/neofs-api-go/v2/session"
"github.com/nspcc-dev/neofs-sdk-go/session"
)

// sessionContainer is a special type which unifies session logic management for client parameters.
// All methods make public, because sessionContainer is included in Prm* structs.
type sessionContainer struct {
isSessionIgnored bool
meta v2session.RequestMetaHeader
}

// GetSession returns session object.
//
// Returns:
// - [ErrNoSession] err if session wasn't set.
// - [ErrNoSessionExplicitly] if IgnoreSession was used.
func (x *sessionContainer) GetSession() (*session.Object, error) {
if x.isSessionIgnored {
return nil, ErrNoSessionExplicitly
}

token := x.meta.GetSessionToken()
if token == nil {
return nil, ErrNoSession
}

var sess session.Object
if err := sess.ReadFromV2(*token); err != nil {
return nil, err
}

return &sess, nil
}

// WithinSession specifies session within which the query must be executed.
//
// Creator of the session acquires the authorship of the request.
// This may affect the execution of an operation (e.g. access control).
//
// See also IgnoreSession.
//
// Must be signed.
func (x *sessionContainer) WithinSession(t session.Object) {
var tokv2 v2session.Token
t.WriteToV2(&tokv2)
x.meta.SetSessionToken(&tokv2)
x.isSessionIgnored = false
}

// IgnoreSession disables auto-session creation.
//
// See also WithinSession.
func (x *sessionContainer) IgnoreSession() {
x.isSessionIgnored = true
x.meta.SetSessionToken(nil)
}
6 changes: 3 additions & 3 deletions pool/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func (p *Pool) withinContainerSession(
verb session.ObjectVerb,
params containerSessionParams,
) error {
// empty error means the session was set.
if _, err := params.GetSession(); err == nil {
_, err := params.GetSession()
if err == nil || errors.Is(err, client.ErrNoSessionExplicitly) {
return nil
}

Expand All @@ -78,7 +78,7 @@ func (p *Pool) withinContainerSession(
tok, ok := p.cache.Get(cacheKey)
if !ok {
// init new session
err := initSession(ctx, &tok, c, p.stokenDuration, signer)
err = initSession(ctx, &tok, c, p.stokenDuration, signer)
if err != nil {
return fmt.Errorf("init session: %w", err)
}
Expand Down

0 comments on commit da93d03

Please sign in to comment.