From cc34f659d17c314cc3273023e874b6c73d1744fe Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Fri, 9 Feb 2024 09:40:40 +0300 Subject: [PATCH] [#305] Extract removal checking into separate method Signed-off-by: Denis Kirillov --- creds/tokens/credentials.go | 45 ++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 18 deletions(-) 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()))