mirror of
https://github.com/octoleo/syncthing.git
synced 2024-12-30 13:40:35 +00:00
3450b5f80c
Integers are for numbers, enabling arithmetic like subtractions and for loops without getting shot in the foot. Unsigneds are for bitfields. - "int" for numbers that will always be laughably smaller than four billion, and where we don't care about the serialization format. - "int32" for numbers that will always be laughably smaller than four billion, and will be serialized to four bytes. - "int64" for numbers that may approach four billion or will be serialized to eight bytes. - "uint32" and "uint64" for bitfields, depending on required number of bits and serialization format. Likewise "uint8" and "uint16", although rare in this project since they don't exist in XDR. - "int8", "int16" and plain "uint" are almost never useful.
63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
// Copyright (C) 2014 The Protocol Authors.
|
|
|
|
package protocol
|
|
|
|
import (
|
|
"io"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
type countingReader struct {
|
|
io.Reader
|
|
tot int64 // bytes
|
|
last int64 // unix nanos
|
|
}
|
|
|
|
var (
|
|
totalIncoming int64
|
|
totalOutgoing int64
|
|
)
|
|
|
|
func (c *countingReader) Read(bs []byte) (int, error) {
|
|
n, err := c.Reader.Read(bs)
|
|
atomic.AddInt64(&c.tot, int64(n))
|
|
atomic.AddInt64(&totalIncoming, int64(n))
|
|
atomic.StoreInt64(&c.last, time.Now().UnixNano())
|
|
return n, err
|
|
}
|
|
|
|
func (c *countingReader) Tot() int64 {
|
|
return atomic.LoadInt64(&c.tot)
|
|
}
|
|
|
|
func (c *countingReader) Last() time.Time {
|
|
return time.Unix(0, atomic.LoadInt64(&c.last))
|
|
}
|
|
|
|
type countingWriter struct {
|
|
io.Writer
|
|
tot int64 // bytes
|
|
last int64 // unix nanos
|
|
}
|
|
|
|
func (c *countingWriter) Write(bs []byte) (int, error) {
|
|
n, err := c.Writer.Write(bs)
|
|
atomic.AddInt64(&c.tot, int64(n))
|
|
atomic.AddInt64(&totalOutgoing, int64(n))
|
|
atomic.StoreInt64(&c.last, time.Now().UnixNano())
|
|
return n, err
|
|
}
|
|
|
|
func (c *countingWriter) Tot() int64 {
|
|
return atomic.LoadInt64(&c.tot)
|
|
}
|
|
|
|
func (c *countingWriter) Last() time.Time {
|
|
return time.Unix(0, atomic.LoadInt64(&c.last))
|
|
}
|
|
|
|
func TotalInOut() (int64, int64) {
|
|
return atomic.LoadInt64(&totalIncoming), atomic.LoadInt64(&totalOutgoing)
|
|
}
|