-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache headers and logs using accepted depth instead of LRU (#387)
* save progress * save progress * saving more progress * add more context * more comments * fix items * fix removal * passs config * add additional comment * move cache init * remove TODO * fix logs format * nits * insert into cache oldest first * make bounded buffer generic * cleanup FIFO cache * cleanup comments * simplify bounded buffer * fix `Last` * nits * more comment nits * fix min size of FIFOCache * add NoOpFIFOCache * move `FlattenLogs` to `types` * fix off by 1 * revert LastBloomIndex * fifo cache nits * simplify bounded buffer * move acceptedHeaderCache to headerchain * remove unnecessary comment
- Loading branch information
1 parent
a54eae2
commit e3cbd01
Showing
16 changed files
with
263 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// (c) 2021, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package core | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
var ( | ||
_ FIFOCache[int, int] = (*BufferFIFOCache[int, int])(nil) | ||
_ FIFOCache[int, int] = (*NoOpFIFOCache[int, int])(nil) | ||
) | ||
|
||
// FIFOCache evicts the oldest element added to it after [limit] items are | ||
// added. | ||
type FIFOCache[K comparable, V any] interface { | ||
Put(K, V) | ||
Get(K) (V, bool) | ||
} | ||
|
||
// NewFIFOCache creates a new First-In-First-Out cache of size [limit]. | ||
// | ||
// If a [limit] of 0 is passed as an argument, a no-op cache is returned that | ||
// does nothing. | ||
func NewFIFOCache[K comparable, V any](limit int) FIFOCache[K, V] { | ||
if limit <= 0 { | ||
return &NoOpFIFOCache[K, V]{} | ||
} | ||
|
||
c := &BufferFIFOCache[K, V]{ | ||
m: make(map[K]V, limit), | ||
} | ||
c.buffer = NewBoundedBuffer(limit, c.remove) | ||
return c | ||
} | ||
|
||
type BufferFIFOCache[K comparable, V any] struct { | ||
l sync.RWMutex | ||
|
||
buffer *BoundedBuffer[K] | ||
m map[K]V | ||
} | ||
|
||
func (f *BufferFIFOCache[K, V]) Put(key K, val V) { | ||
f.l.Lock() | ||
defer f.l.Unlock() | ||
|
||
f.buffer.Insert(key) // Insert will remove the oldest [K] if we are at the [limit] | ||
f.m[key] = val | ||
} | ||
|
||
func (f *BufferFIFOCache[K, V]) Get(key K) (V, bool) { | ||
f.l.RLock() | ||
defer f.l.RUnlock() | ||
|
||
v, ok := f.m[key] | ||
return v, ok | ||
} | ||
|
||
// remove is used as the callback in [BoundedBuffer]. It is assumed that the | ||
// [WriteLock] is held when this is accessed. | ||
func (f *BufferFIFOCache[K, V]) remove(key K) { | ||
delete(f.m, key) | ||
} | ||
|
||
type NoOpFIFOCache[K comparable, V any] struct{} | ||
|
||
func (f *NoOpFIFOCache[K, V]) Put(_ K, _ V) {} | ||
func (f *NoOpFIFOCache[K, V]) Get(_ K) (V, bool) { | ||
return *new(V), false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.