[#5] services/tree: User generic LRU cache

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2022-12-30 12:53:41 +03:00 committed by fyrchik
parent d0a0432a51
commit 1b3374ac7f
2 changed files with 9 additions and 10 deletions

View file

@ -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",

View file

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