WriteLoop is a helper method that invokes WriteTo to the passed writer every time the passed channel fires. This method blocks until ctx is canceled, so clients probably want to run it in its own goroutine. For typical usage, create a time.Ticker and pass its C channel to this method.
(ctx context.Context, c <-chan time.Time, w io.Writer)
| 115 | // so clients probably want to run it in its own goroutine. For typical |
| 116 | // usage, create a time.Ticker and pass its C channel to this method. |
| 117 | func (d *Dogstatsd) WriteLoop(ctx context.Context, c <-chan time.Time, w io.Writer) { |
| 118 | for { |
| 119 | select { |
| 120 | case <-c: |
| 121 | if _, err := d.WriteTo(w); err != nil { |
| 122 | d.logger.Log("during", "WriteTo", "err", err) |
| 123 | } |
| 124 | case <-ctx.Done(): |
| 125 | return |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // SendLoop is a helper method that wraps WriteLoop, passing a managed |
| 131 | // connection to the network and address. Like WriteLoop, this method blocks |