sessions: a Sessions poll's three reads share one listing of the files
The Sessions tab asks /api/sessions, /api/sessions/stats and /api/sessions/overview at once, and statsFor refreshes its memo in a goroutine beside them. Each listed every session file again. The stats and overview routes and the refresh now take sessions.ShareListing: a hold that, unlike OneListing, doesn't start a new "since" when one is already held, so a listing a read in flight is making is waited for and shared (heldFiles keeps a ready channel per listing) instead of made again. The refresh is also counted as a page read (readingPage), so its memory goes back once it rests. Run at this head: go test ./internal/sessions ./internal/gui ok, TestShareListing -race -count=5 ok. TestShareListing was red with the wait taken out of heldFiles by hand (the second read listed again). A/B on real session files, 8 polls of the parallel trio then /api/state, v0.1.1661 vs this: total allocated 1.96 -> 1.82 GB, trio mean 572 -> 438 ms, first trio 2071 -> 1456 ms, allFiles 353 -> 322 MB. Footprint between polls stayed ~160 MB; statsRead (~520 MB) and listSessions (~345 MB) are the rest and are not changed here. Rebased on65ee8c65cand run again: dirent and sessions ok; gui had one red, TestRestartHandsTheGatewayOver, with an A/B run of magpie beside it; it is #3451 (EOF during the handover under load). It passed 3 of 3 alone and the whole gui package passed on a rerun. Rebased again oncc9d68ffd(agent and update only) and run at the pushed head: dirent, sessions and gui ok.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user