Skip to content

Commit

Permalink
util.GCacheObjectPool removed
Browse files Browse the repository at this point in the history
  • Loading branch information
spikeekips committed Jan 11, 2024
1 parent 54fbd46 commit 5642277
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 133 deletions.
4 changes: 2 additions & 2 deletions base/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ func DecodeAddress(s string, enc encoder.Encoder) (Address, error) {
ad, err := decodeAddress(s, enc)
if err != nil {
err = errors.WithMessage(err, "address")
objcache.Set(s, err, nil)
objcache.Set(s, err, 0)

return nil, err
}

objcache.Set(s, ad, nil)
objcache.Set(s, ad, 0)

return ad, nil
}
Expand Down
6 changes: 3 additions & 3 deletions base/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package base

import "github.com/spikeekips/mitum/util"

var objcache *util.GCacheObjectPool
var objcache util.GCache[string, any]

func init() {
objcache = util.NewGCacheObjectPool(1 << 13) //nolint:gomnd //...
objcache = util.NewLRUGCache[string, any](1 << 13) //nolint:gomnd //...
}

func SetObjCache(c *util.GCacheObjectPool) {
func SetObjCache(c util.GCache[string, any]) {
objcache = c
}
4 changes: 2 additions & 2 deletions base/pk.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ func DecodePublickeyFromString(s string, enc encoder.Encoder) (Publickey, error)

pub, err := decodePublickeyFromString(s, enc)
if err != nil {
objcache.Set(s, err, nil)
objcache.Set(s, err, 0)

return nil, e.Wrap(err)
}

objcache.Set(s, pub, nil)
objcache.Set(s, pub, 0)

return pub, nil
}
Expand Down
17 changes: 8 additions & 9 deletions isaac/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"sync"
"time"

"github.com/bluele/gcache"
"github.com/pkg/errors"
"github.com/spikeekips/mitum/base"
"github.com/spikeekips/mitum/network/quicstream"
Expand All @@ -31,7 +30,7 @@ type Syncer interface {
}

type SyncSourcePool struct {
problems gcache.Cache
problems util.GCache[string, any]
nonfixed map[string]NodeConnInfo
fixed []NodeConnInfo
fixedids []string
Expand All @@ -42,8 +41,8 @@ type SyncSourcePool struct {
func NewSyncSourcePool(fixed []NodeConnInfo) *SyncSourcePool {
p := &SyncSourcePool{
nonfixed: map[string]NodeConnInfo{},
problems: gcache.New(1 << 14).LRU().Build(), //nolint:gomnd // big enough for suffrage size
renewTimeout: time.Second * 3, //nolint:gomnd //...
problems: util.NewLRUGCache[string, any](1 << 14), //nolint:gomnd // big enough for suffrage size
renewTimeout: time.Second * 3, //nolint:gomnd //...
}

_ = p.UpdateFixed(fixed)
Expand Down Expand Up @@ -306,7 +305,7 @@ func (p *SyncSourcePool) Actives(f func(NodeConnInfo) bool) {
defer p.RUnlock()

for i := range p.fixedids {
if p.problems.Has(p.fixedids[i]) {
if p.problems.Exists(p.fixedids[i]) {
continue
}

Expand All @@ -316,7 +315,7 @@ func (p *SyncSourcePool) Actives(f func(NodeConnInfo) bool) {
}

for id := range p.nonfixed {
if p.problems.Has(id) {
if p.problems.Exists(id) {
continue
}

Expand All @@ -343,7 +342,7 @@ func (p *SyncSourcePool) pick(skipid string) (_ string, _ NodeConnInfo, report f
continue
case !foundid:
continue
case p.problems.Has(id):
case p.problems.Exists(id):
continue
default:
return id, p.fixed[i], func(err error) { p.reportProblem(id, err) }, nil
Expand All @@ -352,7 +351,7 @@ func (p *SyncSourcePool) pick(skipid string) (_ string, _ NodeConnInfo, report f

for id := range p.nonfixed {
switch {
case skipid == id, p.problems.Has(id):
case skipid == id, p.problems.Exists(id):
continue
default:
return id, p.nonfixed[id], func(err error) { p.reportProblem(id, err) }, nil
Expand All @@ -376,7 +375,7 @@ func (p *SyncSourcePool) reportProblem(id string, err error) {
}
}

_ = p.problems.SetWithExpire(id, struct{}{}, p.renewTimeout)
p.problems.Set(id, nil, p.renewTimeout)
}

func (p *SyncSourcePool) NodeConnInfo(node base.Address) []NodeConnInfo {
Expand Down
2 changes: 1 addition & 1 deletion launch/p_design.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func PINITObjectCache(pctx context.Context) (context.Context, error) {

cachesize := design.LocalParams.MISC.ObjectCacheSize()

base.SetObjCache(util.NewGCacheObjectPool(int(cachesize)))
base.SetObjCache(util.NewLRUGCache[string, any](int(cachesize)))

log.Log().Debug().Uint64("cache_size", cachesize).Msg("set object cache size")

Expand Down
13 changes: 6 additions & 7 deletions network/quicmemberlist/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net"
"time"

"github.com/bluele/gcache"
"github.com/hashicorp/memberlist"
"github.com/pkg/errors"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -88,7 +87,7 @@ type AliveDelegate struct {
allowf DelegateNodeFunc // NOTE allowf controls which node can be entered or not
storeconninfof DelegateStoreConnInfo
challengef DelegateNodeFunc
challengecache gcache.Cache
challengecache util.GCache[string, time.Time]
challengeexpire time.Duration
}

Expand Down Expand Up @@ -117,7 +116,7 @@ func NewAliveDelegate(
challengef: nchallengef,
allowf: nallowf,
storeconninfof: func(quicstream.ConnInfo) {},
challengecache: gcache.New(1 << 9).LRU().Build(), //nolint:gomnd //...
challengecache: util.NewLRUGCache[string, time.Time](1 << 9), //nolint:gomnd //...
challengeexpire: defaultNodeChallengeExpire,
}
}
Expand All @@ -138,20 +137,20 @@ func (d *AliveDelegate) NotifyAlive(peer *memberlist.Node) error {

memberkey := member.Addr().String()

switch i, err := d.challengecache.Get(memberkey); {
case err != nil && errors.Is(err, gcache.KeyNotFoundError):
switch i, found := d.challengecache.Get(memberkey); {
case !found:
// NOTE challenge with member publickey
willchallenge = true
default:
willchallenge = time.Now().After(i.(time.Time).Add(d.challengeexpire)) //nolint:forcetypeassert //...
willchallenge = time.Now().After(i.Add(d.challengeexpire)) //nolint:forcetypeassert //...
}

if willchallenge {
if err := d.challengef(member); err != nil {
return err
}

_ = d.challengecache.SetWithExpire(memberkey, time.Now(), d.challengeexpire)
d.challengecache.Set(memberkey, time.Now(), d.challengeexpire)
}

l := d.Log().With().Interface("member", member).Logger()
Expand Down
10 changes: 3 additions & 7 deletions network/quicmemberlist/memberlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"sync"
"time"

"github.com/bluele/gcache"
"github.com/hashicorp/memberlist"
"github.com/pkg/errors"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -587,10 +586,7 @@ func (srv *Memberlist) patch(config *memberlist.Config) error { // revive:disabl
return errors.Errorf("alive delegate missing")
}

notallowedcache := gcache.New(1 << 9).LRU().Build() //nolint:gomnd //...
setnotallowedcache := func(addr string) {
_ = notallowedcache.SetWithExpire(addr, nil, srv.args.NotAllowedMemberExpire)
}
notallowedcache := util.NewLRUGCache[string, any](1 << 9) //nolint:gomnd //...

switch i, ok := config.Transport.(*Transport); {
case !ok:
Expand All @@ -606,7 +602,7 @@ func (srv *Memberlist) patch(config *memberlist.Config) error { // revive:disabl
}

i.args.NotAllowFunc = func(addr string) bool {
return notallowedcache.Has(addr)
return notallowedcache.Exists(addr)
}
}

Expand Down Expand Up @@ -638,7 +634,7 @@ func (srv *Memberlist) patch(config *memberlist.Config) error { // revive:disabl
if err != nil {
srv.Log().Trace().Err(err).Interface("member", member).Msg("set not allowed")

setnotallowedcache(member.Addr().String())
notallowedcache.Set(member.Addr().String(), nil, srv.args.NotAllowedMemberExpire)
}

return err
Expand Down
6 changes: 3 additions & 3 deletions util/encoder/json/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var JSONEncoderHint = hint.MustNewHint("json-encoder-v0.0.1")

type Encoder struct {
decoders *hint.CompatibleSet[encoder.DecodeDetail]
pool util.ObjectPool
pool util.GCache[string, any]
}

func NewEncoder() *Encoder {
Expand All @@ -29,7 +29,7 @@ func (*Encoder) Hint() hint.Hint {
return JSONEncoderHint
}

func (enc *Encoder) SetPool(pool util.ObjectPool) *Encoder {
func (enc *Encoder) SetPool(pool util.GCache[string, any]) *Encoder {
enc.pool = pool

return nil
Expand Down Expand Up @@ -301,7 +301,7 @@ func (enc *Encoder) poolSet(s string, v interface{}) {
return
}

enc.pool.Set(s, v, nil)
enc.pool.Set(s, v, 0)
}

func (*Encoder) analyzeExtensible(d encoder.DecodeDetail, ptr reflect.Value) encoder.DecodeDetail {
Expand Down
4 changes: 2 additions & 2 deletions util/encoder/json/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (t *testJSONEncoder) TestDecodeWithFixedHintType() {
}

func (t *testJSONEncoder) TestDecodeWithFixedHintTypePool() {
pool := util.NewGCacheObjectPool(10)
pool := util.NewLRUGCache[string, any](10)
_ = t.enc.SetPool(pool)

ht := hint.MustNewHint("findme-v1.2.3")
Expand Down Expand Up @@ -258,7 +258,7 @@ func (t *testJSONEncoder) TestDecodeWithFixedHintTypePool() {
}

func (t *testJSONEncoder) TestDecodeWithFixedHintTypePoolError() {
pool := util.NewGCacheObjectPool(10)
pool := util.NewLRUGCache[string, any](10)
_ = t.enc.SetPool(pool)

ht := hint.MustNewHint("findme-v1.2.3")
Expand Down
8 changes: 4 additions & 4 deletions util/hint/hint.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ var (
regVersion = regexp.MustCompile(`\-v\d+`)
)

var hintcache *util.GCacheObjectPool
var hintcache util.GCache[string, any]

func init() {
hintcache = util.NewGCacheObjectPool(1 << 13) //nolint:gomnd //...
hintcache = util.NewLRUGCache[string, any](1 << 13) //nolint:gomnd //...
}

type Hinter interface {
Expand Down Expand Up @@ -63,12 +63,12 @@ func ParseHint(s string) (Hint, error) {

ht, err := parseHint(s)
if err != nil {
hintcache.Set(s, err, nil)
hintcache.Set(s, err, 0)

return Hint{}, err
}

hintcache.Set(s, &ht, nil)
hintcache.Set(s, &ht, 0)

return ht, nil
}
Expand Down
55 changes: 0 additions & 55 deletions util/pool.go

This file was deleted.

38 changes: 0 additions & 38 deletions util/pool_test.go

This file was deleted.

0 comments on commit 5642277

Please sign in to comment.