Ping sends a ping to the peer and waits for a pong. Use this to measure latency or ensure the peer is responsive. Ping must be called concurrently with Reader as it does not read from the connection but instead waits for a Reader call to read the pong. TCP Keepalives should suffice for most use cas
(ctx context.Context)
| 221 | // |
| 222 | // TCP Keepalives should suffice for most use cases. |
| 223 | func (c *Conn) Ping(ctx context.Context) error { |
| 224 | p := c.pingCounter.Add(1) |
| 225 | |
| 226 | err := c.ping(ctx, strconv.FormatInt(p, 10)) |
| 227 | if err != nil { |
| 228 | return fmt.Errorf("failed to ping: %w", err) |
| 229 | } |
| 230 | return nil |
| 231 | } |
| 232 | |
| 233 | func (c *Conn) ping(ctx context.Context, p string) error { |
| 234 | pong := make(chan struct{}, 1) |