diff --git a/drive/driveimpl/compositedav/children_test.go b/drive/driveimpl/compositedav/children_test.go new file mode 100644 index 000000000..cffe3c42a --- /dev/null +++ b/drive/driveimpl/compositedav/children_test.go @@ -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) + } +} diff --git a/drive/driveimpl/compositedav/compositedav.go b/drive/driveimpl/compositedav/compositedav.go index d4edbc523..52ff1d8f4 100644 --- a/drive/driveimpl/compositedav/compositedav.go +++ b/drive/driveimpl/compositedav/compositedav.go @@ -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()