[#47] pool: Resolve contextcheck and containedctx linters

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-04-07 08:51:20 +03:00
parent 31271ad8b1
commit bc62e2f712
2 changed files with 20 additions and 42 deletions

View file

@ -1938,11 +1938,7 @@ func initSessionForDuration(ctx context.Context, dst *session.Object, c client,
return nil
}
// nolint: containedctx
type callContext struct {
// base context for RPC
context.Context
client client
// client endpoint
@ -1993,13 +1989,13 @@ func (p *Pool) initCallContext(ctx *callContext, cfg prmCommon, prmCtx prmContex
// opens new session or uses cached one.
// Must be called only on initialized callContext with set sessionTarget.
func (p *Pool) openDefaultSession(ctx *callContext) error {
cacheKey := formCacheKey(ctx.endpoint, ctx.key)
func (p *Pool) openDefaultSession(ctx context.Context, cc *callContext) error {
cacheKey := formCacheKey(cc.endpoint, cc.key)
tok, ok := p.cache.Get(cacheKey)
if !ok {
// 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 {
return fmt.Errorf("session API client: %w", err)
}
@ -2008,37 +2004,37 @@ func (p *Pool) openDefaultSession(ctx *callContext) error {
p.cache.Put(cacheKey, tok)
}
tok.ForVerb(ctx.sessionVerb)
tok.BindContainer(ctx.sessionCnr)
tok.ForVerb(cc.sessionVerb)
tok.BindContainer(cc.sessionCnr)
if ctx.sessionObjSet {
tok.LimitByObjects(ctx.sessionObjs...)
if cc.sessionObjSet {
tok.LimitByObjects(cc.sessionObjs...)
}
// 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)
}
ctx.sessionTarget(tok)
cc.sessionTarget(tok)
return nil
}
// opens default session (if sessionDefault is set), and calls f. If f returns
// 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
if ctx.sessionDefault {
err = p.openDefaultSession(ctx)
if cc.sessionDefault {
err = p.openDefaultSession(ctx, cc)
if err != nil {
return fmt.Errorf("open default session: %w", err)
}
}
err = f()
_ = p.checkSessionTokenErr(err, ctx.endpoint)
_ = p.checkSessionTokenErr(err, cc.endpoint)
return err
}
@ -2064,17 +2060,13 @@ func (p *Pool) PutObject(ctx context.Context, prm PrmObjectPut) (oid.ID, error)
p.fillAppropriateKey(&prm.prmCommon)
var ctxCall callContext
ctxCall.Context = ctx
if err := p.initCallContext(&ctxCall, prm.prmCommon, prmCtx); err != nil {
return oid.ID{}, fmt.Errorf("init call context: %w", err)
}
if ctxCall.sessionDefault {
ctxCall.sessionTarget = prm.UseSession
// nolint: contextcheck
if err := p.openDefaultSession(&ctxCall); err != nil {
if err := p.openDefaultSession(ctx, &ctxCall); err != nil {
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)
var cc callContext
cc.Context = ctx
cc.sessionTarget = prm.UseSession
err := p.initCallContext(&cc, prm.prmCommon, prmCtx)
@ -2126,8 +2116,7 @@ func (p *Pool) DeleteObject(ctx context.Context, prm PrmObjectDelete) error {
return err
}
// nolint: contextcheck
return p.call(&cc, func() error {
return p.call(ctx, &cc, func() error {
if err = cc.client.objectDelete(ctx, prm); err != nil {
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)
var cc callContext
cc.Context = ctx
cc.sessionTarget = prm.UseSession
var res ResGetObject
@ -2179,8 +2167,7 @@ func (p *Pool) GetObject(ctx context.Context, prm PrmObjectGet) (ResGetObject, e
return res, err
}
// nolint: contextcheck
return res, p.call(&cc, func() error {
return res, p.call(ctx, &cc, func() error {
res, err = cc.client.objectGet(ctx, prm)
return err
})
@ -2193,8 +2180,6 @@ func (p *Pool) HeadObject(ctx context.Context, prm PrmObjectHead) (object.Object
p.fillAppropriateKey(&prm.prmCommon)
var cc callContext
cc.Context = ctx
cc.sessionTarget = prm.UseSession
var obj object.Object
@ -2204,8 +2189,7 @@ func (p *Pool) HeadObject(ctx context.Context, prm PrmObjectHead) (object.Object
return obj, err
}
// nolint: contextcheck
return obj, p.call(&cc, func() error {
return obj, p.call(ctx, &cc, func() error {
obj, err = cc.client.objectHead(ctx, prm)
return err
})
@ -2244,7 +2228,6 @@ func (p *Pool) ObjectRange(ctx context.Context, prm PrmObjectRange) (ResObjectRa
p.fillAppropriateKey(&prm.prmCommon)
var cc callContext
cc.Context = ctx
cc.sessionTarget = prm.UseSession
var res ResObjectRange
@ -2254,8 +2237,7 @@ func (p *Pool) ObjectRange(ctx context.Context, prm PrmObjectRange) (ResObjectRa
return res, err
}
// nolint: contextcheck
return res, p.call(&cc, func() error {
return res, p.call(ctx, &cc, func() error {
res, err = cc.client.objectRange(ctx, prm)
return err
})
@ -2307,8 +2289,6 @@ func (p *Pool) SearchObjects(ctx context.Context, prm PrmObjectSearch) (ResObjec
p.fillAppropriateKey(&prm.prmCommon)
var cc callContext
cc.Context = ctx
cc.sessionTarget = prm.UseSession
var res ResObjectSearch
@ -2318,8 +2298,7 @@ func (p *Pool) SearchObjects(ctx context.Context, prm PrmObjectSearch) (ResObjec
return res, err
}
// nolint: contextcheck
return res, p.call(&cc, func() error {
return res, p.call(ctx, &cc, func() error {
res, err = cc.client.objectSearch(ctx, prm)
return err
})

View file

@ -458,14 +458,13 @@ func TestSessionTokenOwner(t *testing.T) {
var tkn session.Object
var cc callContext
cc.Context = ctx
cc.sessionTarget = func(tok session.Object) {
tkn = tok
}
err = p.initCallContext(&cc, prm, prmCtx)
require.NoError(t, err)
err = p.openDefaultSession(&cc)
err = p.openDefaultSession(ctx, &cc)
require.NoError(t, err)
require.True(t, tkn.VerifySignature())
require.True(t, tkn.Issuer().Equals(anonOwner))