1
- package healthcheck
1
+ package derphealth
2
2
3
3
import (
4
4
"context"
@@ -24,11 +24,11 @@ import (
24
24
"github.com/coder/coder/v2/coderd/util/ptr"
25
25
)
26
26
27
- // @typescript-generate DERPReport
28
- type DERPReport struct {
27
+ // @typescript-generate Report
28
+ type Report struct {
29
29
Healthy bool `json:"healthy"`
30
30
31
- Regions map [int ]* DERPRegionReport `json:"regions"`
31
+ Regions map [int ]* RegionReport `json:"regions"`
32
32
33
33
Netcheck * netcheck.Report `json:"netcheck"`
34
34
NetcheckErr * string `json:"netcheck_err"`
@@ -37,18 +37,18 @@ type DERPReport struct {
37
37
Error * string `json:"error"`
38
38
}
39
39
40
- // @typescript-generate DERPRegionReport
41
- type DERPRegionReport struct {
40
+ // @typescript-generate RegionReport
41
+ type RegionReport struct {
42
42
mu sync.Mutex
43
43
Healthy bool `json:"healthy"`
44
44
45
45
Region * tailcfg.DERPRegion `json:"region"`
46
- NodeReports []* DERPNodeReport `json:"node_reports"`
46
+ NodeReports []* NodeReport `json:"node_reports"`
47
47
Error * string `json:"error"`
48
48
}
49
49
50
- // @typescript-generate DERPNodeReport
51
- type DERPNodeReport struct {
50
+ // @typescript-generate NodeReport
51
+ type NodeReport struct {
52
52
mu sync.Mutex
53
53
clientCounter int
54
54
@@ -64,23 +64,23 @@ type DERPNodeReport struct {
64
64
ClientErrs [][]string `json:"client_errs"`
65
65
Error * string `json:"error"`
66
66
67
- STUN DERPStunReport `json:"stun"`
67
+ STUN StunReport `json:"stun"`
68
68
}
69
69
70
- // @typescript-generate DERPStunReport
71
- type DERPStunReport struct {
70
+ // @typescript-generate StunReport
71
+ type StunReport struct {
72
72
Enabled bool
73
73
CanSTUN bool
74
74
Error * string
75
75
}
76
76
77
- type DERPReportOptions struct {
77
+ type ReportOptions struct {
78
78
DERPMap * tailcfg.DERPMap
79
79
}
80
80
81
- func (r * DERPReport ) Run (ctx context.Context , opts * DERPReportOptions ) {
81
+ func (r * Report ) Run (ctx context.Context , opts * ReportOptions ) {
82
82
r .Healthy = true
83
- r .Regions = map [int ]* DERPRegionReport {}
83
+ r .Regions = map [int ]* RegionReport {}
84
84
85
85
wg := & sync.WaitGroup {}
86
86
mu := sync.Mutex {}
@@ -89,7 +89,7 @@ func (r *DERPReport) Run(ctx context.Context, opts *DERPReportOptions) {
89
89
for _ , region := range opts .DERPMap .Regions {
90
90
var (
91
91
region = region
92
- regionReport = DERPRegionReport {
92
+ regionReport = RegionReport {
93
93
Region : region ,
94
94
}
95
95
)
@@ -128,17 +128,17 @@ func (r *DERPReport) Run(ctx context.Context, opts *DERPReportOptions) {
128
128
wg .Wait ()
129
129
}
130
130
131
- func (r * DERPRegionReport ) Run (ctx context.Context ) {
131
+ func (r * RegionReport ) Run (ctx context.Context ) {
132
132
r .Healthy = true
133
- r .NodeReports = []* DERPNodeReport {}
133
+ r .NodeReports = []* NodeReport {}
134
134
135
135
wg := & sync.WaitGroup {}
136
136
137
137
wg .Add (len (r .Region .Nodes ))
138
138
for _ , node := range r .Region .Nodes {
139
139
var (
140
140
node = node
141
- nodeReport = DERPNodeReport {
141
+ nodeReport = NodeReport {
142
142
Node : node ,
143
143
Healthy : true ,
144
144
}
@@ -166,7 +166,7 @@ func (r *DERPRegionReport) Run(ctx context.Context) {
166
166
wg .Wait ()
167
167
}
168
168
169
- func (r * DERPNodeReport ) derpURL () * url.URL {
169
+ func (r * NodeReport ) derpURL () * url.URL {
170
170
derpURL := & url.URL {
171
171
Scheme : "https" ,
172
172
Host : r .Node .HostName ,
@@ -185,7 +185,7 @@ func (r *DERPNodeReport) derpURL() *url.URL {
185
185
return derpURL
186
186
}
187
187
188
- func (r * DERPNodeReport ) Run (ctx context.Context ) {
188
+ func (r * NodeReport ) Run (ctx context.Context ) {
189
189
ctx , cancel := context .WithTimeout (ctx , 10 * time .Second )
190
190
defer cancel ()
191
191
@@ -218,7 +218,7 @@ func (r *DERPNodeReport) Run(ctx context.Context) {
218
218
}
219
219
}
220
220
221
- func (r * DERPNodeReport ) doExchangeMessage (ctx context.Context ) {
221
+ func (r * NodeReport ) doExchangeMessage (ctx context.Context ) {
222
222
if r .Node .STUNOnly {
223
223
return
224
224
}
@@ -299,7 +299,7 @@ func (r *DERPNodeReport) doExchangeMessage(ctx context.Context) {
299
299
wg .Wait ()
300
300
}
301
301
302
- func (r * DERPNodeReport ) doSTUNTest (ctx context.Context ) {
302
+ func (r * NodeReport ) doSTUNTest (ctx context.Context ) {
303
303
if r .Node .STUNPort == - 1 {
304
304
return
305
305
}
@@ -331,7 +331,7 @@ func (r *DERPNodeReport) doSTUNTest(ctx context.Context) {
331
331
r .mu .Unlock ()
332
332
}
333
333
334
- func (r * DERPNodeReport ) stunAddr (ctx context.Context ) (string , int , error ) {
334
+ func (r * NodeReport ) stunAddr (ctx context.Context ) (string , int , error ) {
335
335
port := r .Node .STUNPort
336
336
if port == 0 {
337
337
port = 3478
@@ -387,13 +387,13 @@ func (r *DERPNodeReport) stunAddr(ctx context.Context) (string, int, error) {
387
387
return "" , 0 , xerrors .New ("no stun ips provided" )
388
388
}
389
389
390
- func (r * DERPNodeReport ) writeClientErr (clientID int , err error ) {
390
+ func (r * NodeReport ) writeClientErr (clientID int , err error ) {
391
391
r .mu .Lock ()
392
392
r .ClientErrs [clientID ] = append (r .ClientErrs [clientID ], err .Error ())
393
393
r .mu .Unlock ()
394
394
}
395
395
396
- func (r * DERPNodeReport ) derpClient (ctx context.Context , derpURL * url.URL ) (* derphttp.Client , int , error ) {
396
+ func (r * NodeReport ) derpClient (ctx context.Context , derpURL * url.URL ) (* derphttp.Client , int , error ) {
397
397
r .mu .Lock ()
398
398
id := r .clientCounter
399
399
r .clientCounter ++
@@ -440,7 +440,7 @@ func (r *DERPNodeReport) derpClient(ctx context.Context, derpURL *url.URL) (*der
440
440
return client , id , nil
441
441
}
442
442
443
- func (r * DERPNodeReport ) recvData (client * derphttp.Client ) (derp.ReceivedPacket , error ) {
443
+ func (r * NodeReport ) recvData (client * derphttp.Client ) (derp.ReceivedPacket , error ) {
444
444
for {
445
445
msg , err := client .Recv ()
446
446
if err != nil {
@@ -459,3 +459,11 @@ func (r *DERPNodeReport) recvData(client *derphttp.Client) (derp.ReceivedPacket,
459
459
}
460
460
}
461
461
}
462
+
463
+ func convertError (err error ) * string {
464
+ if err != nil {
465
+ return ptr .Ref (err .Error ())
466
+ }
467
+
468
+ return nil
469
+ }
0 commit comments