This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more accurate and customizable clock tick intervals
- Loading branch information
1 parent
c5d26bc
commit cd33d91
Showing
4 changed files
with
96 additions
and
35 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,13 @@ | ||
package api | ||
|
||
import "time" | ||
|
||
// TimeController controls the update loop for the bot | ||
type TimeController interface { | ||
// ShouldUpdate defines when to enter the bot's update cycle | ||
// lastUpdateTime will never start off as the zero value | ||
ShouldUpdate(lastUpdateTime time.Time, currentUpdateTime time.Time) bool | ||
|
||
// SleepTime computes how long we want to sleep before the next call to ShouldUpdate | ||
SleepTime(lastUpdateTime time.Time, currentUpdateTime time.Time) time.Duration | ||
} |
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,35 @@ | ||
package plugins | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"github.com/lightyeario/kelp/api" | ||
) | ||
|
||
// IntervalTimeController provides a standard time interval | ||
type IntervalTimeController struct { | ||
tickInterval time.Duration | ||
} | ||
|
||
// MakeIntervalTimeController is a factory method | ||
func MakeIntervalTimeController(tickInterval time.Duration) api.TimeController { | ||
return &IntervalTimeController{tickInterval: tickInterval} | ||
} | ||
|
||
var _ api.TimeController = &IntervalTimeController{} | ||
|
||
// ShouldUpdate impl | ||
func (t *IntervalTimeController) ShouldUpdate(lastUpdateTime time.Time, currentUpdateTime time.Time) bool { | ||
elapsedSinceUpdate := currentUpdateTime.Sub(lastUpdateTime) | ||
shouldUpdate := elapsedSinceUpdate >= t.tickInterval | ||
log.Printf("intervalTimeController tickInterval=%s, shouldUpdate=%v, elapsedSinceUpdate=%s\n", t.tickInterval, shouldUpdate, elapsedSinceUpdate) | ||
return shouldUpdate | ||
} | ||
|
||
// SleepTime impl | ||
func (t *IntervalTimeController) SleepTime(lastUpdateTime time.Time, currentUpdateTime time.Time) time.Duration { | ||
// use time till now as opposed to currentUpdateTime because we want the start of the clock cycle to be synchronized | ||
elapsedSinceUpdate := time.Since(lastUpdateTime) | ||
return time.Duration(t.tickInterval.Nanoseconds() - elapsedSinceUpdate.Nanoseconds()) | ||
} |
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