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

fix: use a background context when piping derp connections#6750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
kylecarbs merged 1 commit intomainfrombgctxderp
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletionscoderd/workspaceagents.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -294,7 +294,7 @@ func (api *API) workspaceAgentPTY(rw http.ResponseWriter, r *http.Request) {

gohttpapi.Heartbeat(ctx,conn)

agentConn,release,err:=api.workspaceAgentCache.Acquire(r,workspaceAgent.ID)
agentConn,release,err:=api.workspaceAgentCache.Acquire(workspaceAgent.ID)
iferr!=nil {
_=conn.Close(websocket.StatusInternalError,httpapi.WebsocketCloseSprintf("dial workspace agent: %s",err))
return
Expand DownExpand Up@@ -339,7 +339,7 @@ func (api *API) workspaceAgentListeningPorts(rw http.ResponseWriter, r *http.Req
return
}

agentConn,release,err:=api.workspaceAgentCache.Acquire(r,workspaceAgent.ID)
agentConn,release,err:=api.workspaceAgentCache.Acquire(workspaceAgent.ID)
iferr!=nil {
httpapi.Write(ctx,rw,http.StatusInternalServerError, codersdk.Response{
Message:"Internal error dialing workspace agent.",
Expand DownExpand Up@@ -414,10 +414,8 @@ func (api *API) workspaceAgentListeningPorts(rw http.ResponseWriter, r *http.Req
httpapi.Write(ctx,rw,http.StatusOK,portsResponse)
}

func (api*API)dialWorkspaceAgentTailnet(r*http.Request,agentID uuid.UUID) (*codersdk.WorkspaceAgentConn,error) {
ctx:=r.Context()
func (api*API)dialWorkspaceAgentTailnet(agentID uuid.UUID) (*codersdk.WorkspaceAgentConn,error) {
clientConn,serverConn:=net.Pipe()

conn,err:=tailnet.NewConn(&tailnet.Options{
Addresses: []netip.Prefix{netip.PrefixFrom(tailnet.IP(),128)},
DERPMap:api.DERPMap,
Expand All@@ -428,6 +426,7 @@ func (api *API) dialWorkspaceAgentTailnet(r *http.Request, agentID uuid.UUID) (*
_=serverConn.Close()
returnnil,xerrors.Errorf("create tailnet conn: %w",err)
}
ctx,cancel:=context.WithCancel(api.ctx)
conn.SetDERPRegionDialer(func(_ context.Context,region*tailcfg.DERPRegion) net.Conn {
if!region.EmbeddedRelay {
returnnil
Expand All@@ -437,7 +436,7 @@ func (api *API) dialWorkspaceAgentTailnet(r *http.Request, agentID uuid.UUID) (*
deferleft.Close()
deferright.Close()
brw:=bufio.NewReadWriter(bufio.NewReader(right),bufio.NewWriter(right))
api.DERPServer.Accept(ctx,right,brw,r.RemoteAddr)
api.DERPServer.Accept(ctx,right,brw,"internal")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I wrote this on the previous version of this PR, but I guess the suggestion still applies:


How do we avoid leaks here? Isleft guaranteed to be closed at an appropriate time and propagate toright?

Should we actually doctx, cancel := context.WithCancel(api.ctx) outside here and callcancel() inagentConn.CloseFunc?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Ahh, good point! Will fix.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Fixed!

}()
returnleft
})
Expand All@@ -453,14 +452,15 @@ func (api *API) dialWorkspaceAgentTailnet(r *http.Request, agentID uuid.UUID) (*
agentConn:=&codersdk.WorkspaceAgentConn{
Conn:conn,
CloseFunc:func() {
cancel()
_=clientConn.Close()
_=serverConn.Close()
},
}
gofunc() {
err:= (*api.TailnetCoordinator.Load()).ServeClient(serverConn,uuid.New(),agentID)
iferr!=nil {
api.Logger.Warn(r.Context(),"tailnet coordinator client error",slog.Error(err))
api.Logger.Warn(ctx,"tailnet coordinator client error",slog.Error(err))
_=agentConn.Close()
}
}()
Expand Down
2 changes: 1 addition & 1 deletioncoderd/workspaceapps.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -639,7 +639,7 @@ func (api *API) proxyWorkspaceApplication(rw http.ResponseWriter, r *http.Reques
})
}

conn,release,err:=api.workspaceAgentCache.Acquire(r,ticket.AgentID)
conn,release,err:=api.workspaceAgentCache.Acquire(ticket.AgentID)
iferr!=nil {
site.RenderStaticErrorPage(rw,r, site.ErrorPageData{
Status:http.StatusBadGateway,
Expand Down
6 changes: 3 additions & 3 deletionscoderd/wsconncache/wsconncache.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -32,7 +32,7 @@ func New(dialer Dialer, inactiveTimeout time.Duration) *Cache {
}

// Dialer creates a new agent connection by ID.
typeDialerfunc(r*http.Request,id uuid.UUID) (*codersdk.WorkspaceAgentConn,error)
typeDialerfunc(id uuid.UUID) (*codersdk.WorkspaceAgentConn,error)

// Conn wraps an agent connection with a reusable HTTP transport.
typeConnstruct {
Expand DownExpand Up@@ -78,7 +78,7 @@ type Cache struct {
// The returned function is used to release a lock on the connection. Once zero
// locks exist on a connection, the inactive timeout will begin to tick down.
// After the time expires, the connection will be cleared from the cache.
func (c*Cache)Acquire(r*http.Request,id uuid.UUID) (*Conn,func(),error) {
func (c*Cache)Acquire(id uuid.UUID) (*Conn,func(),error) {
rawConn,found:=c.connMap.Load(id.String())
// If the connection isn't found, establish a new one!
if!found {
Expand All@@ -95,7 +95,7 @@ func (c *Cache) Acquire(r *http.Request, id uuid.UUID) (*Conn, func(), error) {
}
c.closeGroup.Add(1)
c.closeMutex.Unlock()
agentConn,err:=c.dialer(r,id)
agentConn,err:=c.dialer(id)
iferr!=nil {
c.closeGroup.Done()
returnnil,xerrors.Errorf("dial: %w",err)
Expand Down
20 changes: 10 additions & 10 deletionscoderd/wsconncache/wsconncache_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,47 +40,47 @@ func TestCache(t *testing.T) {
t.Parallel()
t.Run("Same",func(t*testing.T) {
t.Parallel()
cache:=wsconncache.New(func(r*http.Request,id uuid.UUID) (*codersdk.WorkspaceAgentConn,error) {
cache:=wsconncache.New(func(id uuid.UUID) (*codersdk.WorkspaceAgentConn,error) {
returnsetupAgent(t, agentsdk.Metadata{},0),nil
},0)
deferfunc() {
_=cache.Close()
}()
conn1,_,err:=cache.Acquire(httptest.NewRequest(http.MethodGet,"/",nil),uuid.Nil)
conn1,_,err:=cache.Acquire(uuid.Nil)
require.NoError(t,err)
conn2,_,err:=cache.Acquire(httptest.NewRequest(http.MethodGet,"/",nil),uuid.Nil)
conn2,_,err:=cache.Acquire(uuid.Nil)
require.NoError(t,err)
require.True(t,conn1==conn2)
})
t.Run("Expire",func(t*testing.T) {
t.Parallel()
called:=atomic.NewInt32(0)
cache:=wsconncache.New(func(r*http.Request,id uuid.UUID) (*codersdk.WorkspaceAgentConn,error) {
cache:=wsconncache.New(func(id uuid.UUID) (*codersdk.WorkspaceAgentConn,error) {
called.Add(1)
returnsetupAgent(t, agentsdk.Metadata{},0),nil
},time.Microsecond)
deferfunc() {
_=cache.Close()
}()
conn,release,err:=cache.Acquire(httptest.NewRequest(http.MethodGet,"/",nil),uuid.Nil)
conn,release,err:=cache.Acquire(uuid.Nil)
require.NoError(t,err)
release()
<-conn.Closed()
conn,release,err=cache.Acquire(httptest.NewRequest(http.MethodGet,"/",nil),uuid.Nil)
conn,release,err=cache.Acquire(uuid.Nil)
require.NoError(t,err)
release()
<-conn.Closed()
require.Equal(t,int32(2),called.Load())
})
t.Run("NoExpireWhenLocked",func(t*testing.T) {
t.Parallel()
cache:=wsconncache.New(func(r*http.Request,id uuid.UUID) (*codersdk.WorkspaceAgentConn,error) {
cache:=wsconncache.New(func(id uuid.UUID) (*codersdk.WorkspaceAgentConn,error) {
returnsetupAgent(t, agentsdk.Metadata{},0),nil
},time.Microsecond)
deferfunc() {
_=cache.Close()
}()
conn,release,err:=cache.Acquire(httptest.NewRequest(http.MethodGet,"/",nil),uuid.Nil)
conn,release,err:=cache.Acquire(uuid.Nil)
require.NoError(t,err)
time.Sleep(time.Millisecond)
release()
Expand All@@ -107,7 +107,7 @@ func TestCache(t *testing.T) {
}()
goserver.Serve(random)

cache:=wsconncache.New(func(r*http.Request,id uuid.UUID) (*codersdk.WorkspaceAgentConn,error) {
cache:=wsconncache.New(func(id uuid.UUID) (*codersdk.WorkspaceAgentConn,error) {
returnsetupAgent(t, agentsdk.Metadata{},0),nil
},time.Microsecond)
deferfunc() {
Expand All@@ -130,7 +130,7 @@ func TestCache(t *testing.T) {
defercancel()
req:=httptest.NewRequest(http.MethodGet,"/",nil)
req=req.WithContext(ctx)
conn,release,err:=cache.Acquire(req,uuid.Nil)
conn,release,err:=cache.Acquire(uuid.Nil)
if!assert.NoError(t,err) {
return
}
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp