Fix missing timer reset in TxManager

Also, replace usage of time.Duration by time.NewTimer, because the later allows
replacing timers by stopping them before so that we never leak resources.
This commit is contained in:
Eduard S
2021-02-25 14:03:15 +01:00
parent 706e4c7a3d
commit 2a5992d218
4 changed files with 37 additions and 25 deletions

View File

@@ -146,7 +146,7 @@ const longWaitDuration = 999 * time.Hour
// const provingDuration = 2 * time.Second
func (s *Mock) runProver(ctx context.Context) {
waitCh := time.After(longWaitDuration)
timer := time.NewTimer(longWaitDuration)
for {
select {
case <-ctx.Done():
@@ -154,21 +154,27 @@ func (s *Mock) runProver(ctx context.Context) {
case msg := <-s.msgCh:
switch msg.value {
case "cancel":
waitCh = time.After(longWaitDuration)
if !timer.Stop() {
<-timer.C
}
timer.Reset(longWaitDuration)
s.Lock()
if !s.status.IsReady() {
s.status = prover.StatusCodeAborted
}
s.Unlock()
case "prove":
waitCh = time.After(s.provingDuration)
if !timer.Stop() {
<-timer.C
}
timer.Reset(s.provingDuration)
s.Lock()
s.status = prover.StatusCodeBusy
s.Unlock()
}
msg.ackCh <- true
case <-waitCh:
waitCh = time.After(longWaitDuration)
case <-timer.C:
timer.Reset(longWaitDuration)
s.Lock()
if s.status != prover.StatusCodeBusy {
s.Unlock()