-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
262 additions
and
241 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package clock | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
const WALLCLOCK_PRECISION = 1 * time.Second | ||
|
||
func AfterWallClock(d time.Duration) <-chan time.Time { | ||
ch := make(chan time.Time, 1) | ||
deadline := time.Now().Add(d).Truncate(0) | ||
after_ch := time.After(d) | ||
ticker := time.NewTicker(WALLCLOCK_PRECISION) | ||
go func() { | ||
var t time.Time | ||
defer ticker.Stop() | ||
for { | ||
select { | ||
case t = <-after_ch: | ||
ch <- t | ||
return | ||
case t = <-ticker.C: | ||
if t.After(deadline) { | ||
ch <- t | ||
return | ||
} | ||
} | ||
} | ||
}() | ||
return ch | ||
} | ||
|
||
func RunTicker(ctx context.Context, interval, retryInterval time.Duration, cb func(context.Context) error) { | ||
go func() { | ||
var err error | ||
for { | ||
nextInterval := interval | ||
if err != nil { | ||
nextInterval = retryInterval | ||
} | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-AfterWallClock(nextInterval): | ||
err = cb(ctx) | ||
} | ||
} | ||
}() | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package dialer | ||
|
||
import ( | ||
"context" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package dialer | ||
|
||
import ( | ||
"context" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package log | ||
|
||
import ( | ||
"fmt" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package log | ||
|
||
import ( | ||
"errors" | ||
|
Oops, something went wrong.