mirror of
https://github.com/zrepl/zrepl.git
synced 2024-11-22 08:23:50 +01:00
4f9b63aa09
- use control connection (gRPC) - use uint64 everywhere => fixes https://github.com/zrepl/zrepl/issues/463 - [BREAK] bump protocol version closes https://github.com/zrepl/zrepl/pull/518 fixes https://github.com/zrepl/zrepl/issues/463
43 lines
755 B
Go
43 lines
755 B
Go
package bytecounter
|
|
|
|
import (
|
|
"io"
|
|
"sync/atomic"
|
|
)
|
|
|
|
// ReadCloser wraps an io.ReadCloser, reimplementing
|
|
// its interface and counting the bytes written to during copying.
|
|
type ReadCloser interface {
|
|
io.ReadCloser
|
|
Count() uint64
|
|
}
|
|
|
|
// NewReadCloser wraps rc.
|
|
func NewReadCloser(rc io.ReadCloser) ReadCloser {
|
|
return &readCloser{rc, 0}
|
|
}
|
|
|
|
type readCloser struct {
|
|
rc io.ReadCloser
|
|
count uint64
|
|
}
|
|
|
|
func (r *readCloser) Count() uint64 {
|
|
return atomic.LoadUint64(&r.count)
|
|
}
|
|
|
|
var _ io.ReadCloser = &readCloser{}
|
|
|
|
func (r *readCloser) Close() error {
|
|
return r.rc.Close()
|
|
}
|
|
|
|
func (r *readCloser) Read(p []byte) (int, error) {
|
|
n, err := r.rc.Read(p)
|
|
if n < 0 {
|
|
panic("expecting n >= 0")
|
|
}
|
|
atomic.AddUint64(&r.count, uint64(n))
|
|
return n, err
|
|
}
|