title: Resetting a ticker
body: <p>Consider a server that is initialised with a timer/ticker resource that will fire every <code>t</code> (<code>t</code> is 20ms in my example) ticks. Every time the server listens to something on the network (like a periodic signal from peers), it has to reset the timer. On the other hand if the timer expires without a reset (for example, all of its peers are dead), it triggers some event (I'm just printing the time from start of program in my example).</p>
  
  <p>I'm having trouble implementing this behaviour using <code>time.Ticker</code>. Resetting the timer seems to work (it does not fire for the first 50ms), but the ticker is not active (doesn't tick every 20ms) after that. </p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "time"
  )
  
  var wallclock time.Time
  
  type server struct {
      timeout *time.Ticker
      stop    chan bool
  }
  
  func (srv *server) start() {
      for {
          select {
          case &lt;-srv.timeout.C:
              {
                  elapsed := time.Since(wallclock)
                  fmt.Println("timed out after ", elapsed, " elapsed from start ")
              }
          case &lt;-srv.stop:
              {
                  return
              }
          }
      }
  }
  
  func main() {
      wallclock = time.Now()
      //make the server with a timer that will fire every 20ms
      srv := server{
          timeout: time.NewTicker(20 * time.Millisecond),
          //channel to indicate the server to stop listening
          stop:    make(chan bool),
      }
      //start listening on a different thread 
      go srv.start()
      for i := 0; i &lt; 5; i++ {
          //reset it every 10ms
          time.Sleep(10 * time.Millisecond)
          srv.timeout.Stop()
          //as the reset frequency is higher, 
          //I'm not expecting this to fire within 
          //the first 50ms (5*10ms)
          srv.timeout = time.NewTicker(20 * time.Millisecond)
      }
      //sleep for 110ms
      //I'm expecting the timer to fire at least 5 times here
      time.Sleep(110 * time.Millisecond)
      //stop listening
      srv.stop &lt;- true
      fmt.Println("Hi from tckr!")
  }
  </code></pre>
  
  <p>I'm expecting to see something like</p>
  
  <pre><code>timed out after ~70ms elapsed from start
  timed out after ~90ms elapsed from start
  timed out after ~110ms elapsed from start
  timed out after ~130ms elapsed from start
  timed out after ~150ms elapsed from start
  Hi from tckr!
  </code></pre>
  
  <p>Five times because I let the main thread sleep for 110ms and the 20ms timer could fire five times within this interval.</p>
  
  <p>But I just see <code>Hi from tckr!</code>. Is <code>srv.timeout = time.NewTicker(20 * time.Millisecond)</code> the proper way to reset <code>Ticker</code> ? </p>
  
  <p>If I don't stop the ticker within the <code>for</code> loop (<code>srv.timeout.Stop()</code>), the ticker seems to keep ticking. Here's a sample output after commenting <code>srv.timeout.Stop()</code>. </p>
  
  <pre><code>timed out after  20.6872ms  elapsed from start
  timed out after  41.4278ms  elapsed from start
  timed out after  61.8747ms  elapsed from start
  timed out after  72.7793ms  elapsed from start
  timed out after  94.1448ms  elapsed from start
  timed out after  112.5283ms  elapsed from start
  timed out after  134.0131ms  elapsed from start
  timed out after  152.5846ms  elapsed from start
  Hi from tckr!
  </code></pre>
  
  <p>I don't want the ticker to fire within the first 50ms (i.e, I don't want to see the first two lines with 20.6872ms and 41.4278ms). </p>