mirror of
https://github.com/bluenviron/mediamtx.git
synced 2025-12-20 02:00:05 -08:00
Some checks are pending
code_lint / golangci_lint (push) Waiting to run
code_lint / mod_tidy (push) Waiting to run
code_lint / api_docs (push) Waiting to run
code_test / test_64 (push) Waiting to run
code_test / test_32 (push) Waiting to run
code_test / test_highlevel (push) Waiting to run
Discarded frames, decode errors and lost packets were logged individually, then there was a mechanism that prevented more than 1 log entry per second from being printed, resulting in inaccurate reports. Now discarded frames, decode errors and lost packets are accurately counted, and their count is printed once every second.
66 lines
1.2 KiB
Go
66 lines
1.2 KiB
Go
// Package counterdumper contains a counter that that periodically invokes a callback if the counter is not zero.
|
|
package counterdumper
|
|
|
|
import (
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
callbackPeriod = 1 * time.Second
|
|
)
|
|
|
|
// CounterDumper is a counter that periodically invokes a callback if the counter is not zero.
|
|
type CounterDumper struct {
|
|
OnReport func(v uint64)
|
|
|
|
counter *uint64
|
|
|
|
terminate chan struct{}
|
|
done chan struct{}
|
|
}
|
|
|
|
// Start starts the counter.
|
|
func (c *CounterDumper) Start() {
|
|
c.counter = new(uint64)
|
|
c.terminate = make(chan struct{})
|
|
c.done = make(chan struct{})
|
|
|
|
go c.run()
|
|
}
|
|
|
|
// Stop stops the counter.
|
|
func (c *CounterDumper) Stop() {
|
|
close(c.terminate)
|
|
<-c.done
|
|
}
|
|
|
|
// Increase increases the counter value by 1.
|
|
func (c *CounterDumper) Increase() {
|
|
atomic.AddUint64(c.counter, 1)
|
|
}
|
|
|
|
// Add adds value to the counter.
|
|
func (c *CounterDumper) Add(v uint64) {
|
|
atomic.AddUint64(c.counter, v)
|
|
}
|
|
|
|
func (c *CounterDumper) run() {
|
|
defer close(c.done)
|
|
|
|
t := time.NewTicker(callbackPeriod)
|
|
defer t.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-c.terminate:
|
|
return
|
|
|
|
case <-t.C:
|
|
v := atomic.SwapUint64(c.counter, 0)
|
|
if v != 0 {
|
|
c.OnReport(v)
|
|
}
|
|
}
|
|
}
|
|
}
|