8000 added a response_reader that tracks response body size by shmul · Pull Request #4 · miolini/datacounter · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

added a response_reader that tracks response body size #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/miolini/datacounter
module github.com/shmul/datacounter

go 1.11
36 changes: 36 additions & 0 deletions response_reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package datacounter

import (
"io"
"sync/atomic"
)

// ResponseBodyCounter is counter for ReadCloser primarily targeted at wrapping http.Request Body
type ResponseBodyCounter struct {
io.ReadCloser
count uint64
}

// NewResponseBodyCounter function for create new ResponseBodyCounter
func NewResponseBodyCounter(r io.ReadCloser) *ResponseBodyCounter {
return &ResponseBodyCounter{
ReadCloser: r,
}
}

// Read invokes the underlying Reader
func (counter *ResponseBodyCounter) Read(buf []byte) (int, error) {
n, err := counter.ReadCloser.Read(buf)
atomic.AddUint64(&counter.count, uint64(n))
return n, err
}

// Close invokes the underlying Closer
func (counter *ResponseBodyCounter) Close() error {
return counter.ReadCloser.Close()
}

// Count function return counted bytes
func (counter *ResponseBodyCounter) Count() uint64 {
return atomic.LoadUint64(&counter.count)
}
0