-
Notifications
You must be signed in to change notification settings - Fork 54
/
ping.go
61 lines (57 loc) · 1.23 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package ch
import (
"context"
"github.com/go-faster/errors"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"github.com/ClickHouse/ch-go/otelch"
"github.com/ClickHouse/ch-go/proto"
)
// Ping server.
//
// Do not call concurrently with Do.
func (c *Client) Ping(ctx context.Context) (err error) {
if c.IsClosed() {
return ErrClosed
}
if c.otel {
newCtx, span := c.tracer.Start(ctx, "Ping",
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(
otelch.ProtocolVersion(c.protocolVersion),
),
)
ctx = newCtx
defer func() {
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "Failed")
} else {
span.SetStatus(codes.Ok, "")
}
span.End()
}()
}
c.writer.ChainBuffer(func(b *proto.Buffer) {
b.Encode(proto.ClientCodePing)
})
if err := c.flush(ctx); err != nil {
return errors.Wrap(err, "flush")
}
p, err := c.packet(ctx)
if err != nil {
return errors.Wrap(err, "read")
}
switch p {
case proto.ServerCodePong:
return nil
case proto.ServerCodeException:
e, err := c.exception()
if err != nil {
return errors.Wrap(err, "decode exception")
}
return errors.Wrap(e, "exception")
default:
return errors.Errorf("unexpected packet %s", p)
}
}