Skip to content

Commit

Permalink
remove lru logic from counter cache to simplify things
Browse files Browse the repository at this point in the history
  • Loading branch information
Termina1 committed Oct 10, 2024
1 parent 1893cea commit b7db9b0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 25 deletions.
27 changes: 7 additions & 20 deletions chotki.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/drpcorg/chotki/protocol"
"github.com/drpcorg/chotki/rdx"
"github.com/drpcorg/chotki/utils"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/puzpuzpuz/xsync/v3"
)
Expand Down Expand Up @@ -85,7 +84,6 @@ type Options struct {
ReadMaxBufferSize int
TcpReadBufferSize int
TcpWriteBufferSize int
CounterCacheSize int

TlsConfig *tls.Config
}
Expand All @@ -95,10 +93,6 @@ func (o *Options) SetDefaults() {
o.MaxLogLen = 1 << 23
}

if o.CounterCacheSize == 0 {
o.CounterCacheSize = 1000
}

if o.PingPeriod == 0 {
o.PingPeriod = 30 * time.Second
}
Expand Down Expand Up @@ -164,7 +158,7 @@ type Chotki struct {
dir string
opts Options
log utils.Logger
counterCache *lru.Cache[rdx.ID, *AtomicCounter]
counterCache sync.Map

outq *xsync.MapOf[string, protocol.DrainCloser] // queues to broadcast all new packets
syncs *xsync.MapOf[rdx.ID, *pebble.Batch]
Expand Down Expand Up @@ -212,10 +206,6 @@ func Open(dirname string, opts Options) (*Chotki, error) {
return nil, err
}

lruCache, err := lru.New[rdx.ID, *AtomicCounter](opts.CounterCacheSize)
if err != nil {
return nil, err
}
cho := Chotki{
db: db,
src: opts.Src,
Expand All @@ -224,11 +214,10 @@ func Open(dirname string, opts Options) (*Chotki, error) {
opts: opts,
clock: &rdx.LocalLogicalClock{Source: opts.Src},

outq: xsync.NewMapOf[string, protocol.DrainCloser](),
syncs: xsync.NewMapOf[rdx.ID, *pebble.Batch](),
hooks: xsync.NewMapOf[rdx.ID, []Hook](),
types: xsync.NewMapOf[rdx.ID, Fields](),
counterCache: lruCache,
outq: xsync.NewMapOf[string, protocol.DrainCloser](),
syncs: xsync.NewMapOf[rdx.ID, *pebble.Batch](),
hooks: xsync.NewMapOf[rdx.ID, []Hook](),
types: xsync.NewMapOf[rdx.ID, Fields](),
}

cho.net = protocol.NewNet(cho.log,
Expand Down Expand Up @@ -327,10 +316,8 @@ func (cho *Chotki) Close() error {
}

func (cho *Chotki) Counter(rid rdx.ID, offset uint64, updatePeriod time.Duration) *AtomicCounter {
counter, _, _ := cho.counterCache.PeekOrAdd(rid.ToOff(offset),
NewAtomicCounter(cho, rid, offset, updatePeriod))
cho.counterCache.Get(rid.ToOff(offset))
return counter
counter, _ := cho.counterCache.LoadOrStore(rid.ToOff(offset), NewAtomicCounter(cho, rid, offset, updatePeriod))
return counter.(*AtomicCounter)
}

func (cho *Chotki) KeepAliveLoop() {
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ go 1.21.4
require (
github.com/cockroachdb/pebble v1.1.0
github.com/ergochat/readline v0.1.0
github.com/google/uuid v1.6.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.12.0
github.com/puzpuzpuz/xsync/v3 v3.1.0
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df
Expand All @@ -25,14 +27,11 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.0 // indirect
github.com/prometheus/client_model v0.2.1-0.20210607210712-147c58e9608a // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
Expand Down

0 comments on commit b7db9b0

Please sign in to comment.