Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit139f905

Browse files
committed
fix: use a background context when piping derp connections
This was causing boatloads of connects to reestablish every time...See#6746
1 parenteaacc26 commit139f905

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

‎coderd/workspaceagents.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,8 @@ func (api *API) workspaceAgentListeningPorts(rw http.ResponseWriter, r *http.Req
414414
httpapi.Write(ctx,rw,http.StatusOK,portsResponse)
415415
}
416416

417-
func (api*API)dialWorkspaceAgentTailnet(r*http.Request,agentID uuid.UUID) (*codersdk.WorkspaceAgentConn,error) {
418-
ctx:=r.Context()
417+
func (api*API)dialWorkspaceAgentTailnet(ctx context.Context,agentID uuid.UUID) (*codersdk.WorkspaceAgentConn,error) {
419418
clientConn,serverConn:=net.Pipe()
420-
421419
conn,err:=tailnet.NewConn(&tailnet.Options{
422420
Addresses: []netip.Prefix{netip.PrefixFrom(tailnet.IP(),128)},
423421
DERPMap:api.DERPMap,
@@ -437,7 +435,7 @@ func (api *API) dialWorkspaceAgentTailnet(r *http.Request, agentID uuid.UUID) (*
437435
deferleft.Close()
438436
deferright.Close()
439437
brw:=bufio.NewReadWriter(bufio.NewReader(right),bufio.NewWriter(right))
440-
api.DERPServer.Accept(ctx,right,brw,r.RemoteAddr)
438+
api.DERPServer.Accept(ctx,right,brw,"internal")
441439
}()
442440
returnleft
443441
})
@@ -460,7 +458,7 @@ func (api *API) dialWorkspaceAgentTailnet(r *http.Request, agentID uuid.UUID) (*
460458
gofunc() {
461459
err:= (*api.TailnetCoordinator.Load()).ServeClient(serverConn,uuid.New(),agentID)
462460
iferr!=nil {
463-
api.Logger.Warn(r.Context(),"tailnet coordinator client error",slog.Error(err))
461+
api.Logger.Warn(ctx,"tailnet coordinator client error",slog.Error(err))
464462
_=agentConn.Close()
465463
}
466464
}()

‎coderd/wsconncache/wsconncache.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@ func New(dialer Dialer, inactiveTimeout time.Duration) *Cache {
2424
ifinactiveTimeout==0 {
2525
inactiveTimeout=5*time.Minute
2626
}
27+
ctx,cancelFunc:=context.WithCancel(context.Background())
2728
return&Cache{
29+
closeContext:ctx,
30+
closeCancel:cancelFunc,
2831
closed:make(chanstruct{}),
2932
dialer:dialer,
3033
inactiveTimeout:inactiveTimeout,
3134
}
3235
}
3336

3437
// Dialer creates a new agent connection by ID.
35-
typeDialerfunc(r*http.Request,id uuid.UUID) (*codersdk.WorkspaceAgentConn,error)
38+
typeDialerfunc(ctx context.Context,id uuid.UUID) (*codersdk.WorkspaceAgentConn,error)
3639

3740
// Conn wraps an agent connection with a reusable HTTP transport.
3841
typeConnstruct {
@@ -66,6 +69,8 @@ type Cache struct {
6669
closedchanstruct{}
6770
closeMutex sync.Mutex
6871
closeGroup sync.WaitGroup
72+
closeContext context.Context
73+
closeCancel context.CancelFunc
6974
connGroup singleflight.Group
7075
connMap sync.Map
7176
dialerDialer
@@ -95,7 +100,7 @@ func (c *Cache) Acquire(r *http.Request, id uuid.UUID) (*Conn, func(), error) {
95100
}
96101
c.closeGroup.Add(1)
97102
c.closeMutex.Unlock()
98-
agentConn,err:=c.dialer(r,id)
103+
agentConn,err:=c.dialer(c.closeContext,id)
99104
iferr!=nil {
100105
c.closeGroup.Done()
101106
returnnil,xerrors.Errorf("dial: %w",err)
@@ -161,6 +166,7 @@ func (c *Cache) Close() error {
161166
default:
162167
}
163168
close(c.closed)
169+
c.closeCancel()
164170
c.closeGroup.Wait()
165171
returnnil
166172
}

‎coderd/wsconncache/wsconncache_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestCache(t *testing.T) {
4040
t.Parallel()
4141
t.Run("Same",func(t*testing.T) {
4242
t.Parallel()
43-
cache:=wsconncache.New(func(r*http.Request,id uuid.UUID) (*codersdk.WorkspaceAgentConn,error) {
43+
cache:=wsconncache.New(func(_ context.Context,id uuid.UUID) (*codersdk.WorkspaceAgentConn,error) {
4444
returnsetupAgent(t, agentsdk.Metadata{},0),nil
4545
},0)
4646
deferfunc() {
@@ -55,7 +55,7 @@ func TestCache(t *testing.T) {
5555
t.Run("Expire",func(t*testing.T) {
5656
t.Parallel()
5757
called:=atomic.NewInt32(0)
58-
cache:=wsconncache.New(func(r*http.Request,id uuid.UUID) (*codersdk.WorkspaceAgentConn,error) {
58+
cache:=wsconncache.New(func(_ context.Context,id uuid.UUID) (*codersdk.WorkspaceAgentConn,error) {
5959
called.Add(1)
6060
returnsetupAgent(t, agentsdk.Metadata{},0),nil
6161
},time.Microsecond)
@@ -74,7 +74,7 @@ func TestCache(t *testing.T) {
7474
})
7575
t.Run("NoExpireWhenLocked",func(t*testing.T) {
7676
t.Parallel()
77-
cache:=wsconncache.New(func(r*http.Request,id uuid.UUID) (*codersdk.WorkspaceAgentConn,error) {
77+
cache:=wsconncache.New(func(_ context.Context,id uuid.UUID) (*codersdk.WorkspaceAgentConn,error) {
7878
returnsetupAgent(t, agentsdk.Metadata{},0),nil
7979
},time.Microsecond)
8080
deferfunc() {
@@ -107,7 +107,7 @@ func TestCache(t *testing.T) {
107107
}()
108108
goserver.Serve(random)
109109

110-
cache:=wsconncache.New(func(r*http.Request,id uuid.UUID) (*codersdk.WorkspaceAgentConn,error) {
110+
cache:=wsconncache.New(func(_ context.Context,id uuid.UUID) (*codersdk.WorkspaceAgentConn,error) {
111111
returnsetupAgent(t, agentsdk.Metadata{},0),nil
112112
},time.Microsecond)
113113
deferfunc() {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp