mirror of
https://github.com/aljazceru/lspd.git
synced 2025-12-19 14:54:22 +01:00
add a cached chainfee estimator
This commit is contained in:
61
chain/cached_fee_estimator.go
Normal file
61
chain/cached_fee_estimator.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package chain
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var cacheDuration time.Duration = time.Minute * 5
|
||||
|
||||
type feeCache struct {
|
||||
time time.Time
|
||||
estimation *FeeEstimation
|
||||
}
|
||||
|
||||
type CachedFeeEstimator struct {
|
||||
cache map[FeeStrategy]*feeCache
|
||||
inner FeeEstimator
|
||||
mtx sync.Mutex
|
||||
}
|
||||
|
||||
func NewCachedFeeEstimator(inner FeeEstimator) *CachedFeeEstimator {
|
||||
return &CachedFeeEstimator{
|
||||
inner: inner,
|
||||
cache: make(map[FeeStrategy]*feeCache),
|
||||
}
|
||||
}
|
||||
|
||||
func (e *CachedFeeEstimator) EstimateFeeRate(
|
||||
ctx context.Context,
|
||||
strategy FeeStrategy,
|
||||
) (*FeeEstimation, error) {
|
||||
|
||||
// Make sure we're in a lock, because we're reading/writing a map.
|
||||
e.mtx.Lock()
|
||||
defer e.mtx.Unlock()
|
||||
|
||||
// See if there's a cached value first.
|
||||
cached, ok := e.cache[strategy]
|
||||
|
||||
// If there is and it's still valid, return that.
|
||||
if ok && cached.time.Add(cacheDuration).After(time.Now()) {
|
||||
return cached.estimation, nil
|
||||
}
|
||||
|
||||
// There was no valid cache.
|
||||
// Fetch the new fee estimate.
|
||||
now := time.Now()
|
||||
estimation, err := e.inner.EstimateFeeRate(ctx, strategy)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Cache it.
|
||||
e.cache[strategy] = &feeCache{
|
||||
time: now,
|
||||
estimation: estimation,
|
||||
}
|
||||
|
||||
return estimation, nil
|
||||
}
|
||||
Reference in New Issue
Block a user