diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go index 2ccbdea51..f3415c086 100644 --- a/internal/gui/sessions.go +++ b/internal/gui/sessions.go @@ -151,6 +151,7 @@ func sessionRoutes(mux *http.ServeMux, w Windows) { // (every day when 0 or none), cut by agent, folder and model for the // page to filter; names are the agents' as the page shows them. mux.HandleFunc("GET /api/sessions/stats", func(rw http.ResponseWriter, r *http.Request) { + defer sessions.ShareListing()() n, _ := strconv.Atoi(r.URL.Query().Get("days")) st := statsFor(n) names := map[string]string{} @@ -175,6 +176,7 @@ func sessionRoutes(mux *http.ServeMux, w Windows) { // how many, what the middle one spent, how many each day, and the ones // that spent the most. mux.HandleFunc("GET /api/sessions/overview", func(rw http.ResponseWriter, r *http.Request) { + defer sessions.ShareListing()() q := r.URL.Query() n, _ := strconv.Atoi(q.Get("days")) st := statsFor(n) @@ -470,7 +472,12 @@ func statsFor(days int) sessions.Stats { k.used = statsMemo.asks if !k.busy && time.Since(k.at) > 10*time.Second { k.busy = true + // read beside the page's own reads: with their listing of + // the session files, and handed back once they all rest + shared, rest := sessions.ShareListing(), readingPage() go func() { + defer rest() + defer shared() st := combinedSessionStats(days) statsMemo.Lock() k.at, k.st, k.busy = time.Now(), st, false diff --git a/internal/sessions/one_listing.go b/internal/sessions/one_listing.go index fb7632060..54a74c752 100644 --- a/internal/sessions/one_listing.go +++ b/internal/sessions/one_listing.go @@ -15,12 +15,15 @@ var held struct { sync.Mutex n int since time.Time // when the latest hold began - lists map[string]heldList + lists map[string]*heldList } type heldList struct { at time.Time files []file + // closed once files is in: a read that comes while the folder is + // being listed waits for that listing rather than taking its own + ready chan struct{} } // OneListing has the session folders listed once until done is called: @@ -32,6 +35,25 @@ func OneListing() (done func()) { held.n++ held.since = time.Now() held.Unlock() + return letGo() +} + +// ShareListing is OneListing for a read that may see the listings the +// holds there are now share, taken before it came: it begins a hold only +// when there is none. The Usage page's Sessions tab asks for the list, +// the stats and the overview at once, and the stats, read again behind +// (statsFor), listed every session file a second time beside the list's +// own listing. +func ShareListing() (done func()) { + held.Lock() + defer held.Unlock() + if held.n++; held.n == 1 { + held.since = time.Now() + } + return letGo() +} + +func letGo() func() { return sync.OnceFunc(func() { held.Lock() if held.n--; held.n == 0 { @@ -41,8 +63,12 @@ func OneListing() (done func()) { }) } +// heldWaiting is a test's: called as a read waits for another's listing. +var heldWaiting func() + // heldFiles is list(), or, under a hold, the listing of key taken since -// the latest hold began. The folders are listed outside the lock. +// the latest hold began. The folders are listed outside the lock, once +// for the reads that ask meanwhile. func heldFiles(key string, list func() []file) []file { held.Lock() if held.n == 0 { @@ -51,18 +77,21 @@ func heldFiles(key string, list func() []file) []file { } if l, ok := held.lists[key]; ok && !l.at.Before(held.since) { held.Unlock() + if heldWaiting != nil { + heldWaiting() + } + <-l.ready return slices.Clone(l.files) } - held.Unlock() - at := time.Now() - files := list() - held.Lock() - if held.n > 0 && !at.Before(held.since) { - if held.lists == nil { - held.lists = map[string]heldList{} - } - held.lists[key] = heldList{at, slices.Clone(files)} + mine := &heldList{at: time.Now(), ready: make(chan struct{})} + if held.lists == nil { + held.lists = map[string]*heldList{} } + held.lists[key] = mine held.Unlock() + defer close(mine.ready) + files := list() + // before ready is closed, so whoever waited reads it + mine.files = slices.Clone(files) return files } diff --git a/internal/sessions/one_listing_test.go b/internal/sessions/one_listing_test.go index 428394f5e..2fc31ed21 100644 --- a/internal/sessions/one_listing_test.go +++ b/internal/sessions/one_listing_test.go @@ -1,6 +1,9 @@ package sessions -import "testing" +import ( + "sync/atomic" + "testing" +) // Under a hold a folder is listed once; a hold begun after a listing has // it listed again, so no request reads files from before it came; with @@ -46,3 +49,55 @@ func TestOneListing(t *testing.T) { t.Fatalf("holds left: %d, lists %v", held.n, held.lists) } } + +// A read that comes while a folder is being listed under a hold waits for +// that listing; a shared hold reads the listing a hold before it took, and +// begins one of its own when there is none. +func TestShareListing(t *testing.T) { + var n atomic.Int32 + listing, release := make(chan struct{}), make(chan struct{}) + slow := func() []file { + if n.Add(1) == 1 { + close(listing) + <-release + } + return []file{{path: "a"}} + } + waiting := make(chan struct{}, 1) + heldWaiting = func() { waiting <- struct{}{} } + t.Cleanup(func() { heldWaiting = nil }) + done := OneListing() + got := make(chan []file, 2) + go func() { got <- heldFiles("k", slow) }() + <-listing + shared := ShareListing() + go func() { got <- heldFiles("k", slow) }() + select { + case <-waiting: + case <-got: + t.Fatal("a read during the listing didn't wait for it") + } + close(release) + for range 2 { + if fs := <-got; len(fs) != 1 || fs[0].path != "a" { + t.Fatalf("heldFiles = %v", fs) + } + } + if n.Load() != 1 { + t.Fatalf("listed %d times for a read during the listing and a shared hold, want 1", n.Load()) + } + done() + shared() + + n.Store(1) // listing again is quick + shared = ShareListing() + heldFiles("k", slow) + heldFiles("k", slow) + shared() + if n.Load() != 2 { + t.Fatalf("a shared hold with none before it listed %d times, want 1", n.Load()-1) + } + if held.n != 0 || held.lists != nil { + t.Fatalf("holds left: %d, lists %v", held.n, held.lists) + } +}