Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 980b331

Browse files
authored
chore: unmarshal HTTP errors for p2p commands (#321)
1 parent eccaa79 commit 980b331

File tree

3 files changed

+53
-27
lines changed

3 files changed

+53
-27
lines changed

.golangci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ linters-settings:
44
min-len: 4
55
min-occurrences: 3
66
gocognit:
7-
min-complexity: 46
7+
# tunnel.go has a 150 line function. Someone should fix it and
8+
# decrement this back down to a rational number.
9+
min-complexity: 52
810
nestif:
911
min-complexity: 10
1012
govet:
@@ -68,4 +70,4 @@ issues:
6870
# gosec: Too many issues in popular repos
6971
- (Expect directory permissions to be 0750 or less|Expect file permissions to be 0600 or less)
7072
# gosec: False positive is triggered by 'src, err := ioutil.ReadFile(filename)'
71-
- Potential file inclusion via variable
73+
- Potential file inclusion via variable

agent/server.go

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"go.coder.com/retry"
1212
"golang.org/x/xerrors"
1313
"nhooyr.io/websocket"
14+
15+
"cdr.dev/coder-cli/coder-sdk"
1416
)
1517

1618
const (
@@ -45,32 +47,48 @@ func NewServer(args ServerArgs) (*Server, error) {
4547

4648
// Run will listen and proxy new peer connections on a retry loop.
4749
func (s *Server) Run(ctx context.Context) error {
48-
err := retry.New(time.Second).Context(ctx).Backoff(15 * time.Second).Run(func() error {
49-
ctx, cancelFunc := context.WithTimeout(ctx, time.Second*15)
50-
defer cancelFunc()
51-
s.log.Info(ctx, "connecting to coder", slog.F("url", s.listenURL.String()))
52-
conn, _, err := websocket.Dial(ctx, s.listenURL.String(), nil)
53-
if err != nil {
54-
return fmt.Errorf("dial: %w", err)
55-
}
56-
nc := websocket.NetConn(context.Background(), conn, websocket.MessageBinary)
57-
session, err := yamux.Server(nc, nil)
58-
if err != nil {
59-
return fmt.Errorf("open: %w", err)
60-
}
61-
s.log.Info(ctx, "connected to coder. awaiting connection requests")
62-
for {
63-
st, err := session.AcceptStream()
50+
err := retry.New(time.Second).
51+
Context(ctx).
52+
Backoff(15 * time.Second).
53+
Conditions(
54+
retry.Condition(func(err error) bool {
55+
if err != nil {
56+
s.log.Error(ctx, "failed to connect", slog.Error(err))
57+
}
58+
return true
59+
}),
60+
).Run(
61+
func() error {
62+
ctx, cancelFunc := context.WithTimeout(ctx, time.Second*15)
63+
defer cancelFunc()
64+
s.log.Info(ctx, "connecting to coder", slog.F("url", s.listenURL.String()))
65+
conn, resp, err := websocket.Dial(ctx, s.listenURL.String(), nil)
66+
if err != nil && resp == nil {
67+
return fmt.Errorf("dial: %w", err)
68+
}
69+
if err != nil && resp != nil {
70+
return &coder.HTTPError{
71+
Response: resp,
72+
}
73+
}
74+
nc := websocket.NetConn(context.Background(), conn, websocket.MessageBinary)
75+
session, err := yamux.Server(nc, nil)
6476
if err != nil {
65-
return fmt.Errorf("accept stream: %w", err)
77+
return fmt.Errorf("open: %w", err)
6678
}
67-
stream := &stream{
68-
logger: s.log.Named(fmt.Sprintf("stream %d", st.StreamID())),
69-
stream: st,
79+
s.log.Info(ctx, "connected to coder. awaiting connection requests")
80+
for {
81+
st, err := session.AcceptStream()
82+
if err != nil {
83+
return fmt.Errorf("accept stream: %w", err)
84+
}
85+
stream := &stream{
86+
logger: s.log.Named(fmt.Sprintf("stream %d", st.StreamID())),
87+
stream: st,
88+
}
89+
go stream.listen()
7090
}
71-
go stream.listen()
72-
}
73-
})
91+
})
7492

7593
return err
7694
}

internal/cmd/tunnel.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"golang.org/x/xerrors"
1818
"nhooyr.io/websocket"
1919

20+
"cdr.dev/coder-cli/coder-sdk"
2021
"cdr.dev/coder-cli/internal/x/xcobra"
2122
"cdr.dev/coder-cli/internal/x/xwebrtc"
2223
"cdr.dev/coder-cli/pkg/proto"
@@ -110,10 +111,15 @@ func (c *client) start() error {
110111
url := fmt.Sprintf("%s%s%s%s%s", c.brokerAddr, "/api/private/envagent/", c.id, "/connect?session_token=", c.token)
111112
c.logger.Info(c.ctx, "connecting to broker", slog.F("url", url))
112113

113-
conn, _, err := websocket.Dial(c.ctx, url, nil)
114-
if err != nil {
114+
conn, resp, err := websocket.Dial(c.ctx, url, nil)
115+
if err != nil && resp == nil {
115116
return fmt.Errorf("dial: %w", err)
116117
}
118+
if err != nil && resp != nil {
119+
return &coder.HTTPError{
120+
Response: resp,
121+
}
122+
}
117123
nconn := websocket.NetConn(context.Background(), conn, websocket.MessageBinary)
118124

119125
rtc, err := xwebrtc.NewPeerConnection()

0 commit comments

Comments
 (0)