Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ctx management suggestion #5

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions data/txDupCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,21 @@ type txSaltedCache struct {
ctx context.Context
}

func makeSaltedCache(ctx context.Context, size int, refreshInterval time.Duration) *txSaltedCache {
c := &txSaltedCache{
digestCache: digestCache{
cur: map[crypto.Digest]struct{}{},
maxSize: size,
},
ctx: ctx,
func makeSaltedCache(size int) *txSaltedCache {
return &txSaltedCache{
digestCache: *makeDigestCache(size),
}
}

func (c *txSaltedCache) start(ctx context.Context, refreshInterval time.Duration) {
c.ctx = ctx
if refreshInterval != 0 {
go c.salter(refreshInterval)
}

c.mu.Lock()
defer c.mu.Unlock()
c.moreSalt()

return c
}

// salter is a goroutine refreshing the cache by schedule
Expand Down
13 changes: 9 additions & 4 deletions data/txDupCache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ func TestTxHandlerSaltedCacheBasic(t *testing.T) {
t.Parallel()

const size = 20
cache := makeSaltedCache(context.Background(), size, 0)
cache := makeSaltedCache(size)
cache.start(context.Background(), 0)
require.Zero(t, cache.Len())

// add some unique random
Expand Down Expand Up @@ -202,7 +203,8 @@ func TestTxHandlerSaltedCacheScheduled(t *testing.T) {

const size = 20
updateInterval := 1000 * time.Microsecond
cache := makeSaltedCache(context.Background(), size, updateInterval)
cache := makeSaltedCache(size)
cache.start(context.Background(), updateInterval)
require.Zero(t, cache.Len())

// add some unique random
Expand All @@ -226,7 +228,8 @@ func TestTxHandlerSaltedCacheManual(t *testing.T) {
t.Parallel()

const size = 20
cache := makeSaltedCache(context.Background(), 2*size, 0)
cache := makeSaltedCache(2 * size)
cache.start(context.Background(), 0)
require.Zero(t, cache.Len())

// add some unique random
Expand Down Expand Up @@ -290,7 +293,9 @@ func (m digestCacheMaker) make(size int) cachePusher {
return &digestCachePusher{c: makeDigestCache(size)}
}
func (m saltedCacheMaker) make(size int) cachePusher {
return &saltedCachePusher{c: makeSaltedCache(context.Background(), size, 0)}
scp := &saltedCachePusher{c: makeSaltedCache(size)}
scp.c.start(context.Background(), 0)
return scp
}

type digestCachePusher struct {
Expand Down
12 changes: 3 additions & 9 deletions data/txHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ func MakeTxHandler(opts TxHandlerOpts) *TxHandler {
return nil
}

ctx, ctxCancel := context.WithCancel(context.Background())
handler := &TxHandler{
txPool: opts.TxPool,
genesisID: opts.GenesisID,
Expand All @@ -133,22 +132,17 @@ func MakeTxHandler(opts TxHandlerOpts) *TxHandler {
backlogQueue: make(chan *txBacklogMsg, txBacklogSize),
postVerificationQueue: make(chan *txBacklogMsg, txBacklogSize),
net: opts.Net,
msgCache: makeSaltedCache(ctx, 2*txBacklogSize, 60*time.Second),
msgCache: makeSaltedCache(2 * txBacklogSize),
txCanonicalCache: makeDigestCache(2 * txBacklogSize),
cacheConfig: txHandlerConfig{opts.Config.TxFilterRawMsgEnabled(), opts.Config.TxFilterCanonicalEnabled()},
ctx: ctx,
ctxCancel: ctxCancel,
}
return handler
}

// Start enables the processing of incoming messages at the transaction handler
func (handler *TxHandler) Start() {
if handler.ctx.Err() != nil {
// existing context has gone, create a new one
handler.ctx, handler.ctxCancel = context.WithCancel(context.Background())
handler.msgCache = makeSaltedCache(handler.ctx, 2*txBacklogSize, 60*time.Second)
}
handler.ctx, handler.ctxCancel = context.WithCancel(context.Background())
handler.msgCache.start(handler.ctx, 60*time.Second)
handler.net.RegisterHandlers([]network.TaggedMessageHandler{
{Tag: protocol.TxnTag, MessageHandler: network.HandlerFunc(handler.processIncomingTxn)},
})
Expand Down
6 changes: 4 additions & 2 deletions data/txHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,12 +725,14 @@ func makeTestTxHandlerOrphanedWithContext(ctx context.Context, backlogSize int,
if cacheSize <= 0 {
cacheSize = txBacklogSize
}
return &TxHandler{
handler := &TxHandler{
backlogQueue: make(chan *txBacklogMsg, backlogSize),
msgCache: makeSaltedCache(ctx, cacheSize, refreshInterval),
msgCache: makeSaltedCache(cacheSize),
txCanonicalCache: makeDigestCache(cacheSize),
cacheConfig: txHandlerConfig{true, true},
}
handler.msgCache.start(ctx, refreshInterval)
return handler
}

func makeTestTxHandler(dl *Ledger, cfg config.Local) *TxHandler {
Expand Down