mirror of
https://github.com/bluenviron/mediamtx
synced 2024-12-14 10:45:08 +00:00
8bee4af86a
* API: number of bytes received/sent from/to RTSP connections * API: number of bytes received/sent from/to RTSP sessions * API: number of bytes received/sent from/to RTMP connections * API: number of bytes sent to HLS connections * API: number of bytes received from paths * metrics of all the above
37 lines
616 B
Go
37 lines
616 B
Go
package bytecounter
|
|
|
|
import (
|
|
"io"
|
|
"sync/atomic"
|
|
)
|
|
|
|
// Writer allows to count written bytes.
|
|
type Writer struct {
|
|
w io.Writer
|
|
count uint64
|
|
}
|
|
|
|
// NewWriter allocates a Writer.
|
|
func NewWriter(w io.Writer) *Writer {
|
|
return &Writer{
|
|
w: w,
|
|
}
|
|
}
|
|
|
|
// Write implements io.Writer.
|
|
func (w *Writer) Write(p []byte) (int, error) {
|
|
n, err := w.w.Write(p)
|
|
atomic.AddUint64(&w.count, uint64(n))
|
|
return n, err
|
|
}
|
|
|
|
// Count returns sent bytes.
|
|
func (w *Writer) Count() uint64 {
|
|
return atomic.LoadUint64(&w.count)
|
|
}
|
|
|
|
// SetCount sets sent bytes.
|
|
func (w *Writer) SetCount(v uint64) {
|
|
atomic.StoreUint64(&w.count, v)
|
|
}
|