awake: turned off, the setting is waited for, not looked at every 5s (#3185)
Keep awake is off unless the user turns it on, and with it off Keep still read settings.json on every 5s beat to see whether it had been. Only a settings write can turn it on, and every one goes through settings.save, which closes filememo.Written. Off, Keep now waits on that, and looks again once a minute (OthersEvery) for another process's write the watch missed. On, it keeps its 5s look, since a request starting is not a write. Run at this head: - go test -count=3 ./internal/awake ok. TestKeepOffWaitsForTheSetting is red on the old loop (52 looks in 50ms) and green here; TestKeep and TestKeepDisplay now write the setting as settings.save does. - gofmt clean; GOOS=linux / windows go vet -tags nogui ./internal/awake ok. - Real gateway, sandbox HOME, idle, 120s after 90s warm-up: with the watch alone 1.8 wakeups/s, with this too 1.3 (v0.1.1561: 2.1 to 2.7).
This commit is contained in:
+12
-2
@@ -23,6 +23,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/beat"
|
||||
"github.com/yetone/magpie/internal/filememo"
|
||||
)
|
||||
|
||||
// Grace is how long after the last request ended sleep is still held off.
|
||||
@@ -71,7 +72,8 @@ func Keep(ctx context.Context, level func() Level, state func() State) {
|
||||
defer let()
|
||||
failed := false
|
||||
for {
|
||||
if l := level(); l != Off && want(state(), time.Now()) {
|
||||
l := level()
|
||||
if l != Off && want(state(), time.Now()) {
|
||||
if release != nil && held != l {
|
||||
release()
|
||||
release = nil
|
||||
@@ -95,10 +97,18 @@ func Keep(ctx context.Context, level func() Level, state func() State) {
|
||||
let()
|
||||
failed = false
|
||||
}
|
||||
// turned off (as it is unless the user turns it on), only a change
|
||||
// to settings can turn it on: waited for, not looked for each few
|
||||
// seconds (#3185: these looks woke an idle magpie)
|
||||
every, changed := tick, (<-chan struct{})(nil)
|
||||
if l == Off {
|
||||
every, changed = filememo.OthersEvery(), filememo.Written()
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-beat.Next(tick):
|
||||
case <-changed:
|
||||
case <-beat.Next(every):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/filememo"
|
||||
)
|
||||
|
||||
func TestWant(t *testing.T) {
|
||||
@@ -39,7 +41,8 @@ func TestKeep(t *testing.T) {
|
||||
}
|
||||
var mu sync.Mutex
|
||||
on, st := true, State{}
|
||||
set := func(o bool, s State) { mu.Lock(); on, st = o, s; mu.Unlock() }
|
||||
// the setting is written as settings.Save writes it (filememo.Forget)
|
||||
set := func(o bool, s State) { mu.Lock(); on, st = o, s; mu.Unlock(); filememo.Forget() }
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
@@ -102,7 +105,7 @@ func TestKeepDisplay(t *testing.T) {
|
||||
return func() { mu.Lock(); live--; mu.Unlock() }, nil
|
||||
}
|
||||
level := System
|
||||
set := func(l Level) { mu.Lock(); level = l; mu.Unlock() }
|
||||
set := func(l Level) { mu.Lock(); level = l; mu.Unlock(); filememo.Forget() }
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
@@ -143,3 +146,44 @@ func TestKeepDisplay(t *testing.T) {
|
||||
cancel()
|
||||
<-done
|
||||
}
|
||||
|
||||
// Turned off, Keep looks at the setting again when it is written, not each
|
||||
// few seconds (#3185), and turning it on is still taken at once.
|
||||
func TestKeepOffWaitsForTheSetting(t *testing.T) {
|
||||
oldHold, oldTick := hold, tick
|
||||
defer func() { hold, tick = oldHold, oldTick }()
|
||||
tick = time.Millisecond
|
||||
var held atomic.Int32
|
||||
hold = func(bool) (func(), error) { held.Add(1); return func() { held.Add(-1) }, nil }
|
||||
var looks atomic.Int32
|
||||
var on atomic.Bool
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
Keep(ctx, func() Level {
|
||||
looks.Add(1)
|
||||
if on.Load() {
|
||||
return System
|
||||
}
|
||||
return Off
|
||||
}, func() State { return State{Busy: true} })
|
||||
close(done)
|
||||
}()
|
||||
defer func() { cancel(); <-done }()
|
||||
for end := time.Now().Add(2 * time.Second); looks.Load() == 0; time.Sleep(time.Millisecond) {
|
||||
if time.Now().After(end) {
|
||||
t.Fatal("Keep never looked at the setting")
|
||||
}
|
||||
}
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
if n := looks.Load(); n != 1 {
|
||||
t.Fatalf("turned off, Keep looked at the setting %d times in 50ms with nothing written", n)
|
||||
}
|
||||
on.Store(true)
|
||||
filememo.Forget()
|
||||
for end := time.Now().Add(2 * time.Second); held.Load() != 1; time.Sleep(time.Millisecond) {
|
||||
if time.Now().After(end) {
|
||||
t.Fatal("turning the setting on held nothing")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user