mirror of
https://dev.narayana.im/narayana/telegabber.git
synced 2026-08-05 04:07:07 +00:00
48 lines
905 B
Go
48 lines
905 B
Go
package signaling
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// AfterFunc-backed Timers, one per kind, firing Bridge.Timeout.
|
|
// Wiring is two-step because Bridge and timers want each other at
|
|
// construction: NewStdTimers, New(...), SetBridge.
|
|
type StdTimers struct {
|
|
mu sync.Mutex
|
|
bridge *Bridge
|
|
t map[TimerKind]*time.Timer
|
|
}
|
|
|
|
func NewStdTimers() *StdTimers {
|
|
return &StdTimers{t: make(map[TimerKind]*time.Timer)}
|
|
}
|
|
|
|
func (s *StdTimers) SetBridge(b *Bridge) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.bridge = b
|
|
}
|
|
|
|
func (s *StdTimers) Set(kind TimerKind, dur time.Duration) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if old, ok := s.t[kind]; ok {
|
|
old.Stop()
|
|
}
|
|
b := s.bridge
|
|
s.t[kind] = time.AfterFunc(dur, func() {
|
|
if b != nil {
|
|
b.Timeout(kind)
|
|
}
|
|
})
|
|
}
|
|
|
|
func (s *StdTimers) Cancel(kind TimerKind) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if old, ok := s.t[kind]; ok {
|
|
old.Stop()
|
|
delete(s.t, kind)
|
|
}
|
|
}
|