Documentation
¶
Overview ¶
Page 64 - 65 Listing 3-10: A function that pings a network connection at a regular interval.
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
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.