Removed timers locks.

pull/4369/head
frozen 2 years ago committed by Casey Gardiner
parent e264e11bdf
commit 5175ec6225
  1. 14
      internal/utils/timer.go

@ -1,7 +1,6 @@
package utils package utils
import ( import (
"sync"
"time" "time"
) )
@ -20,7 +19,6 @@ type Timeout struct {
state TimeoutState state TimeoutState
d time.Duration d time.Duration
start time.Time start time.Time
mu sync.Mutex
} }
// NewTimeout creates a new timeout class // NewTimeout creates a new timeout class
@ -31,24 +29,18 @@ func NewTimeout(d time.Duration) *Timeout {
// Start starts the timeout clock // Start starts the timeout clock
func (timeout *Timeout) Start() { func (timeout *Timeout) Start() {
timeout.mu.Lock()
timeout.state = Active timeout.state = Active
timeout.start = time.Now() timeout.start = time.Now()
timeout.mu.Unlock()
} }
// Stop stops the timeout clock // Stop stops the timeout clock
func (timeout *Timeout) Stop() { func (timeout *Timeout) Stop() {
timeout.mu.Lock()
timeout.state = Inactive timeout.state = Inactive
timeout.start = time.Now() timeout.start = time.Now()
timeout.mu.Unlock()
} }
// CheckExpire checks whether the timeout is reached/expired // CheckExpire checks whether the timeout is reached/expired
func (timeout *Timeout) CheckExpire() bool { func (timeout *Timeout) CheckExpire() bool {
timeout.mu.Lock()
defer timeout.mu.Unlock()
if timeout.state == Active && time.Since(timeout.start) > timeout.d { if timeout.state == Active && time.Since(timeout.start) > timeout.d {
timeout.state = Expired timeout.state = Expired
} }
@ -60,23 +52,17 @@ func (timeout *Timeout) CheckExpire() bool {
// Duration returns the duration period of timeout // Duration returns the duration period of timeout
func (timeout *Timeout) Duration() time.Duration { func (timeout *Timeout) Duration() time.Duration {
timeout.mu.Lock()
defer timeout.mu.Unlock()
return timeout.d return timeout.d
} }
// SetDuration set new duration for the timer // SetDuration set new duration for the timer
func (timeout *Timeout) SetDuration(nd time.Duration) { func (timeout *Timeout) SetDuration(nd time.Duration) {
timeout.mu.Lock()
timeout.d = nd timeout.d = nd
timeout.mu.Unlock()
} }
// IsActive checks whether timeout clock is active; // IsActive checks whether timeout clock is active;
// A timeout is active means it's not stopped caused by stop // A timeout is active means it's not stopped caused by stop
// and also not expired with time elapses longer than duration from start // and also not expired with time elapses longer than duration from start
func (timeout *Timeout) IsActive() bool { func (timeout *Timeout) IsActive() bool {
timeout.mu.Lock()
defer timeout.mu.Unlock()
return timeout.state == Active return timeout.state == Active
} }

Loading…
Cancel
Save