Created
August 29, 2016 01:25
-
-
Save Amit-PivotalLabs/246134b028de544e557315138acf9679 to your computer and use it in GitHub Desktop.
Golang scheduling DSL
This file contains hidden or 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
package main | |
import . "time" | |
type Every Duration | |
type Do func() | |
func (schedule Every) Do(schedulable func()) { | |
go func() { | |
c := NewTicker(Duration(schedule)).C | |
for { | |
<-c | |
go schedulable() | |
} | |
}() | |
} | |
func (schedulable Do) Every(schedule Duration) { | |
Every(schedule).Do(schedulable) | |
} | |
func Wait() { select {} } | |
func main() { | |
Every(Second).Do(func() { println("every second, do this") }) | |
Sleep(2 * Second) | |
Do(func() { println("do that, every 500 ms") }).Every(500 * Millisecond) | |
Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment