[#47] pool: Resolve contextcheck and containedctx linters
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
parent
31271ad8b1
commit
bc62e2f712
2 changed files with 20 additions and 42 deletions
59
pool/pool.go
59
pool/pool.go
|
@ -1938,11 +1938,7 @@ func initSessionForDuration(ctx context.Context, dst *session.Object, c client,
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// nolint: containedctx
|
|
||||||
type callContext struct {
|
type callContext struct {
|
||||||
// base context for RPC
|
|
||||||
context.Context
|
|
||||||
|
|
||||||
client client
|
client client
|
||||||
|
|
||||||
// client endpoint
|
// client endpoint
|
||||||
|
@ -1993,13 +1989,13 @@ func (p *Pool) initCallContext(ctx *callContext, cfg prmCommon, prmCtx prmContex
|
||||||
|
|
||||||
// opens new session or uses cached one.
|
// opens new session or uses cached one.
|
||||||
// Must be called only on initialized callContext with set sessionTarget.
|
// Must be called only on initialized callContext with set sessionTarget.
|
||||||
func (p *Pool) openDefaultSession(ctx *callContext) error {
|
func (p *Pool) openDefaultSession(ctx context.Context, cc *callContext) error {
|
||||||
cacheKey := formCacheKey(ctx.endpoint, ctx.key)
|
cacheKey := formCacheKey(cc.endpoint, cc.key)
|
||||||
|
|
||||||
tok, ok := p.cache.Get(cacheKey)
|
tok, ok := p.cache.Get(cacheKey)
|
||||||
if !ok {
|
if !ok {
|
||||||
// init new session
|
// init new session
|
||||||
err := initSessionForDuration(ctx, &tok, ctx.client, p.stokenDuration, *ctx.key)
|
err := initSessionForDuration(ctx, &tok, cc.client, p.stokenDuration, *cc.key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("session API client: %w", err)
|
return fmt.Errorf("session API client: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -2008,37 +2004,37 @@ func (p *Pool) openDefaultSession(ctx *callContext) error {
|
||||||
p.cache.Put(cacheKey, tok)
|
p.cache.Put(cacheKey, tok)
|
||||||
}
|
}
|
||||||
|
|
||||||
tok.ForVerb(ctx.sessionVerb)
|
tok.ForVerb(cc.sessionVerb)
|
||||||
tok.BindContainer(ctx.sessionCnr)
|
tok.BindContainer(cc.sessionCnr)
|
||||||
|
|
||||||
if ctx.sessionObjSet {
|
if cc.sessionObjSet {
|
||||||
tok.LimitByObjects(ctx.sessionObjs...)
|
tok.LimitByObjects(cc.sessionObjs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// sign the token
|
// sign the token
|
||||||
if err := tok.Sign(*ctx.key); err != nil {
|
if err := tok.Sign(*cc.key); err != nil {
|
||||||
return fmt.Errorf("sign token of the opened session: %w", err)
|
return fmt.Errorf("sign token of the opened session: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.sessionTarget(tok)
|
cc.sessionTarget(tok)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// opens default session (if sessionDefault is set), and calls f. If f returns
|
// opens default session (if sessionDefault is set), and calls f. If f returns
|
||||||
// session-related error then cached token is removed.
|
// session-related error then cached token is removed.
|
||||||
func (p *Pool) call(ctx *callContext, f func() error) error {
|
func (p *Pool) call(ctx context.Context, cc *callContext, f func() error) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if ctx.sessionDefault {
|
if cc.sessionDefault {
|
||||||
err = p.openDefaultSession(ctx)
|
err = p.openDefaultSession(ctx, cc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("open default session: %w", err)
|
return fmt.Errorf("open default session: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = f()
|
err = f()
|
||||||
_ = p.checkSessionTokenErr(err, ctx.endpoint)
|
_ = p.checkSessionTokenErr(err, cc.endpoint)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -2064,17 +2060,13 @@ func (p *Pool) PutObject(ctx context.Context, prm PrmObjectPut) (oid.ID, error)
|
||||||
p.fillAppropriateKey(&prm.prmCommon)
|
p.fillAppropriateKey(&prm.prmCommon)
|
||||||
|
|
||||||
var ctxCall callContext
|
var ctxCall callContext
|
||||||
|
|
||||||
ctxCall.Context = ctx
|
|
||||||
|
|
||||||
if err := p.initCallContext(&ctxCall, prm.prmCommon, prmCtx); err != nil {
|
if err := p.initCallContext(&ctxCall, prm.prmCommon, prmCtx); err != nil {
|
||||||
return oid.ID{}, fmt.Errorf("init call context: %w", err)
|
return oid.ID{}, fmt.Errorf("init call context: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctxCall.sessionDefault {
|
if ctxCall.sessionDefault {
|
||||||
ctxCall.sessionTarget = prm.UseSession
|
ctxCall.sessionTarget = prm.UseSession
|
||||||
// nolint: contextcheck
|
if err := p.openDefaultSession(ctx, &ctxCall); err != nil {
|
||||||
if err := p.openDefaultSession(&ctxCall); err != nil {
|
|
||||||
return oid.ID{}, fmt.Errorf("open default session: %w", err)
|
return oid.ID{}, fmt.Errorf("open default session: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2117,8 +2109,6 @@ func (p *Pool) DeleteObject(ctx context.Context, prm PrmObjectDelete) error {
|
||||||
p.fillAppropriateKey(&prm.prmCommon)
|
p.fillAppropriateKey(&prm.prmCommon)
|
||||||
|
|
||||||
var cc callContext
|
var cc callContext
|
||||||
|
|
||||||
cc.Context = ctx
|
|
||||||
cc.sessionTarget = prm.UseSession
|
cc.sessionTarget = prm.UseSession
|
||||||
|
|
||||||
err := p.initCallContext(&cc, prm.prmCommon, prmCtx)
|
err := p.initCallContext(&cc, prm.prmCommon, prmCtx)
|
||||||
|
@ -2126,8 +2116,7 @@ func (p *Pool) DeleteObject(ctx context.Context, prm PrmObjectDelete) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// nolint: contextcheck
|
return p.call(ctx, &cc, func() error {
|
||||||
return p.call(&cc, func() error {
|
|
||||||
if err = cc.client.objectDelete(ctx, prm); err != nil {
|
if err = cc.client.objectDelete(ctx, prm); err != nil {
|
||||||
return fmt.Errorf("remove object via client: %w", err)
|
return fmt.Errorf("remove object via client: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -2169,7 +2158,6 @@ func (p *Pool) GetObject(ctx context.Context, prm PrmObjectGet) (ResGetObject, e
|
||||||
p.fillAppropriateKey(&prm.prmCommon)
|
p.fillAppropriateKey(&prm.prmCommon)
|
||||||
|
|
||||||
var cc callContext
|
var cc callContext
|
||||||
cc.Context = ctx
|
|
||||||
cc.sessionTarget = prm.UseSession
|
cc.sessionTarget = prm.UseSession
|
||||||
|
|
||||||
var res ResGetObject
|
var res ResGetObject
|
||||||
|
@ -2179,8 +2167,7 @@ func (p *Pool) GetObject(ctx context.Context, prm PrmObjectGet) (ResGetObject, e
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// nolint: contextcheck
|
return res, p.call(ctx, &cc, func() error {
|
||||||
return res, p.call(&cc, func() error {
|
|
||||||
res, err = cc.client.objectGet(ctx, prm)
|
res, err = cc.client.objectGet(ctx, prm)
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
|
@ -2193,8 +2180,6 @@ func (p *Pool) HeadObject(ctx context.Context, prm PrmObjectHead) (object.Object
|
||||||
p.fillAppropriateKey(&prm.prmCommon)
|
p.fillAppropriateKey(&prm.prmCommon)
|
||||||
|
|
||||||
var cc callContext
|
var cc callContext
|
||||||
|
|
||||||
cc.Context = ctx
|
|
||||||
cc.sessionTarget = prm.UseSession
|
cc.sessionTarget = prm.UseSession
|
||||||
|
|
||||||
var obj object.Object
|
var obj object.Object
|
||||||
|
@ -2204,8 +2189,7 @@ func (p *Pool) HeadObject(ctx context.Context, prm PrmObjectHead) (object.Object
|
||||||
return obj, err
|
return obj, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// nolint: contextcheck
|
return obj, p.call(ctx, &cc, func() error {
|
||||||
return obj, p.call(&cc, func() error {
|
|
||||||
obj, err = cc.client.objectHead(ctx, prm)
|
obj, err = cc.client.objectHead(ctx, prm)
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
|
@ -2244,7 +2228,6 @@ func (p *Pool) ObjectRange(ctx context.Context, prm PrmObjectRange) (ResObjectRa
|
||||||
p.fillAppropriateKey(&prm.prmCommon)
|
p.fillAppropriateKey(&prm.prmCommon)
|
||||||
|
|
||||||
var cc callContext
|
var cc callContext
|
||||||
cc.Context = ctx
|
|
||||||
cc.sessionTarget = prm.UseSession
|
cc.sessionTarget = prm.UseSession
|
||||||
|
|
||||||
var res ResObjectRange
|
var res ResObjectRange
|
||||||
|
@ -2254,8 +2237,7 @@ func (p *Pool) ObjectRange(ctx context.Context, prm PrmObjectRange) (ResObjectRa
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// nolint: contextcheck
|
return res, p.call(ctx, &cc, func() error {
|
||||||
return res, p.call(&cc, func() error {
|
|
||||||
res, err = cc.client.objectRange(ctx, prm)
|
res, err = cc.client.objectRange(ctx, prm)
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
|
@ -2307,8 +2289,6 @@ func (p *Pool) SearchObjects(ctx context.Context, prm PrmObjectSearch) (ResObjec
|
||||||
p.fillAppropriateKey(&prm.prmCommon)
|
p.fillAppropriateKey(&prm.prmCommon)
|
||||||
|
|
||||||
var cc callContext
|
var cc callContext
|
||||||
|
|
||||||
cc.Context = ctx
|
|
||||||
cc.sessionTarget = prm.UseSession
|
cc.sessionTarget = prm.UseSession
|
||||||
|
|
||||||
var res ResObjectSearch
|
var res ResObjectSearch
|
||||||
|
@ -2318,8 +2298,7 @@ func (p *Pool) SearchObjects(ctx context.Context, prm PrmObjectSearch) (ResObjec
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// nolint: contextcheck
|
return res, p.call(ctx, &cc, func() error {
|
||||||
return res, p.call(&cc, func() error {
|
|
||||||
res, err = cc.client.objectSearch(ctx, prm)
|
res, err = cc.client.objectSearch(ctx, prm)
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
|
|
|
@ -458,14 +458,13 @@ func TestSessionTokenOwner(t *testing.T) {
|
||||||
|
|
||||||
var tkn session.Object
|
var tkn session.Object
|
||||||
var cc callContext
|
var cc callContext
|
||||||
cc.Context = ctx
|
|
||||||
cc.sessionTarget = func(tok session.Object) {
|
cc.sessionTarget = func(tok session.Object) {
|
||||||
tkn = tok
|
tkn = tok
|
||||||
}
|
}
|
||||||
err = p.initCallContext(&cc, prm, prmCtx)
|
err = p.initCallContext(&cc, prm, prmCtx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
err = p.openDefaultSession(&cc)
|
err = p.openDefaultSession(ctx, &cc)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.True(t, tkn.VerifySignature())
|
require.True(t, tkn.VerifySignature())
|
||||||
require.True(t, tkn.Issuer().Equals(anonOwner))
|
require.True(t, tkn.Issuer().Equals(anonOwner))
|
||||||
|
|
Loading…
Reference in a new issue