setTimer sets a new timer with the specified duration
(td time.Duration, periodic bool, arg interface{}, cb TimerCallback)
| 94 | |
| 95 | // setTimer sets a new timer with the specified duration |
| 96 | func (tm *TimerManager) setTimer(td time.Duration, periodic bool, arg interface{}, cb TimerCallback) int { |
| 97 | |
| 98 | // Creates timeout entry |
| 99 | t := timeout{ |
| 100 | id: tm.nextID, |
| 101 | expire: time.Now().Add(td), |
| 102 | cb: cb, |
| 103 | arg: arg, |
| 104 | period: 0, |
| 105 | } |
| 106 | if periodic { |
| 107 | t.period = td |
| 108 | } |
| 109 | tm.nextID++ |
| 110 | |
| 111 | // Look for empty entry |
| 112 | for pos, ct := range tm.timers { |
| 113 | if ct.id == 0 { |
| 114 | tm.timers[pos] = t |
| 115 | return t.id |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // If no empty entry found, add to end of array |
| 120 | tm.timers = append(tm.timers, t) |
| 121 | return t.id |
| 122 | } |
no test coverage detected