[#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 31 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

2
go.sum
View file

@ -31,8 +31,6 @@ github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZx
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bluele/gcache v0.0.2 h1:WcbfdXICg7G/DGBh1PFfcirkWOQV+v077yF1pSy3DGw=
github.com/bluele/gcache v0.0.2/go.mod h1:m15KV+ECjptwSPxKhOhQoAFQVtUFjTVkc3H8o0t/fp0=
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ= github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
github.com/btcsuite/btcd v0.22.0-beta h1:LTDpDKUM5EeOFBPM8IXpinEcmZ6FWfNZbE3lfrfdnWo= github.com/btcsuite/btcd v0.22.0-beta h1:LTDpDKUM5EeOFBPM8IXpinEcmZ6FWfNZbE3lfrfdnWo=
github.com/btcsuite/btcd v0.22.0-beta/go.mod h1:9n5ntfhhHQBIhUvlhDvD3Qg6fRUj4jkN0VB8L8svzOA= github.com/btcsuite/btcd v0.22.0-beta/go.mod h1:9n5ntfhhHQBIhUvlhDvD3Qg6fRUj4jkN0VB8L8svzOA=

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,
} }