From c42398b5b7a95b8ef13ea71c57cd436199445533 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Thu, 5 Oct 2023 16:26:06 -0700 Subject: [PATCH] ipn/ipnlocal: cleanup incomingFile (#9678) This is being moved to taildrop, so clean it up to stop depending on so much unreleated functionality by removing a dependency on peerAPIHandler. Updates tailscale/corp#14772 Signed-off-by: Joe Tsai Co-authored-by: Rhea Ghosh --- ipn/ipnlocal/peerapi.go | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/ipn/ipnlocal/peerapi.go b/ipn/ipnlocal/peerapi.go index d0e15681c..71ce4f50f 100644 --- a/ipn/ipnlocal/peerapi.go +++ b/ipn/ipnlocal/peerapi.go @@ -39,6 +39,7 @@ import ( "tailscale.com/net/sockstats" "tailscale.com/tailcfg" "tailscale.com/taildrop" + "tailscale.com/tstime" "tailscale.com/types/views" "tailscale.com/util/clientmetric" "tailscale.com/version/distro" @@ -586,12 +587,14 @@ func (h *peerAPIHandler) handleServeSockStats(w http.ResponseWriter, r *http.Req } type incomingFile struct { - name string // "foo.jpg" - started time.Time - size int64 // or -1 if unknown; never 0 - w io.Writer // underlying writer - ph *peerAPIHandler - partialPath string // non-empty in direct mode + clock tstime.Clock + + name string // "foo.jpg" + started time.Time + size int64 // or -1 if unknown; never 0 + w io.Writer // underlying writer + sendFileNotify func() // called when done + partialPath string // non-empty in direct mode mu sync.Mutex copied int64 @@ -603,25 +606,23 @@ func (f *incomingFile) markAndNotifyDone() { f.mu.Lock() f.done = true f.mu.Unlock() - b := f.ph.ps.b - b.sendFileNotify() + f.sendFileNotify() } func (f *incomingFile) Write(p []byte) (n int, err error) { n, err = f.w.Write(p) - b := f.ph.ps.b var needNotify bool defer func() { if needNotify { - b.sendFileNotify() + f.sendFileNotify() } }() if n > 0 { f.mu.Lock() defer f.mu.Unlock() f.copied += int64(n) - now := b.clock.Now() + now := f.clock.Now() if f.lastNotify.IsZero() || now.Sub(f.lastNotify) > time.Second { f.lastNotify = now needNotify = true @@ -760,11 +761,12 @@ func (h *peerAPIHandler) handlePeerPut(w http.ResponseWriter, r *http.Request) { var inFile *incomingFile if r.ContentLength != 0 { inFile = &incomingFile{ - name: baseName, - started: h.ps.b.clock.Now(), - size: r.ContentLength, - w: f, - ph: h, + clock: h.ps.b.clock, + name: baseName, + started: h.ps.b.clock.Now(), + size: r.ContentLength, + w: f, + sendFileNotify: h.ps.b.sendFileNotify, } if h.ps.taildrop.DirectFileMode { inFile.partialPath = partialFile