[#38] Replace gcache with golang-lru

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2021-11-08 16:28:28 +03:00 committed by Alex Vanin
parent 1d546711e5
commit 818f38b811
6 changed files with 32 additions and 29 deletions

3
go.mod
View file

@ -4,13 +4,14 @@ go 1.16
require (
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20210521073959-f0d4d129b7f1
github.com/bluele/gcache v0.0.2
github.com/golang/mock v1.6.0
github.com/google/uuid v1.2.0
github.com/hashicorp/golang-lru v0.5.4
github.com/mr-tron/base58 v1.2.0
github.com/nspcc-dev/hrw v1.0.9
github.com/nspcc-dev/neo-go v0.96.1
github.com/nspcc-dev/neofs-api-go v1.30.0
github.com/nspcc-dev/neofs-crypto v0.3.0
github.com/stretchr/testify v1.7.0
go.uber.org/zap v1.18.1
google.golang.org/grpc v1.41.0

BIN
go.sum

Binary file not shown.

View file

@ -3,44 +3,38 @@ package pool
import (
"strings"
"github.com/bluele/gcache"
"github.com/nspcc-dev/neofs-api-go/pkg/session"
lru "github.com/hashicorp/golang-lru"
"github.com/nspcc-dev/neofs-sdk-go/session"
)
type SessionCache struct {
cache gcache.Cache
cache *lru.Cache
}
func NewCache() *SessionCache {
return &SessionCache{
cache: gcache.New(100).Build(),
func NewCache() (*SessionCache, error) {
cache, err := lru.New(100)
if err != nil {
return nil, err
}
return &SessionCache{cache: cache}, nil
}
func (c *SessionCache) Get(key string) *session.Token {
tokenRaw, err := c.cache.Get(key)
if err != nil {
return nil
}
token, ok := tokenRaw.(*session.Token)
tokenRaw, ok := c.cache.Get(key)
if !ok {
return nil
}
return token
return tokenRaw.(*session.Token)
}
func (c *SessionCache) Put(key string, token *session.Token) error {
return c.cache.Set(key, token)
func (c *SessionCache) Put(key string, token *session.Token) bool {
return c.cache.Add(key, token)
}
func (c *SessionCache) DeleteByPrefix(prefix string) {
for _, key := range c.cache.Keys(false) {
keyStr, ok := key.(string)
if !ok {
continue
}
if strings.HasPrefix(keyStr, prefix) {
for _, key := range c.cache.Keys() {
if strings.HasPrefix(key.(string), prefix) {
c.cache.Remove(key)
}
}

View file

@ -142,7 +142,10 @@ type pool struct {
}
func newPool(ctx context.Context, options *BuilderOptions) (Pool, error) {
cache := NewCache()
cache, err := NewCache()
if err != nil {
return nil, fmt.Errorf("couldn't create cache: %w", err)
}
clientPacks := make([]*clientPack, len(options.weights))
var atLeastOneHealthy bool

View file

@ -309,7 +309,6 @@ func TestTwoFailed(t *testing.T) {
func TestSessionCache(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
var tokens []*session.Token
clientBuilder := func(opts ...client.Option) (client.Client, error) {
@ -339,6 +338,7 @@ func TestSessionCache(t *testing.T) {
opts := &BuilderOptions{
Key: &key.PrivateKey,
clientBuilder: clientBuilder,
ClientRebalanceInterval: 30 * time.Second,
}
ctx, cancel := context.WithCancel(context.Background())
@ -371,7 +371,6 @@ func TestSessionCache(t *testing.T) {
func TestSessionCacheWithKey(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
var tokens []*session.Token
clientBuilder := func(opts ...client.Option) (client.Client, error) {
@ -439,6 +438,9 @@ func TestWaitPresence(t *testing.T) {
key, err := keys.NewPrivateKey()
require.NoError(t, err)
cache, err := NewCache()
require.NoError(t, err)
p := &pool{
sampler: NewSampler([]float64{1}, rand.NewSource(0)),
clientPacks: []*clientPack{{
@ -446,7 +448,7 @@ func TestWaitPresence(t *testing.T) {
healthy: true,
}},
key: &key.PrivateKey,
cache: NewCache(),
cache: cache,
}
t.Run("context canceled", func(t *testing.T) {

View file

@ -77,12 +77,15 @@ func TestHealthyReweight(t *testing.T) {
key, err := keys.NewPrivateKey()
require.NoError(t, err)
cache, err := NewCache()
require.NoError(t, err)
p := &pool{
sampler: NewSampler(weights, rand.NewSource(0)),
clientPacks: []*clientPack{
{client: newNetmapMock(names[0], true), healthy: true, address: "address0"},
{client: newNetmapMock(names[1], false), healthy: true, address: "address1"}},
cache: NewCache(),
cache: cache,
key: &key.PrivateKey,
}