forked from TrueCloudLab/frostfs-sdk-go
[#38] Replace gcache with golang-lru
Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
parent
1d546711e5
commit
818f38b811
6 changed files with 32 additions and 31 deletions
3
go.mod
3
go.mod
|
@ -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
|
||||
|
|
2
go.sum
2
go.sum
|
@ -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 v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
|
||||
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.22.0-beta h1:LTDpDKUM5EeOFBPM8IXpinEcmZ6FWfNZbE3lfrfdnWo=
|
||||
github.com/btcsuite/btcd v0.22.0-beta/go.mod h1:9n5ntfhhHQBIhUvlhDvD3Qg6fRUj4jkN0VB8L8svzOA=
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
@ -337,8 +336,9 @@ func TestSessionCache(t *testing.T) {
|
|||
pb.AddNode("peer0", 1)
|
||||
|
||||
opts := &BuilderOptions{
|
||||
Key: &key.PrivateKey,
|
||||
clientBuilder: clientBuilder,
|
||||
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) {
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue