Source File
periodic_timer.go
Belonging Package
github.com/pion/turn/v4/internal/client
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>// SPDX-License-Identifier: MITpackage clientimport ()// PeriodicTimerTimeoutHandler is a handler called on timeout.type PeriodicTimerTimeoutHandler func(timerID int)// PeriodicTimer is a periodic timer.type PeriodicTimer struct {id intinterval time.DurationtimeoutHandler PeriodicTimerTimeoutHandlerstopFunc func()mutex sync.RWMutex}// NewPeriodicTimer create a new timer.func ( int, PeriodicTimerTimeoutHandler, time.Duration) *PeriodicTimer {return &PeriodicTimer{id: ,interval: ,timeoutHandler: ,}}// Start starts the timer.func ( *PeriodicTimer) () bool {.mutex.Lock()defer .mutex.Unlock()// This is a noop if the timer is always runningif .stopFunc != nil {return false}:= make(chan struct{})go func() {:= falsefor ! {:= time.NewTimer(.interval)select {case <-.C:.timeoutHandler(.id)case <-:= true.Stop()}}}().stopFunc = func() {close()}return true}// Stop stops the timer.func ( *PeriodicTimer) () {.mutex.Lock()defer .mutex.Unlock()if .stopFunc != nil {.stopFunc().stopFunc = nil}}// IsRunning tests if the timer is running.// Debug purpose only.func ( *PeriodicTimer) () bool {.mutex.RLock()defer .mutex.RUnlock()return (.stopFunc != nil)}
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |