drive/driveimpl/compositedav: close idle connections of replaced children

SetChildren took oldChildren from its argument instead of from
h.children, so it closed the idle connections of the children it had
just set and left those of the replaced children open.

FileSystemForRemote builds a fresh Handler for every request from its
long-lived children, so idle connections to the local file server were
closed before every request and were generally not reused.

Updates #11187

Signed-off-by: Lim-jooyoung <[email protected]>
This commit is contained in:
Lim-jooyoung authored and Brad Fitzpatrick committed 2026-11-16 14:48:41 -08:00
1 parent 04ed834b36
commit cfc3f365d4
2 files changed
+114 -1

No files matched your search

@@ -0,0 +1,113 @@
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package compositedav
import (
"context"
"io"
"net"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"tailscale.com/drive/driveimpl/dirfs"
)
// connCounter counts the connections dialed and closed by a transport.
type connCounter struct {
dials atomic.Int32
closes atomic.Int32
}
type countedConn struct {
net.Conn
c *connCounter
closed atomic.Bool
}
func (cc *countedConn) Close() error {
if cc.closed.CompareAndSwap(false, true) {
cc.c.closes.Add(1)
}
return cc.Conn.Close()
}
func (c *connCounter) transport() *http.Transport {
return &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
var d net.Dialer
conn, err := d.DialContext(ctx, network, addr)
if err != nil {
return nil, err
}
c.dials.Add(1)
return &countedConn{Conn: conn, c: c}, nil
},
}
}
// mustGet sends a GET through h to the given path. ReverseProxy reads the
// child's response to EOF, which returns the connection to the idle pool.
func mustGet(t *testing.T, h *Handler, path string) {
t.Helper()
w := httptest.NewRecorder()
h.ServeHTTP(w, httptest.NewRequest("GET", path, nil))
if w.Code != http.StatusOK {
t.Fatalf("GET %s: got status %d, want %d", path, w.Code, http.StatusOK)
}
}
func TestSetChildrenClosesIdleConnectionsOfReplacedChildren(t *testing.T) {
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "contents")
}))
defer backend.Close()
newChild := func(c *connCounter) *Child {
return &Child{
Child: &dirfs.Child{Name: "share"},
BaseURL: func() (string, error) { return backend.URL, nil },
Transport: c.transport(),
}
}
var oldConns, newConns connCounter
oldChild := newChild(&oldConns)
defer oldChild.CloseIdleConnections()
replacement := newChild(&newConns)
defer replacement.CloseIdleConnections()
h := &Handler{}
h.SetChildren("", oldChild)
mustGet(t, h, "/share/file")
if got := oldConns.dials.Load(); got != 1 {
t.Fatalf("old child dialed %d connections, want 1", got)
}
// Replacing the children must close the idle connection of the child
// that was replaced.
h.SetChildren("", replacement)
if got := oldConns.closes.Load(); got != 1 {
t.Errorf("after SetChildren, old child closed %d connections, want 1", got)
}
mustGet(t, h, "/share/file")
if got := newConns.dials.Load(); got != 1 {
t.Fatalf("new child dialed %d connections, want 1", got)
}
// FileSystemForRemote builds a fresh Handler for every request from the
// same long-lived children. Setting them must not close their idle
// connections, so that the next request can reuse them.
h2 := &Handler{}
h2.SetChildren("", replacement)
if got := newConns.closes.Load(); got != 0 {
t.Errorf("SetChildren on a fresh Handler closed %d connections of the given children, want 0", got)
}
mustGet(t, h2, "/share/file")
if got := newConns.dials.Load(); got != 1 {
t.Errorf("after SetChildren on a fresh Handler, new child dialed %d connections in total, want 1 (reused)", got)
}
}
+1 -1
View File
@@ -202,7 +202,7 @@ func (h *Handler) SetChildren(staticRoot string, children ...*Child) {
})
h.childrenMu.Lock()
oldChildren := children
oldChildren := h.children
h.children = children
h.staticRoot = staticRoot
h.childrenMu.Unlock()