MCPcopy Index your code
hub / github.com/coder/coder / Run

Method Run

coderd/healthcheck/websocket.go:29–105  ·  view source on GitHub ↗
(ctx context.Context, opts *WebsocketReportOptions)

Source from the content-addressed store, hash-verified

27}
28
29func (r *WebsocketReport) Run(ctx context.Context, opts *WebsocketReportOptions) {
30 ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
31 defer cancel()
32
33 r.Severity = health.SeverityOK
34 r.Warnings = []health.Message{}
35 r.Dismissed = opts.Dismissed
36
37 u, err := opts.AccessURL.Parse("/api/v2/debug/ws")
38 if err != nil {
39 r.Error = convertError(xerrors.Errorf("parse access url: %w", err))
40 r.Severity = health.SeverityError
41 return
42 }
43 if u.Scheme == "https" {
44 u.Scheme = "wss"
45 } else {
46 u.Scheme = "ws"
47 }
48
49 //nolint:bodyclose // websocket package closes this for you
50 c, res, err := websocket.Dial(ctx, u.String(), &websocket.DialOptions{
51 HTTPClient: opts.HTTPClient,
52 HTTPHeader: http.Header{"Coder-Session-Token": []string{opts.APIKey}},
53 })
54 if res != nil {
55 var body string
56 if res.Body != nil {
57 b, err := io.ReadAll(res.Body)
58 if err == nil {
59 body = string(b)
60 }
61 }
62
63 r.Body = body
64 r.Code = res.StatusCode
65 }
66 if err != nil {
67 r.Error = convertError(xerrors.Errorf("websocket dial: %w", err))
68 r.Error = health.Errorf(health.CodeWebsocketDial, "websocket dial: %s", err)
69 r.Severity = health.SeverityError
70 return
71 }
72 defer c.Close(websocket.StatusGoingAway, "goodbye")
73
74 for i := 0; i < 3; i++ {
75 msg := strconv.Itoa(i)
76 err := c.Write(ctx, websocket.MessageText, []byte(msg))
77 if err != nil {
78 r.Error = health.Errorf(health.CodeWebsocketEcho, "write message: %s", err)
79 r.Severity = health.SeverityError
80 return
81 }
82
83 ty, got, err := c.Read(ctx)
84 if err != nil {
85 r.Error = health.Errorf(health.CodeWebsocketEcho, "read message: %s", err)
86 r.Severity = health.SeverityError

Callers 2

WebsocketMethod · 0.95
TestWebsocketFunction · 0.95

Calls 10

ErrorfFunction · 0.92
convertErrorFunction · 0.70
ParseMethod · 0.65
DialMethod · 0.65
CloseMethod · 0.65
WriteMethod · 0.65
ReadMethod · 0.65
ErrorfMethod · 0.45
StringMethod · 0.45
ReadAllMethod · 0.45

Tested by 1

TestWebsocketFunction · 0.76