Skip to content

Commit

Permalink
Try to fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Sep 8, 2022
1 parent 66f458a commit e2d2231
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
50 changes: 36 additions & 14 deletions cmd/mempool/cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"sync"
"time"
)
Expand All @@ -9,35 +10,27 @@ import (
type Cache struct {
mux sync.RWMutex
lookup map[string]int64
ticker *time.Ticker
ttl time.Duration

wg sync.WaitGroup
}

// NewCache -
func NewCache(ttl time.Duration) *Cache {
return &Cache{
lookup: make(map[string]int64),
ttl: ttl,
ticker: time.NewTicker(time.Minute),
}
}

// Has -
func (c *Cache) Has(key string) bool {
c.mux.RLock()
expires, ok := c.lookup[key]
_, ok := c.lookup[key]
c.mux.RUnlock()

if !ok {
return false
}

if time.Now().UnixNano() > expires {
c.mux.Lock()
delete(c.lookup, key)
c.mux.Unlock()
return false
}

return true
return ok
}

// Set -
Expand All @@ -47,3 +40,32 @@ func (c *Cache) Set(key string) {
c.lookup[key] = expires
c.mux.Unlock()
}

// Start -
func (c *Cache) Start(ctx context.Context) {
c.wg.Add(1)
go c.checkExpiration(ctx)
}

func (c *Cache) checkExpiration(ctx context.Context) {
defer c.wg.Done()

for {
select {
case <-ctx.Done():
c.ticker.Stop()
return
case <-c.ticker.C:
c.mux.RLock()
for key, expiration := range c.lookup {
if time.Now().UnixNano() <= expiration {
continue
}
c.mux.Lock()
delete(c.lookup, key)
c.mux.Unlock()
}
c.mux.RUnlock()
}
}
}
5 changes: 1 addition & 4 deletions cmd/mempool/endorsement/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,9 @@ func getCoordinates(data []byte) (*big.Int, *big.Int, error) {

// The equation is y^2 = x^3 - 3x + b
// x^3, mod P
xCubed := new(big.Int).Exp(x, three, c.P)

xCubed := new(big.Int).Exp(x, three, nil)
// 3x, mod P
threeX := new(big.Int).Mul(x, three)
threeX.Mod(threeX, c.P)

// x^3 - 3x
ySquared := new(big.Int).Sub(xCubed, threeX)

Expand Down
8 changes: 8 additions & 0 deletions cmd/mempool/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func NewIndexer(ctx context.Context, network string, indexerCfg config.Indexer,
endorsements: make(chan *models.Endorsement, 1024*32),
rights: ccache.New(ccache.Configure().MaxSize(60)),
}
indexer.cache.Start(ctx)

indexer.state = &database.State{
IndexType: models.IndexTypeMempool,
Expand Down Expand Up @@ -263,6 +264,9 @@ func (indexer *Indexer) listen(ctx context.Context) {
indexer.error().Msgf("invalid applied operation %v", applied)
continue
}
if !indexer.branches.Contains(applied.Branch) {
continue
}
if indexer.isHashProcessed(applied.Hash) {
continue
}
Expand All @@ -276,6 +280,10 @@ func (indexer *Indexer) listen(ctx context.Context) {
indexer.error().Msgf("invalid %s operation %v", msg.Status, failed)
continue
}

if !indexer.branches.Contains(failed.Branch) {
continue
}
if indexer.isHashProcessed(failed.Hash) {
continue
}
Expand Down

0 comments on commit e2d2231

Please sign in to comment.