[#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 ( require (
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20210521073959-f0d4d129b7f1 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/golang/mock v1.6.0
github.com/google/uuid v1.2.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/mr-tron/base58 v1.2.0
github.com/nspcc-dev/hrw v1.0.9 github.com/nspcc-dev/hrw v1.0.9
github.com/nspcc-dev/neo-go v0.96.1 github.com/nspcc-dev/neo-go v0.96.1
github.com/nspcc-dev/neofs-api-go v1.30.0 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 github.com/stretchr/testify v1.7.0
go.uber.org/zap v1.18.1 go.uber.org/zap v1.18.1
google.golang.org/grpc v1.41.0 google.golang.org/grpc v1.41.0

BIN
go.sum

Binary file not shown.

View file

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

View file

@ -142,7 +142,10 @@ type pool struct {
} }
func newPool(ctx context.Context, options *BuilderOptions) (Pool, error) { 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)) clientPacks := make([]*clientPack, len(options.weights))
var atLeastOneHealthy bool var atLeastOneHealthy bool

View file

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

View file

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