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
|
2019-07-13 13:05:39 +00:00
|
|
|
tot int64 // bytes (atomic, must remain 64-bit aligned)
|
|
|
|
last int64 // unix nanos (atomic, must remain 64-bit aligned)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2015-01-18 00:26:52 +00:00
|
|
|
totalIncoming int64
|
|
|
|
totalOutgoing int64
|
2014-09-22 19:42:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *countingReader) Read(bs []byte) (int, error) {
|
|
|
|
n, err := c.Reader.Read(bs)
|
2015-01-18 00:26:52 +00:00
|
|
|
atomic.AddInt64(&c.tot, int64(n))
|
|
|
|
atomic.AddInt64(&totalIncoming, int64(n))
|
2014-09-22 19:42:11 +00:00
|
|
|
atomic.StoreInt64(&c.last, time.Now().UnixNano())
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2015-01-18 00:26:52 +00:00
|
|
|
func (c *countingReader) Tot() int64 {
|
|
|
|
return atomic.LoadInt64(&c.tot)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *countingReader) Last() time.Time {
|
|
|
|
return time.Unix(0, atomic.LoadInt64(&c.last))
|
|
|
|
}
|
|
|
|
|
|
|
|
type countingWriter struct {
|
|
|
|
io.Writer
|
2019-07-13 13:05:39 +00:00
|
|
|
tot int64 // bytes (atomic, must remain 64-bit aligned)
|
|
|
|
last int64 // unix nanos (atomic, must remain 64-bit aligned)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *countingWriter) Write(bs []byte) (int, error) {
|
|
|
|
n, err := c.Writer.Write(bs)
|
2015-01-18 00:26:52 +00:00
|
|
|
atomic.AddInt64(&c.tot, int64(n))
|
|
|
|
atomic.AddInt64(&totalOutgoing, int64(n))
|
2014-09-22 19:42:11 +00:00
|
|
|
atomic.StoreInt64(&c.last, time.Now().UnixNano())
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2015-01-18 00:26:52 +00:00
|
|
|
func (c *countingWriter) Tot() int64 {
|
|
|
|
return atomic.LoadInt64(&c.tot)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *countingWriter) Last() time.Time {
|
|
|
|
return time.Unix(0, atomic.LoadInt64(&c.last))
|
|
|
|
}
|
|
|
|
|
2015-01-18 00:26:52 +00:00
|
|
|
func TotalInOut() (int64, int64) {
|
|
|
|
return atomic.LoadInt64(&totalIncoming), atomic.LoadInt64(&totalOutgoing)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|