-
Notifications
You must be signed in to change notification settings - Fork 104
/
bucket_ping.go
99 lines (85 loc) · 2.82 KB
/
bucket_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package gocb
import (
"context"
"encoding/json"
"time"
)
// EndpointPingReport represents a single entry in a ping report.
type EndpointPingReport struct {
ID string
Local string
Remote string
State PingState
Error string
Namespace string
Latency time.Duration
}
// PingResult encapsulates the details from a executed ping operation.
type PingResult struct {
ID string
Services map[ServiceType][]EndpointPingReport
sdk string
}
type jsonEndpointPingReport struct {
ID string `json:"id,omitempty"`
Local string `json:"local,omitempty"`
Remote string `json:"remote,omitempty"`
State string `json:"state,omitempty"`
Error string `json:"error,omitempty"`
Namespace string `json:"namespace,omitempty"`
LatencyUs uint64 `json:"latency_us"`
}
type jsonPingReport struct {
Version uint16 `json:"version"`
SDK string `json:"sdk,omitempty"`
ID string `json:"id,omitempty"`
Services map[string][]jsonEndpointPingReport `json:"services,omitempty"`
}
// MarshalJSON generates a JSON representation of this ping report.
func (report *PingResult) MarshalJSON() ([]byte, error) {
jsonReport := jsonPingReport{
Version: 2,
SDK: report.sdk,
ID: report.ID,
Services: make(map[string][]jsonEndpointPingReport),
}
for serviceType, serviceInfo := range report.Services {
serviceStr := serviceTypeToString(serviceType)
if _, ok := jsonReport.Services[serviceStr]; !ok {
jsonReport.Services[serviceStr] = make([]jsonEndpointPingReport, 0)
}
for _, service := range serviceInfo {
jsonReport.Services[serviceStr] = append(jsonReport.Services[serviceStr], jsonEndpointPingReport{
ID: service.ID,
Local: service.Local,
Remote: service.Remote,
State: pingStateToString(service.State),
Error: service.Error,
Namespace: service.Namespace,
LatencyUs: uint64(service.Latency / time.Nanosecond),
})
}
}
return json.Marshal(&jsonReport)
}
// PingOptions are the options available to the Ping operation.
type PingOptions struct {
ServiceTypes []ServiceType
ReportID string
Timeout time.Duration
ParentSpan RequestSpan
// Using a deadlined Context alongside a Timeout will cause the shorter of the two to cause cancellation, this
// also applies to global level timeouts.
// UNCOMMITTED: This API may change in the future.
Context context.Context
}
// Ping will ping a list of services and verify they are active and
// responding in an acceptable period of time.
func (b *Bucket) Ping(opts *PingOptions) (*PingResult, error) {
return autoOpControl(b.diagnosticsController(), "", func(provider diagnosticsProvider) (*PingResult, error) {
if opts == nil {
opts = &PingOptions{}
}
return provider.Ping(opts)
})
}