Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Pinger ¶
Example ¶
ctx, cancel := context.WithCancel(context.Background()) r, w := io.Pipe() // in lieu of net.Conn done := make(chan struct{}) resetTimer := make(chan time.Duration, 1) resetTimer <- time.Second // initial ping interval go func() { Pinger(ctx, w, resetTimer) close(done) }() receivePing := func(d time.Duration, r io.Reader) { if d >= 0 { fmt.Printf("resetting timer (%s)\n", d) resetTimer <- d } now := time.Now() buf := make([]byte, 1024) n, err := r.Read(buf) if err != nil { fmt.Println(err) } fmt.Printf("received %q (%s)\n", buf[:n], time.Since(now).Round(100*time.Millisecond)) } for i, v := range []int64{0, 200, 300, 0, -1, -1, -1} { fmt.Printf("Run %d:\n", i+1) receivePing(time.Duration(v)*time.Millisecond, r) } cancel() <-done // ensures the pinger exits after canceling the context
Output: Run 1: resetting timer (0s) received "ping" (1s) Run 2: resetting timer (200ms) received "ping" (200ms) Run 3: resetting timer (300ms) received "ping" (300ms) Run 4: resetting timer (0s) received "ping" (300ms) Run 5: received "ping" (300ms) Run 6: received "ping" (300ms) Run 7: received "ping" (300ms)
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.