[#305] Extract removal checking into separate method
/ DCO (pull_request) Successful in 2m5s Details
/ Builds (1.20) (pull_request) Successful in 2m18s Details
/ Builds (1.21) (pull_request) Successful in 1m50s Details
/ Vulncheck (pull_request) Successful in 2m6s Details
/ Lint (pull_request) Failing after 2m53s Details
/ Tests (1.20) (pull_request) Successful in 2m35s Details
/ Tests (1.21) (pull_request) Successful in 2m39s Details

Signed-off-by: Denis Kirillov <d.kirillov@yadro.com>
pull/305/head
Denis Kirillov 2024-02-09 09:40:40 +03:00
parent 924e87face
commit cc34f659d1
1 changed files with 27 additions and 18 deletions

View File

@ -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()))