This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* bump: [email protected] Signed-off-by: Sam Batschelet <[email protected]> * *: react to bump Signed-off-by: Sam Batschelet <[email protected]> Signed-off-by: Sam Batschelet <[email protected]>
- Loading branch information
Showing
10 changed files
with
213 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package timer | ||
|
||
import "time" | ||
|
||
// NewStagedTimer returns a timer that will execute [f] | ||
// when a timeout occurs and execute an additional timeout after | ||
// the returned duration if [f] returns true and some duration. | ||
func NewStagedTimer(f func() (time.Duration, bool)) *Timer { | ||
t := NewTimer(nil) | ||
t.handler = func() { | ||
delay, repeat := f() | ||
if repeat { | ||
t.SetTimeoutIn(delay) | ||
} | ||
} | ||
return t | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package timer | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSingleStagedTimer(t *testing.T) { | ||
wg := sync.WaitGroup{} | ||
wg.Add(1) | ||
ticks := 1 | ||
i := 0 | ||
timer := NewStagedTimer(func() (time.Duration, bool) { | ||
defer wg.Done() | ||
i++ | ||
return 0, false | ||
}) | ||
go timer.Dispatch() | ||
|
||
timer.SetTimeoutIn(time.Millisecond) | ||
wg.Wait() | ||
require.Equal(t, i, ticks) | ||
} | ||
|
||
func TestMultiStageTimer(t *testing.T) { | ||
wg := sync.WaitGroup{} | ||
ticks := 3 | ||
wg.Add(ticks) | ||
|
||
i := 0 | ||
timer := NewStagedTimer(func() (time.Duration, bool) { | ||
defer wg.Done() | ||
i++ | ||
return time.Millisecond, i < ticks | ||
}) | ||
go timer.Dispatch() | ||
|
||
timer.SetTimeoutIn(time.Millisecond) | ||
wg.Wait() | ||
require.Equal(t, i, ticks) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package timer | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
// Timer wraps a timer object. This allows a user to specify a handler. Once | ||
// specifying the handler, the dispatch thread can be called. The dispatcher | ||
// will only return after calling Stop. SetTimeoutIn will result in calling the | ||
// handler in the specified amount of time. | ||
type Timer struct { | ||
handler func() | ||
timeout chan struct{} | ||
|
||
lock sync.Mutex | ||
wg sync.WaitGroup | ||
finished, shouldExecute bool | ||
duration time.Duration | ||
} | ||
|
||
// NewTimer creates a new timer object | ||
func NewTimer(handler func()) *Timer { | ||
timer := &Timer{ | ||
handler: handler, | ||
timeout: make(chan struct{}, 1), | ||
} | ||
timer.wg.Add(1) | ||
|
||
return timer | ||
} | ||
|
||
// SetTimeoutIn will set the timer to fire the handler in [duration] | ||
func (t *Timer) SetTimeoutIn(duration time.Duration) { | ||
t.lock.Lock() | ||
defer t.lock.Unlock() | ||
|
||
t.duration = duration | ||
t.shouldExecute = true | ||
t.reset() | ||
} | ||
|
||
// Cancel the currently scheduled event | ||
func (t *Timer) Cancel() { | ||
t.lock.Lock() | ||
defer t.lock.Unlock() | ||
|
||
t.shouldExecute = false | ||
t.reset() | ||
} | ||
|
||
// Stop this timer from executing any more. | ||
func (t *Timer) Stop() { | ||
t.lock.Lock() | ||
if !t.finished { | ||
defer t.wg.Wait() | ||
} | ||
defer t.lock.Unlock() | ||
|
||
t.finished = true | ||
t.reset() | ||
} | ||
|
||
func (t *Timer) Dispatch() { | ||
t.lock.Lock() | ||
defer t.lock.Unlock() | ||
defer t.wg.Done() | ||
|
||
timer := time.NewTimer(0) | ||
cleared := false | ||
reset := false | ||
for !t.finished { // t.finished needs to be thread safe | ||
if !reset && !timer.Stop() && !cleared { | ||
<-timer.C | ||
} | ||
|
||
if cleared && t.shouldExecute { | ||
t.lock.Unlock() | ||
t.handler() | ||
} else { | ||
t.lock.Unlock() | ||
} | ||
|
||
cleared = false | ||
reset = false | ||
select { | ||
case <-t.timeout: | ||
t.lock.Lock() | ||
if t.shouldExecute { | ||
timer.Reset(t.duration) | ||
} | ||
reset = true | ||
case <-timer.C: | ||
t.lock.Lock() | ||
cleared = true | ||
} | ||
} | ||
} | ||
|
||
func (t *Timer) reset() { | ||
select { | ||
case t.timeout <- struct{}{}: | ||
default: | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package timer | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestTimer(t *testing.T) { | ||
wg := sync.WaitGroup{} | ||
wg.Add(1) | ||
defer wg.Wait() | ||
|
||
timer := NewTimer(wg.Done) | ||
go timer.Dispatch() | ||
|
||
timer.SetTimeoutIn(time.Millisecond) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters