github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/common/rw/count.go (about) 1 package rw 2 3 import ( 4 "io" 5 "sync/atomic" 6 ) 7 8 type ReadCounter struct { 9 io.Reader 10 count int64 11 } 12 13 func (r *ReadCounter) Read(p []byte) (n int, err error) { 14 n, err = r.Reader.Read(p) 15 if n > 0 { 16 atomic.AddInt64(&r.count, int64(n)) 17 } 18 return 19 } 20 21 func (r *ReadCounter) Count() int64 { 22 return r.count 23 } 24 25 func (r *ReadCounter) Reset() { 26 atomic.StoreInt64(&r.count, 0) 27 }