diff --git a/creds/tokens/credentials.go b/creds/tokens/credentials.go index 1acd48dd..24abb4b6 100644 --- a/creds/tokens/credentials.go +++ b/creds/tokens/credentials.go @@ -118,24 +118,7 @@ func New(cfg Config) Credentials { func (c *cred) GetBox(ctx context.Context, addr oid.Address) (*accessbox.Box, error) { cachedBoxValue := c.cache.Get(addr) if cachedBoxValue != nil { - if time.Since(cachedBoxValue.PutTime) > c.removingCheckDuration { - if box, err := c.getAccessBox(ctx, addr); err != nil { - if client.IsErrObjectAlreadyRemoved(err) { - c.cache.Delete(addr) - return nil, fmt.Errorf("get access box: %w", err) - } - } else { - cachedBox, err := box.GetBox(c.key) - if err != nil { - c.cache.Delete(addr) - return nil, fmt.Errorf("get gate box: %w", err) - } - // we need this to reset PutTime - // to don't check for removing each time after removingCheckDuration interval - c.putBoxToCache(addr, cachedBox) - } - } - return cachedBoxValue.Box, nil + return c.checkIfCredentialsAreRemoved(ctx, addr, cachedBoxValue) } box, err := c.getAccessBox(ctx, addr) @@ -153,6 +136,32 @@ func (c *cred) GetBox(ctx context.Context, addr oid.Address) (*accessbox.Box, er return cachedBox, nil } +func (c *cred) checkIfCredentialsAreRemoved(ctx context.Context, addr oid.Address, cachedBoxValue *cache.AccessBoxCacheValue) (*accessbox.Box, error) { + if time.Since(cachedBoxValue.PutTime) < c.removingCheckDuration { + return cachedBoxValue.Box, nil + } + + box, err := c.getAccessBox(ctx, addr) + if err != nil { + if client.IsErrObjectAlreadyRemoved(err) { + c.cache.Delete(addr) + return nil, fmt.Errorf("get access box: %w", err) + } + return cachedBoxValue.Box, nil + } + + cachedBox, err := box.GetBox(c.key) + if err != nil { + c.cache.Delete(addr) + return nil, fmt.Errorf("get gate box: %w", err) + } + // we need this to reset PutTime + // to don't check for removing each time after removingCheckDuration interval + c.putBoxToCache(addr, cachedBox) + + return cachedBoxValue.Box, nil +} + func (c *cred) putBoxToCache(addr oid.Address, box *accessbox.Box) { if err := c.cache.Put(addr, box); err != nil { c.log.Warn(logs.CouldntPutAccessBoxIntoCache, zap.String("address", addr.EncodeToString()))