Skip to content

Commit

Permalink
feat: expose duration
Browse files Browse the repository at this point in the history
  • Loading branch information
loongy committed Jan 29, 2021
1 parent 21f6797 commit 362ae11
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions timer/timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type LinearTimer struct {
}

// NewLinearTimer constructs a new Linear Timer from the input options and channels
func NewLinearTimer(opts Options, handleTimeoutPropose, handleTimeoutPrevote, handleTimeoutPrecommit func(Timeout)) process.Timer {
func NewLinearTimer(opts Options, handleTimeoutPropose, handleTimeoutPrevote, handleTimeoutPrecommit func(Timeout)) *LinearTimer {
return &LinearTimer{
opts: opts,
handleTimeoutPropose: handleTimeoutPropose,
Expand All @@ -86,7 +86,7 @@ func NewLinearTimer(opts Options, handleTimeoutPropose, handleTimeoutPrevote, ha
func (t *LinearTimer) TimeoutPropose(height process.Height, round process.Round) {
if t.handleTimeoutPropose != nil {
go func() {
time.Sleep(t.timeoutDuration(height, round))
time.Sleep(t.DurationAtHeightAndRound(height, round))
t.handleTimeoutPropose(Timeout{MessageType: process.MessageTypePropose, Height: height, Round: round})
}()
}
Expand All @@ -97,7 +97,7 @@ func (t *LinearTimer) TimeoutPropose(height process.Height, round process.Round)
func (t *LinearTimer) TimeoutPrevote(height process.Height, round process.Round) {
if t.handleTimeoutPrevote != nil {
go func() {
time.Sleep(t.timeoutDuration(height, round))
time.Sleep(t.DurationAtHeightAndRound(height, round))
t.handleTimeoutPrevote(Timeout{MessageType: process.MessageTypePrevote, Height: height, Round: round})
}()
}
Expand All @@ -108,12 +108,15 @@ func (t *LinearTimer) TimeoutPrevote(height process.Height, round process.Round)
func (t *LinearTimer) TimeoutPrecommit(height process.Height, round process.Round) {
if t.handleTimeoutPrecommit != nil {
go func() {
time.Sleep(t.timeoutDuration(height, round))
time.Sleep(t.DurationAtHeightAndRound(height, round))
t.handleTimeoutPrecommit(Timeout{MessageType: process.MessageTypePrecommit, Height: height, Round: round})
}()
}
}

func (t *LinearTimer) timeoutDuration(height process.Height, round process.Round) time.Duration {
// DurationAtHeightAndRound returns the duration of the timeout at the given
// height and round. This is the duration that the other methods will wait
// before scheduling their respective timeout events.
func (t *LinearTimer) DurationAtHeightAndRound(height process.Height, round process.Round) time.Duration {
return t.opts.Timeout + t.opts.Timeout*time.Duration(float64(round)*t.opts.TimeoutScaling)
}

0 comments on commit 362ae11

Please sign in to comment.