Browse Source

Merge pull request #60 from sparrc/after-to-timer

Use time.Timer instead of time.After
Rob Figueiredo 9 years ago
parent
commit
339702a1ff
1 changed files with 4 additions and 1 deletions
  1. 4 1
      cron.go

+ 4 - 1
cron.go

@@ -165,8 +165,9 @@ func (c *Cron) run() {
 			effective = c.entries[0].Next
 		}
 
+		timer := time.NewTimer(effective.Sub(now))
 		select {
-		case now = <-time.After(effective.Sub(now)):
+		case now = <-timer.C:
 			// Run every entry whose next time was this effective time.
 			for _, e := range c.entries {
 				if e.Next != effective {
@@ -186,11 +187,13 @@ func (c *Cron) run() {
 			c.snapshot <- c.entrySnapshot()
 
 		case <-c.stop:
+			timer.Stop()
 			return
 		}
 
 		// 'now' should be updated after newEntry and snapshot cases.
 		now = time.Now().Local()
+		timer.Stop()
 	}
 }