syncthing/lib/protocol/counting.go

63 lines
1.3 KiB
Go
Raw Normal View History

2015-01-13 12:31:14 +00:00
// Copyright (C) 2014 The Protocol Authors.
2014-09-22 19:42:11 +00:00
package protocol
import (
"io"
"sync/atomic"
"time"
)
type countingReader struct {
io.Reader
idString string
tot atomic.Int64 // bytes
last atomic.Int64 // unix nanos
2014-09-22 19:42:11 +00:00
}
var (
totalIncoming atomic.Int64
totalOutgoing atomic.Int64
2014-09-22 19:42:11 +00:00
)
func (c *countingReader) Read(bs []byte) (int, error) {
n, err := c.Reader.Read(bs)
c.tot.Add(int64(n))
totalIncoming.Add(int64(n))
c.last.Store(time.Now().UnixNano())
metricDeviceRecvBytes.WithLabelValues(c.idString).Add(float64(n))
2014-09-22 19:42:11 +00:00
return n, err
}
func (c *countingReader) Tot() int64 { return c.tot.Load() }
2014-09-22 19:42:11 +00:00
func (c *countingReader) Last() time.Time {
return time.Unix(0, c.last.Load())
2014-09-22 19:42:11 +00:00
}
type countingWriter struct {
io.Writer
idString string
tot atomic.Int64 // bytes
last atomic.Int64 // unix nanos
2014-09-22 19:42:11 +00:00
}
func (c *countingWriter) Write(bs []byte) (int, error) {
n, err := c.Writer.Write(bs)
c.tot.Add(int64(n))
totalOutgoing.Add(int64(n))
c.last.Store(time.Now().UnixNano())
metricDeviceSentBytes.WithLabelValues(c.idString).Add(float64(n))
2014-09-22 19:42:11 +00:00
return n, err
}
func (c *countingWriter) Tot() int64 { return c.tot.Load() }
2014-09-22 19:42:11 +00:00
func (c *countingWriter) Last() time.Time {
return time.Unix(0, c.last.Load())
2014-09-22 19:42:11 +00:00
}
func TotalInOut() (int64, int64) {
return totalIncoming.Load(), totalOutgoing.Load()
2014-09-22 19:42:11 +00:00
}