all: Use new Go 1.19 atomic types (#8772)

This commit is contained in:
greatroar
2023-02-07 12:07:34 +01:00
committed by GitHub
parent 882b711958
commit 38f2b34d29
19 changed files with 94 additions and 121 deletions
+17 -21
View File
@@ -10,53 +10,49 @@ import (
type countingReader struct {
io.Reader
tot int64 // bytes (atomic, must remain 64-bit aligned)
last int64 // unix nanos (atomic, must remain 64-bit aligned)
tot atomic.Int64 // bytes
last atomic.Int64 // unix nanos
}
var (
totalIncoming int64
totalOutgoing int64
totalIncoming atomic.Int64
totalOutgoing atomic.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())
c.tot.Add(int64(n))
totalIncoming.Add(int64(n))
c.last.Store(time.Now().UnixNano())
return n, err
}
func (c *countingReader) Tot() int64 {
return atomic.LoadInt64(&c.tot)
}
func (c *countingReader) Tot() int64 { return c.tot.Load() }
func (c *countingReader) Last() time.Time {
return time.Unix(0, atomic.LoadInt64(&c.last))
return time.Unix(0, c.last.Load())
}
type countingWriter struct {
io.Writer
tot int64 // bytes (atomic, must remain 64-bit aligned)
last int64 // unix nanos (atomic, must remain 64-bit aligned)
tot atomic.Int64 // bytes
last atomic.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())
c.tot.Add(int64(n))
totalOutgoing.Add(int64(n))
c.last.Store(time.Now().UnixNano())
return n, err
}
func (c *countingWriter) Tot() int64 {
return atomic.LoadInt64(&c.tot)
}
func (c *countingWriter) Tot() int64 { return c.tot.Load() }
func (c *countingWriter) Last() time.Time {
return time.Unix(0, atomic.LoadInt64(&c.last))
return time.Unix(0, c.last.Load())
}
func TotalInOut() (int64, int64) {
return atomic.LoadInt64(&totalIncoming), atomic.LoadInt64(&totalOutgoing)
return totalIncoming.Load(), totalOutgoing.Load()
}