From f0bf587787d922aee7a59497501c30356ba0f050 Mon Sep 17 00:00:00 2001 From: Matt Aimonetti Date: Sat, 26 Apr 2014 11:02:59 -0600 Subject: [PATCH] removed sleeps from every and after funcs --- utils.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/utils.go b/utils.go index 36afe2783..e41f14054 100644 --- a/utils.go +++ b/utils.go @@ -7,22 +7,24 @@ import ( "time" ) +// Every triggers f every `t` time until the end of days. func Every(t string, f func()) { - dur := parseDuration(t) + c := time.Tick(parseDuration(t)) + // start a go routine to not bloc the function go func() { for { - time.Sleep(dur) + // wait for the ticker to tell us to run + <-c + // run the passed function in another go routine + // so we don't slow down the loop. go f() } }() } +// After triggers the passed function after `t` duration. func After(t string, f func()) { - dur := parseDuration(t) - go func() { - time.Sleep(dur) - f() - }() + time.AfterFunc(parseDuration(t), f) } func Publish(c chan interface{}, val interface{}) {