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
package statusstate
import (
"context"
"fmt"
"io"
"net/http"
"strings"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
)
type RelayObservation struct {
Phase v1alpha1.RelayStatusPhase
Cursor int64
LastEventAt string
Events int64
}
type RelayEndpointFunc func(ref string) (string, bool)
type HTTPRelayHealthSource struct {
endpoint RelayEndpointFunc
client *http.Client
}
func NewHTTPRelayHealthSource(endpoint RelayEndpointFunc, client *http.Client) *HTTPRelayHealthSource {
return &HTTPRelayHealthSource{endpoint: endpoint, client: httpClient(client)}
}
func (s *HTTPRelayHealthSource) RelayStatus(ctx context.Context, ref string) (RelayObservation, error) {
if s == nil {
return RelayObservation{}, fmt.Errorf("statusstate: the relay health source is nil")
}
base, err := s.baseURL(ref)
if err != nil {
return RelayObservation{}, err
}
request, err := http.NewRequestWithContext(ctx, http.MethodGet, base+"/xrpc/_health", nil)
if err != nil {
return RelayObservation{}, err
}
request.Header.Set("Accept", "application/json")
response, err := s.client.Do(request)
if err != nil {
return RelayObservation{}, err
}
defer response.Body.Close()
_, _ = io.Copy(io.Discard, io.LimitReader(response.Body, maxSourceBody))
phase := v1alpha1.RelayStatusPhaseRunning
if response.StatusCode < 200 || response.StatusCode > 299 {
phase = v1alpha1.RelayStatusPhaseFailed
}
return RelayObservation{Phase: phase}, nil
}
func (s *HTTPRelayHealthSource) baseURL(ref string) (string, error) {
ref = strings.TrimSpace(ref)
if s.endpoint != nil {
if resolved, ok := s.endpoint(ref); ok {
return sourceBaseURL(resolved, "relay")
}
}
if strings.Contains(ref, "://") {
return sourceBaseURL(ref, "relay")
}
return "", fmt.Errorf("statusstate: the relay ref %q has no endpoint and is not a URL", ref)
}