Every answered request asks Unretire (the gateway's servedCandidate), and
each catalog built asks retiredNow; each was an os.Stat of retired.json
under retired's lock. Only retired.go writes the file (writeRetired, for
Retire and Unretire), and it keeps retired.at and the file's time current
itself, so this process's own retirements are told at once. The file is
now looked at again only after retiredLook (1s): another magpie's write
is seen within it. A config path of its own (another HOME) is read at
once, as before.
Numbers (Apple M4 Pro, loaded by other sessions):
BenchmarkUnretire before 98.8 us/op 640 B/op 5 allocs/op
after 0.46 us/op 320 B/op 3 allocs/op
Test: TestRetiredLooksOnceInAWhile: 200 Unretire+Retired calls look at
the disk at most twice; Retire and Unretire are told at once; a write
another magpie makes is seen once retiredLook passes; a new config path
is read at once. Broken by hand (the throttle disabled): red, "400 looks
at retired.json for 200 requests".
Run: go test ./internal/provider -run TestRetiredLooksOnceInAWhile;
./internal/gateway -run 'Retired|Zen': ok. The full suite run is in the
last commit of this series.
86 lines
2.3 KiB
Go
86 lines
2.3 KiB
Go
package provider
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func retiredLooks() int {
|
|
retired.Lock()
|
|
defer retired.Unlock()
|
|
return retired.looks
|
|
}
|
|
|
|
// Every answered request asks Unretire (the gateway's servedCandidate):
|
|
// retired.json isn't looked at on disk for each, only once in retiredLook.
|
|
// What this process retires or brings back is told at once, another
|
|
// magpie's write within retiredLook, and a config path of its own (a new
|
|
// HOME) is read at once.
|
|
func TestRetiredLooksOnceInAWhile(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
before := retiredLooks()
|
|
for range 200 {
|
|
Unretire("p", "m")
|
|
if Retired("p", "m") {
|
|
t.Fatal("retired out of nothing")
|
|
}
|
|
}
|
|
if n := retiredLooks() - before; n > 2 {
|
|
t.Fatalf("%d looks at retired.json for 200 requests", n)
|
|
}
|
|
Retire("p", "m")
|
|
if !Retired("p", "m") {
|
|
t.Fatal("this process's retirement isn't told at once")
|
|
}
|
|
Unretire("p", "m")
|
|
if Retired("p", "m") {
|
|
t.Fatal("this process's unretirement isn't told at once")
|
|
}
|
|
|
|
// another magpie's write
|
|
defer func(d time.Duration) { retiredLook = d }(retiredLook)
|
|
retiredLook = 20 * time.Millisecond
|
|
b, _ := json.Marshal(map[string]time.Time{"p/other": time.Now()})
|
|
if err := os.MkdirAll(filepath.Dir(retiredPath()), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(retiredPath(), b, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
later := time.Now().Add(time.Minute)
|
|
os.Chtimes(retiredPath(), later, later)
|
|
for deadline := time.Now().Add(10 * time.Second); !Retired("p", "other"); time.Sleep(5 * time.Millisecond) {
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("another magpie's write isn't seen")
|
|
}
|
|
}
|
|
|
|
// a config of its own is read at once
|
|
home2 := t.TempDir()
|
|
t.Setenv("HOME", home2)
|
|
t.Setenv("USERPROFILE", home2)
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
retiredLook = time.Hour
|
|
if Retired("p", "other") {
|
|
t.Fatal("another config's retirement told")
|
|
}
|
|
}
|
|
|
|
// BenchmarkUnretire is what an answered request's Unretire costs it.
|
|
func BenchmarkUnretire(b *testing.B) {
|
|
home := b.TempDir()
|
|
b.Setenv("HOME", home)
|
|
b.Setenv("USERPROFILE", home)
|
|
b.Setenv("XDG_CONFIG_HOME", b.TempDir())
|
|
Retire("p", "x")
|
|
for b.Loop() {
|
|
Unretire("p", "m")
|
|
}
|
|
}
|