mediamtx/internal/rtmp/bytecounter/reader.go
Alessandro Ros 8bee4af86a
api, metrics: add number of bytes received and sent from/to all entities (#1235)
* 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
2022-11-11 11:59:52 +01:00

37 lines
614 B
Go

package bytecounter
import (
"io"
"sync/atomic"
)
// Reader allows to count read bytes.
type Reader struct {
r io.Reader
count uint64
}
// NewReader allocates a Reader.
func NewReader(r io.Reader) *Reader {
return &Reader{
r: r,
}
}
// Read implements io.Reader.
func (r *Reader) Read(p []byte) (int, error) {
n, err := r.r.Read(p)
atomic.AddUint64(&r.count, uint64(n))
return n, err
}
// Count returns received bytes.
func (r *Reader) Count() uint64 {
return atomic.LoadUint64(&r.count)
}
// SetCount sets read bytes.
func (r *Reader) SetCount(v uint64) {
atomic.StoreUint64(&r.count, v)
}