[#243] services/object: Share common parameters across services

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-12-11 14:59:16 +03:00 committed by Alex Vanin
parent a01262d8bd
commit fb50362dcc
12 changed files with 89 additions and 163 deletions

2
go.sum
View file

@ -283,8 +283,6 @@ github.com/nspcc-dev/neo-go v0.73.1-pre.0.20200303142215-f5a1b928ce09/go.mod h1:
github.com/nspcc-dev/neo-go v0.91.0/go.mod h1:G6HdOWvzQ6tlvFdvFSN/PgCzLPN/X/X4d5hTjFRUDcc=
github.com/nspcc-dev/neo-go v0.91.1-pre.0.20201030072836-71216865717b h1:gk5bZgpWOehaDVKI5vBDkcjXTpRkKqcvIb1h/vHnBH4=
github.com/nspcc-dev/neo-go v0.91.1-pre.0.20201030072836-71216865717b/go.mod h1:9s7LNp2lMgf0caH2t0sam4+WT2SIauXozwP0AdBqnEo=
github.com/nspcc-dev/neofs-api-go v1.20.3-0.20201211081945-1bc91466aa7b h1:0IdLNZHFrPKS7o2gvwo+dWO80k49nTrQq2YmzXN9I/4=
github.com/nspcc-dev/neofs-api-go v1.20.3-0.20201211081945-1bc91466aa7b/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8=
github.com/nspcc-dev/neofs-api-go v1.20.3-0.20201211114827-2aa51d3add29 h1:5Th3BoSCbnKiHFzpoRh2+p3g3Hv1zTHC5dvHwfbIbBk=
github.com/nspcc-dev/neofs-api-go v1.20.3-0.20201211114827-2aa51d3add29/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8=
github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA=

View file

@ -60,7 +60,7 @@ func (exec execCtx) isLocal() bool {
}
func (exec *execCtx) key() *ecdsa.PrivateKey {
return exec.prm.key
return exec.prm.common.PrivateKey()
}
func (exec *execCtx) address() *objectSDK.Address {
@ -76,7 +76,7 @@ func (exec *execCtx) commonParameters() *util.CommonPrm {
}
func (exec execCtx) callOptions() []client.CallOption {
return exec.prm.callOpts
return exec.prm.common.RemoteCallOptions()
}
func (exec *execCtx) newAddress(id *objectSDK.ID) *objectSDK.Address {

View file

@ -1,20 +1,14 @@
package deletesvc
import (
"crypto/ecdsa"
"github.com/nspcc-dev/neofs-api-go/pkg/client"
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
)
// Prm groups parameters of Delete service call.
type Prm struct {
key *ecdsa.PrivateKey
common *util.CommonPrm
callOpts []client.CallOption
client.DeleteObjectParams
}
@ -22,13 +16,3 @@ type Prm struct {
func (p *Prm) SetCommonParameters(common *util.CommonPrm) {
p.common = common
}
// SetPrivateKey sets private key to use during execution.
func (p *Prm) SetPrivateKey(key *ecdsa.PrivateKey) {
p.key = key
}
// SetRemoteCallOptions sets call options of remote client calls.
func (p *Prm) SetRemoteCallOptions(opts ...client.CallOption) {
p.callOpts = opts
}

View file

@ -25,12 +25,10 @@ func (w *headSvcWrapper) headAddress(exec *execCtx, addr *objectSDK.Address) (*o
wr := getsvc.NewSimpleObjectWriter()
p := getsvc.HeadPrm{}
p.SetPrivateKey(exec.key())
p.SetCommonParameters(exec.commonParameters())
p.SetHeaderWriter(wr)
p.WithRawFlag(true)
p.WithAddress(addr)
p.SetRemoteCallOptions(exec.callOptions()...)
err := (*getsvc.Service)(w).Head(exec.context(), p)
if err != nil {
@ -86,10 +84,8 @@ func (w *searchSvcWrapper) splitMembers(exec *execCtx) ([]*objectSDK.ID, error)
p := searchsvc.Prm{}
p.SetWriter(wr)
p.SetCommonParameters(exec.commonParameters())
p.SetPrivateKey(exec.key())
p.WithContainerID(exec.containerID())
p.WithSearchFilters(fs)
p.SetRemoteCallOptions(exec.callOptions()...)
err := (*searchsvc.Service)(w).Search(exec.context(), p)
if err != nil {

View file

@ -1,11 +1,9 @@
package deletesvc
import (
"github.com/nspcc-dev/neofs-api-go/pkg/client"
"github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-api-go/pkg/token"
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
"github.com/nspcc-dev/neofs-api-go/v2/session"
deletesvc "github.com/nspcc-dev/neofs-node/pkg/services/object/delete"
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
)
@ -23,9 +21,9 @@ func (s *Service) toPrm(req *objectV2.DeleteRequest, respBody *objectV2.DeleteRe
}
p := new(deletesvc.Prm)
p.SetPrivateKey(key)
p.SetCommonParameters(commonParameters(meta))
p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...)
p.SetCommonParameters(util.CommonPrmFromV2(req).
WithPrivateKey(key),
)
body := req.GetBody()
p.WithAddress(object.NewAddressFromV2(body.GetAddress()))
@ -39,33 +37,3 @@ func (s *Service) toPrm(req *objectV2.DeleteRequest, respBody *objectV2.DeleteRe
func (w *tombstoneBodyWriter) SetAddress(addr *object.Address) {
w.body.SetTombstone(addr.ToV2())
}
func commonParameters(meta *session.RequestMetaHeader) *util.CommonPrm {
prm := new(util.CommonPrm)
if tok := meta.GetBearerToken(); tok != nil {
prm.WithBearerToken(token.NewBearerTokenFromV2(tok))
}
if tok := meta.GetSessionToken(); tok != nil {
prm.WithSessionToken(token.NewSessionTokenFromV2(tok))
}
return prm
}
// can be shared accross all services
func remoteCallOptionsFromMeta(meta *session.RequestMetaHeader) []client.CallOption {
opts := make([]client.CallOption, 0, 3)
opts = append(opts, client.WithTTL(meta.GetTTL()-1))
if tok := meta.GetBearerToken(); tok != nil {
opts = append(opts, client.WithBearer(token.NewBearerTokenFromV2(tok)))
}
if tok := meta.GetSessionToken(); tok != nil {
opts = append(opts, client.WithSession(token.NewSessionTokenFromV2(tok)))
}
return opts
}

View file

@ -96,11 +96,11 @@ func (exec execCtx) address() *objectSDK.Address {
}
func (exec execCtx) key() *ecdsa.PrivateKey {
return exec.prm.key
return exec.prm.common.PrivateKey()
}
func (exec execCtx) callOptions() []client.CallOption {
return exec.prm.callOpts
return exec.prm.common.RemoteCallOptions()
}
func (exec execCtx) remotePrm() *client.GetObjectParams {

View file

@ -1,7 +1,6 @@
package getsvc
import (
"crypto/ecdsa"
"hash"
"github.com/nspcc-dev/neofs-api-go/pkg/client"
@ -39,11 +38,6 @@ type HeadPrm struct {
type commonPrm struct {
objWriter ObjectWriter
// TODO: replace key and callOpts to CommonPrm
key *ecdsa.PrivateKey
callOpts []client.CallOption
common *util.CommonPrm
client.GetObjectParams
@ -72,16 +66,6 @@ func (p *Prm) SetObjectWriter(w ObjectWriter) {
p.objWriter = w
}
// SetPrivateKey sets private key to use during execution.
func (p *commonPrm) SetPrivateKey(key *ecdsa.PrivateKey) {
p.key = key
}
// SetRemoteCallOptions sets call options remote remote client calls.
func (p *commonPrm) SetRemoteCallOptions(opts ...client.CallOption) {
p.callOpts = opts
}
// SetChunkWriter sets target component to write the object payload range.
func (p *RangePrm) SetChunkWriter(w ChunkWriter) {
p.objWriter = &partWriter{

View file

@ -4,12 +4,10 @@ import (
"crypto/sha256"
"hash"
"github.com/nspcc-dev/neofs-api-go/pkg/client"
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-api-go/pkg/token"
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/nspcc-dev/neofs-api-go/v2/session"
"github.com/nspcc-dev/neofs-node/pkg/core/object"
objectSvc "github.com/nspcc-dev/neofs-node/pkg/services/object"
getsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/get"
@ -27,14 +25,14 @@ func (s *Service) toPrm(req *objectV2.GetRequest, stream objectSvc.GetObjectStre
}
p := new(getsvc.Prm)
p.SetPrivateKey(key)
p.SetCommonParameters(util.CommonPrmFromV2(req).
WithPrivateKey(key),
)
body := req.GetBody()
p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress()))
p.WithRawFlag(body.GetRaw())
p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...)
p.SetObjectWriter(&streamObjectWriter{stream})
p.SetCommonParameters(commonParameters(meta))
return p, nil
}
@ -48,15 +46,15 @@ func (s *Service) toRangePrm(req *objectV2.GetRangeRequest, stream objectSvc.Get
}
p := new(getsvc.RangePrm)
p.SetPrivateKey(key)
p.SetCommonParameters(util.CommonPrmFromV2(req).
WithPrivateKey(key),
)
body := req.GetBody()
p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress()))
p.WithRawFlag(body.GetRaw())
p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...)
p.SetChunkWriter(&streamObjectRangeWriter{stream})
p.SetRange(objectSDK.NewRangeFromV2(body.GetRange()))
p.SetCommonParameters(commonParameters(meta))
return p, nil
}
@ -70,12 +68,12 @@ func (s *Service) toHashRangePrm(req *objectV2.GetRangeHashRequest) (*getsvc.Ran
}
p := new(getsvc.RangeHashPrm)
p.SetPrivateKey(key)
p.SetCommonParameters(util.CommonPrmFromV2(req).
WithPrivateKey(key),
)
body := req.GetBody()
p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress()))
p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...)
p.SetCommonParameters(commonParameters(meta))
rngsV2 := body.GetRanges()
rngs := make([]*objectSDK.Range, 0, len(rngsV2))
@ -127,42 +125,21 @@ func (s *Service) toHeadPrm(req *objectV2.HeadRequest, resp *objectV2.HeadRespon
}
p := new(getsvc.HeadPrm)
p.SetPrivateKey(key)
p.SetCommonParameters(util.CommonPrmFromV2(req).
WithPrivateKey(key),
)
body := req.GetBody()
p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress()))
p.WithRawFlag(body.GetRaw())
p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...)
p.SetHeaderWriter(&headResponseWriter{
mainOnly: body.GetMainOnly(),
body: resp.GetBody(),
})
p.SetCommonParameters(commonParameters(meta))
return p, nil
}
// can be shared accross all services
func remoteCallOptionsFromMeta(meta *session.RequestMetaHeader) []client.CallOption {
opts := make([]client.CallOption, 0, 3)
opts = append(opts, client.WithTTL(meta.GetTTL()-1))
if tok := meta.GetBearerToken(); tok != nil {
opts = append(opts, client.WithBearer(token.NewBearerTokenFromV2(tok)))
}
if tok := meta.GetSessionToken(); tok != nil {
opts = append(opts, client.WithSession(token.NewSessionTokenFromV2(tok)))
}
return opts
}
func commonParameters(meta *session.RequestMetaHeader) *util.CommonPrm {
return new(util.CommonPrm).
WithLocalOnly(meta.GetTTL() <= 1)
}
func splitInfoResponse(info *objectSDK.SplitInfo) *objectV2.GetResponse {
resp := new(objectV2.GetResponse)

View file

@ -60,11 +60,11 @@ func (exec execCtx) isLocal() bool {
}
func (exec execCtx) key() *ecdsa.PrivateKey {
return exec.prm.key
return exec.prm.common.PrivateKey()
}
func (exec execCtx) callOptions() []client.CallOption {
return exec.prm.callOpts
return exec.prm.common.RemoteCallOptions()
}
func (exec execCtx) remotePrm() *client.SearchObjectParams {

View file

@ -1,8 +1,6 @@
package searchsvc
import (
"crypto/ecdsa"
"github.com/nspcc-dev/neofs-api-go/pkg/client"
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
@ -12,11 +10,6 @@ import (
type Prm struct {
writer IDListWriter
// TODO: replace key and callOpts to CommonPrm
key *ecdsa.PrivateKey
callOpts []client.CallOption
common *util.CommonPrm
client.SearchObjectParams
@ -28,16 +21,6 @@ type IDListWriter interface {
WriteIDs([]*objectSDK.ID) error
}
// SetPrivateKey sets private key to use during execution.
func (p *Prm) SetPrivateKey(key *ecdsa.PrivateKey) {
p.key = key
}
// SetRemoteCallOptions sets call options remote remote client calls.
func (p *Prm) SetRemoteCallOptions(opts ...client.CallOption) {
p.callOpts = opts
}
// SetCommonParameters sets common parameters of the operation.
func (p *Prm) SetCommonParameters(common *util.CommonPrm) {
p.common = common

View file

@ -1,12 +1,10 @@
package searchsvc
import (
"github.com/nspcc-dev/neofs-api-go/pkg/client"
"github.com/nspcc-dev/neofs-api-go/pkg/container"
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-api-go/pkg/token"
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
"github.com/nspcc-dev/neofs-api-go/v2/session"
objectSvc "github.com/nspcc-dev/neofs-node/pkg/services/object"
searchsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/search"
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
@ -21,9 +19,10 @@ func (s *Service) toPrm(req *objectV2.SearchRequest, stream objectSvc.SearchStre
}
p := new(searchsvc.Prm)
p.SetPrivateKey(key)
p.SetCommonParameters(commonParameters(meta))
p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...)
p.SetCommonParameters(util.CommonPrmFromV2(req).
WithPrivateKey(key),
)
p.SetWriter(&streamWriter{
stream: stream,
})
@ -34,25 +33,3 @@ func (s *Service) toPrm(req *objectV2.SearchRequest, stream objectSvc.SearchStre
return p, nil
}
// can be shared accross all services
func remoteCallOptionsFromMeta(meta *session.RequestMetaHeader) []client.CallOption {
opts := make([]client.CallOption, 0, 3)
opts = append(opts, client.WithTTL(meta.GetTTL()-1))
if tok := meta.GetBearerToken(); tok != nil {
opts = append(opts, client.WithBearer(token.NewBearerTokenFromV2(tok)))
}
if tok := meta.GetSessionToken(); tok != nil {
opts = append(opts, client.WithSession(token.NewSessionTokenFromV2(tok)))
}
return opts
}
func commonParameters(meta *session.RequestMetaHeader) *util.CommonPrm {
return new(util.CommonPrm).
WithLocalOnly(meta.GetTTL() <= 1)
}

View file

@ -1,6 +1,9 @@
package util
import (
"crypto/ecdsa"
"github.com/nspcc-dev/neofs-api-go/pkg/client"
"github.com/nspcc-dev/neofs-api-go/pkg/token"
"github.com/nspcc-dev/neofs-api-go/v2/session"
)
@ -8,8 +11,13 @@ import (
type CommonPrm struct {
local bool
token *token.SessionToken
token *token.SessionToken
bearer *token.BearerToken
key *ecdsa.PrivateKey
callOpts []client.CallOption
}
func (p *CommonPrm) WithLocalOnly(v bool) *CommonPrm {
@ -44,6 +52,42 @@ func (p *CommonPrm) WithBearerToken(token *token.BearerToken) *CommonPrm {
return p
}
// WithPrivateKey sets private key to use during execution.
func (p *CommonPrm) WithPrivateKey(key *ecdsa.PrivateKey) *CommonPrm {
if p != nil {
p.key = key
}
return p
}
// PrivateKey returns private key to use during execution.
func (p *CommonPrm) PrivateKey() *ecdsa.PrivateKey {
if p != nil {
return p.key
}
return nil
}
// WithRemoteCallOptions sets call options remote remote client calls.
func (p *CommonPrm) WithRemoteCallOptions(opts ...client.CallOption) *CommonPrm {
if p != nil {
p.callOpts = opts
}
return p
}
// RemoteCallOptions return call options for remote client calls.
func (p *CommonPrm) RemoteCallOptions() []client.CallOption {
if p != nil {
return p.callOpts
}
return nil
}
func (p *CommonPrm) SessionToken() *token.SessionToken {
if p != nil {
return p.token
@ -65,9 +109,24 @@ func CommonPrmFromV2(req interface {
}) *CommonPrm {
meta := req.GetMetaHeader()
return &CommonPrm{
local: meta.GetTTL() <= 1, // FIXME: use constant
token: token.NewSessionTokenFromV2(meta.GetSessionToken()),
bearer: token.NewBearerTokenFromV2(meta.GetBearerToken()),
prm := &CommonPrm{
local: meta.GetTTL() <= 1, // FIXME: use constant
token: nil,
bearer: nil,
callOpts: make([]client.CallOption, 0, 3),
}
prm.callOpts = append(prm.callOpts, client.WithTTL(meta.GetTTL()-1))
if tok := meta.GetSessionToken(); tok != nil {
prm.token = token.NewSessionTokenFromV2(tok)
prm.callOpts = append(prm.callOpts, client.WithSession(prm.token))
}
if tok := meta.GetBearerToken(); tok != nil {
prm.bearer = token.NewBearerTokenFromV2(tok)
prm.callOpts = append(prm.callOpts, client.WithBearer(prm.bearer))
}
return prm
}