refactor(wake): убрать неиспользуемый Notifier
This commit is contained in:
1 parent
273a5fa977
commit
7e5b2d4cde
3 files changed
+21
-76
No files matched your search
@@ -225,7 +225,7 @@ func (s *Session) Run(ctx context.Context) (err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
bg.Go(guard(func() {
|
bg.Go(guard(func() {
|
||||||
wake.New().Watch(runCtx, wakeTick, wakeThreshold, func(gap time.Duration) {
|
wake.Watch(runCtx, wakeTick, wakeThreshold, func(gap time.Duration) {
|
||||||
log.Warnf("device slept for %s - checking TURN allocations", gap.Truncate(time.Second))
|
log.Warnf("device slept for %s - checking TURN allocations", gap.Truncate(time.Second))
|
||||||
s.Wake()
|
s.Wake()
|
||||||
})
|
})
|
||||||
|
|||||||
+1
-43
@@ -1,52 +1,11 @@
|
|||||||
// Package wake оповещает компоненты о выходе устройства из режима сна.
|
|
||||||
package wake
|
package wake
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Notifier рассылает сигнал пробуждения слушателям.
|
func Watch(ctx context.Context, tick, threshold time.Duration, onGap func(time.Duration)) {
|
||||||
type Notifier struct {
|
|
||||||
mu sync.Mutex
|
|
||||||
ch chan struct{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func New() *Notifier {
|
|
||||||
return &Notifier{ch: make(chan struct{})}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Chan возвращает канал, закрывающийся при следующем вызове Fire (nil для nil-ресивера).
|
|
||||||
func (n *Notifier) Chan() <-chan struct{} {
|
|
||||||
if n == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
n.mu.Lock()
|
|
||||||
defer n.mu.Unlock()
|
|
||||||
n.initLocked()
|
|
||||||
return n.ch
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *Notifier) Fire() {
|
|
||||||
if n == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
n.mu.Lock()
|
|
||||||
defer n.mu.Unlock()
|
|
||||||
n.initLocked()
|
|
||||||
close(n.ch)
|
|
||||||
n.ch = make(chan struct{})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *Notifier) initLocked() {
|
|
||||||
if n.ch == nil {
|
|
||||||
n.ch = make(chan struct{})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Watch отслеживает разрывы времени между тиками и вызывает Fire при обнаружении сна.
|
|
||||||
func (n *Notifier) Watch(ctx context.Context, tick, threshold time.Duration, onGap func(time.Duration)) {
|
|
||||||
t := time.NewTicker(tick)
|
t := time.NewTicker(tick)
|
||||||
defer t.Stop()
|
defer t.Stop()
|
||||||
|
|
||||||
@@ -66,7 +25,6 @@ func (n *Notifier) Watch(ctx context.Context, tick, threshold time.Duration, onG
|
|||||||
if onGap != nil {
|
if onGap != nil {
|
||||||
onGap(gap)
|
onGap(gap)
|
||||||
}
|
}
|
||||||
n.Fire()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+19
-32
@@ -1,50 +1,37 @@
|
|||||||
package wake
|
package wake
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFireWakesAllListeners(t *testing.T) {
|
func TestWatchStopsOnContext(t *testing.T) {
|
||||||
n := New()
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
a, b := n.Chan(), n.Chan()
|
done := make(chan struct{})
|
||||||
n.Fire()
|
go func() {
|
||||||
|
Watch(ctx, time.Millisecond, time.Hour, nil)
|
||||||
|
close(done)
|
||||||
|
}()
|
||||||
|
|
||||||
for i, ch := range []<-chan struct{}{a, b} {
|
cancel()
|
||||||
select {
|
select {
|
||||||
case <-ch:
|
case <-done:
|
||||||
case <-time.After(time.Second):
|
case <-time.After(time.Second):
|
||||||
t.Fatalf("listener %d did not wake", i)
|
t.Fatal("Watch did not return after context cancel")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestChanIsRenewedAfterFire(t *testing.T) {
|
func TestWatchIgnoresGapsBelowThreshold(t *testing.T) {
|
||||||
n := New()
|
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
|
||||||
n.Fire()
|
defer cancel()
|
||||||
|
|
||||||
|
fired := make(chan time.Duration, 1)
|
||||||
|
Watch(ctx, time.Millisecond, time.Hour, func(gap time.Duration) { fired <- gap })
|
||||||
|
|
||||||
next := n.Chan()
|
|
||||||
select {
|
select {
|
||||||
case <-next:
|
case gap := <-fired:
|
||||||
t.Fatal("fresh channel is already closed")
|
t.Fatalf("onGap fired without a sleep gap: %s", gap)
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
n.Fire()
|
|
||||||
select {
|
|
||||||
case <-next:
|
|
||||||
case <-time.After(time.Second):
|
|
||||||
t.Fatal("second Fire did not reach the fresh channel")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNilNotifierBlocksForever(t *testing.T) {
|
|
||||||
var n *Notifier
|
|
||||||
n.Fire()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-n.Chan():
|
|
||||||
t.Fatal("nil notifier produced a ready channel")
|
|
||||||
case <-time.After(10 * time.Millisecond):
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user