-
Notifications
You must be signed in to change notification settings - Fork 11
/
ping.go
43 lines (38 loc) · 906 Bytes
/
ping.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package chconn
import (
"context"
)
type pong struct{}
// Check that connection to the server is alive.
func (ch *conn) Ping(ctx context.Context) error {
if ctx != context.Background() {
select {
case <-ctx.Done():
return newContextAlreadyDoneError(ctx)
default:
}
ch.contextWatcher.Watch(ctx)
defer ch.contextWatcher.Unwatch()
}
ch.writer.Uvarint(clientPing)
var hasError bool
defer func() {
if hasError {
ch.Close()
}
}()
if _, err := ch.writer.WriteTo(ch.writerTo); err != nil {
hasError = true
return &writeError{"ping: write packet type", preferContextOverNetTimeoutError(ctx, err)}
}
res, err := ch.receiveAndProcessData(emptyOnProgress)
if err != nil {
hasError = true
return preferContextOverNetTimeoutError(ctx, err)
}
if _, ok := res.(*pong); !ok {
hasError = true
return &unexpectedPacket{expected: "serverPong", actual: res}
}
return nil
}