[#1507] timer: Remove unused OnDelta() method

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2024-11-20 10:43:38 +03:00 committed by Evgenii Stratonikov
parent a339b52a60
commit 3042490340
2 changed files with 17 additions and 198 deletions

View file

@ -15,41 +15,19 @@ type BlockTickHandler func()
// It can tick the blocks and perform certain actions
// on block time intervals.
type BlockTimer struct {
rolledBack bool
mtx sync.Mutex
dur BlockMeter
baseDur uint32
mul, div uint32
cur, tgt uint32
last uint32
h BlockTickHandler
ps []BlockTimer
once bool
deltaCfg
}
// DeltaOption is an option of delta-interval handler.
type DeltaOption func(*deltaCfg)
type deltaCfg struct {
pulse bool
}
// WithPulse returns option to call delta-interval handler multiple times.
func WithPulse() DeltaOption {
return func(c *deltaCfg) {
c.pulse = true
}
}
// StaticBlockMeter returns BlockMeters that always returns (d, nil).
@ -65,52 +43,19 @@ func StaticBlockMeter(d uint32) BlockMeter {
func NewBlockTimer(dur BlockMeter, h BlockTickHandler) *BlockTimer {
return &BlockTimer{
dur: dur,
mul: 1,
div: 1,
h: h,
deltaCfg: deltaCfg{
pulse: true,
},
}
}
// NewOneTickTimer creates a new BlockTimer that ticks only once.
//
// Do not use delta handlers with pulse in this timer.
func NewOneTickTimer(dur BlockMeter, h BlockTickHandler) *BlockTimer {
return &BlockTimer{
dur: dur,
mul: 1,
div: 1,
h: h,
once: true,
}
}
// OnDelta registers handler which is executed on (mul / div * BlockMeter()) block
// after basic interval reset.
//
// If WithPulse option is provided, handler is executed (mul / div * BlockMeter()) block
// during base interval.
func (t *BlockTimer) OnDelta(mul, div uint32, h BlockTickHandler, opts ...DeltaOption) {
c := deltaCfg{
pulse: false,
}
for i := range opts {
opts[i](&c)
}
t.ps = append(t.ps, BlockTimer{
mul: mul,
div: div,
h: h,
once: t.once,
deltaCfg: c,
})
}
// Reset resets previous ticks of the BlockTimer.
//
// Returns BlockMeter's error upon occurrence.
@ -124,29 +69,18 @@ func (t *BlockTimer) Reset() error {
t.resetWithBaseInterval(d)
for i := range t.ps {
t.ps[i].resetWithBaseInterval(d)
}
t.mtx.Unlock()
return nil
}
func (t *BlockTimer) resetWithBaseInterval(d uint32) {
t.rolledBack = false
t.baseDur = d
t.reset()
}
func (t *BlockTimer) reset() {
mul, div := t.mul, t.div
if !t.pulse && t.rolledBack && mul < div {
mul, div = 1, 1
}
delta := mul * t.baseDur / div
delta := t.baseDur
if delta == 0 {
delta = 1
}
@ -180,12 +114,7 @@ func (t *BlockTimer) tick(h uint32) {
if !t.once {
t.cur = 0
t.rolledBack = true
t.reset()
}
}
for i := range t.ps {
t.ps[i].tick(h)
}
}