From 1b3374ac7f197559dbb7afc51b998fe0619b97ec Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 30 Dec 2022 12:53:41 +0300 Subject: [PATCH] [#5] services/tree: User generic LRU cache Signed-off-by: Evgenii Stratonikov --- pkg/services/tree/cache.go | 10 +++++----- pkg/services/tree/container.go | 9 ++++----- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/pkg/services/tree/cache.go b/pkg/services/tree/cache.go index 8e2d1962..52f3c647 100644 --- a/pkg/services/tree/cache.go +++ b/pkg/services/tree/cache.go @@ -9,7 +9,7 @@ import ( "time" "github.com/TrueCloudLab/frostfs-node/pkg/network" - "github.com/hashicorp/golang-lru/simplelru" + "github.com/hashicorp/golang-lru/v2/simplelru" "google.golang.org/grpc" "google.golang.org/grpc/connectivity" "google.golang.org/grpc/credentials/insecure" @@ -17,7 +17,7 @@ import ( type clientCache struct { sync.Mutex - simplelru.LRU + simplelru.LRU[string, cacheItem] } type cacheItem struct { @@ -34,8 +34,8 @@ const ( var errRecentlyFailed = errors.New("client has recently failed") func (c *clientCache) init() { - l, _ := simplelru.NewLRU(defaultClientCacheSize, func(key, value interface{}) { - _ = value.(*grpc.ClientConn).Close() + l, _ := simplelru.NewLRU[string, cacheItem](defaultClientCacheSize, func(_ string, value cacheItem) { + _ = value.cc.Close() }) c.LRU = *l } @@ -46,7 +46,7 @@ func (c *clientCache) get(ctx context.Context, netmapAddr string) (TreeServiceCl c.Unlock() if ok { - item := ccInt.(cacheItem) + item := ccInt if item.cc == nil { if d := time.Since(item.lastTry); d < defaultReconnectInterval { return nil, fmt.Errorf("%w: %s till the next reconnection to %s", diff --git a/pkg/services/tree/container.go b/pkg/services/tree/container.go index 462c906a..98ec2903 100644 --- a/pkg/services/tree/container.go +++ b/pkg/services/tree/container.go @@ -10,17 +10,17 @@ import ( "github.com/TrueCloudLab/frostfs-node/pkg/services/object_manager/placement" cidSDK "github.com/TrueCloudLab/frostfs-sdk-go/container/id" netmapSDK "github.com/TrueCloudLab/frostfs-sdk-go/netmap" - "github.com/hashicorp/golang-lru/simplelru" + "github.com/hashicorp/golang-lru/v2/simplelru" ) type containerCache struct { sync.Mutex nm *netmapSDK.NetMap - lru *simplelru.LRU + lru *simplelru.LRU[string, containerCacheItem] } func (c *containerCache) init(size int) { - c.lru, _ = simplelru.NewLRU(size, nil) // no error, size is positive + c.lru, _ = simplelru.NewLRU[string, containerCacheItem](size, nil) // no error, size is positive } type containerCacheItem struct { @@ -48,8 +48,7 @@ func (s *Service) getContainerNodes(cid cidSDK.ID) ([]netmapSDK.NodeInfo, int, e s.containerCache.Lock() if s.containerCache.nm != nm { s.containerCache.lru.Purge() - } else if v, ok := s.containerCache.lru.Get(cidStr); ok { - item := v.(containerCacheItem) + } else if item, ok := s.containerCache.lru.Get(cidStr); ok { if item.cnr == cnr { s.containerCache.Unlock() return item.nodes, item.local, nil