pax_global_header00006660000000000000000000000064136460074330014520gustar00rootroot0000000000000052 comment=5f3b5c3c6de953d6f1e5654098d379e59524672e golang-github-miolini-datacounter-1.0.2/000077500000000000000000000000001364600743300202145ustar00rootroot00000000000000golang-github-miolini-datacounter-1.0.2/.gitignore000066400000000000000000000004151364600743300222040ustar00rootroot00000000000000# Binaries for programs and plugins *.exe *.exe~ *.dll *.so *.dylib # Test binary, built with `go test -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE *.out # Dependency directories (remove the comment below to include it) # vendor/ golang-github-miolini-datacounter-1.0.2/.travis.yml000066400000000000000000000003731364600743300223300ustar00rootroot00000000000000language: go sudo: false go: - "1.11" - "1.12" - "1.13" - "master" matrix: allow_failures: - go: master install: - go install - go get -u golang.org/x/lint/golint script: - go test -v ./... - diff <(golint *.go) <(printf "") golang-github-miolini-datacounter-1.0.2/LICENSE000066400000000000000000000021131364600743300212160ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015 Artem Andreenko Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. golang-github-miolini-datacounter-1.0.2/README.md000066400000000000000000000023221364600743300214720ustar00rootroot00000000000000# Datacounter Golang counters for readers/writers. [![Build Status](https://travis-ci.org/miolini/datacounter.svg)](https://travis-ci.org/miolini/datacounter) [![GoDoc](https://godoc.org/github.com/miolini/datacounter?status.svg)](http://godoc.org/github.com/miolini/datacounter) ## Examples ### ReaderCounter ```go buf := bytes.Buffer{} buf.Write(data) counter := datacounter.NewReaderCounter(&buf) io.Copy(ioutil.Discard, counter) if counter.Count() != dataLen { t.Fatalf("count mismatch len of test data: %d != %d", counter.Count(), len(data)) } ``` ### WriterCounter ```go buf := bytes.Buffer{} counter := datacounter.NewWriterCounter(&buf) counter.Write(data) if counter.Count() != dataLen { t.Fatalf("count mismatch len of test data: %d != %d", counter.Count(), len(data)) } ``` ### http.ResponseWriter Counter ```go handler := func(w http.ResponseWriter, r *http.Request) { w.Write(data) } req, err := http.NewRequest("GET", "http://example.com/foo", nil) if err != nil { t.Fatal(err) } w := httptest.NewRecorder() counter := datacounter.NewResponseWriterCounter(w) handler(counter, req) if counter.Count() != dataLen { t.Fatalf("count mismatch len of test data: %d != %d", counter.Count(), len(data)) } ``` golang-github-miolini-datacounter-1.0.2/go.mod000066400000000000000000000000571364600743300213240ustar00rootroot00000000000000module github.com/miolini/datacounter go 1.11 golang-github-miolini-datacounter-1.0.2/reader.go000066400000000000000000000011241364600743300220030ustar00rootroot00000000000000package datacounter import ( "io" "sync/atomic" ) // ReaderCounter is counter for io.Reader type ReaderCounter struct { io.Reader count uint64 } // NewReaderCounter function for create new ReaderCounter func NewReaderCounter(r io.Reader) *ReaderCounter { return &ReaderCounter{ Reader: r, } } func (counter *ReaderCounter) Read(buf []byte) (int, error) { n, err := counter.Reader.Read(buf) atomic.AddUint64(&counter.count, uint64(n)) return n, err } // Count function return counted bytes func (counter *ReaderCounter) Count() uint64 { return atomic.LoadUint64(&counter.count) } golang-github-miolini-datacounter-1.0.2/reader_test.go000066400000000000000000000006321364600743300230450ustar00rootroot00000000000000package datacounter import ( "bytes" "io" "io/ioutil" "testing" ) var data = []byte("Hello, World!") var dataLen = uint64(len(data)) func TestReaderCounter(t *testing.T) { buf := bytes.Buffer{} buf.Write(data) counter := NewReaderCounter(&buf) io.Copy(ioutil.Discard, counter) if counter.Count() != dataLen { t.Fatalf("count mismatch len of test data: %d != %d", counter.Count(), len(data)) } } golang-github-miolini-datacounter-1.0.2/response_writer.go000066400000000000000000000033331364600743300237770ustar00rootroot00000000000000package datacounter import ( "bufio" "fmt" "net" "net/http" "sync/atomic" "time" ) // ResponseWriterCounter is counter for http.ResponseWriter type ResponseWriterCounter struct { http.ResponseWriter count uint64 started time.Time statusCode int } // NewResponseWriterCounter function create new ResponseWriterCounter func NewResponseWriterCounter(rw http.ResponseWriter) *ResponseWriterCounter { return &ResponseWriterCounter{ ResponseWriter: rw, started: time.Now(), } } // Write returns underlying Write result, while counting data size func (counter *ResponseWriterCounter) Write(buf []byte) (int, error) { n, err := counter.ResponseWriter.Write(buf) atomic.AddUint64(&counter.count, uint64(n)) return n, err } // Header returns underlying Header result func (counter *ResponseWriterCounter) Header() http.Header { return counter.ResponseWriter.Header() } // WriteHeader returns underlying WriteHeader, while setting Runtime header func (counter *ResponseWriterCounter) WriteHeader(statusCode int) { counter.Header().Set("X-Runtime", fmt.Sprintf("%.6f", time.Since(counter.started).Seconds())) counter.ResponseWriter.WriteHeader(statusCode) } // Hijack returns underlying Hijack func (counter *ResponseWriterCounter) Hijack() (net.Conn, *bufio.ReadWriter, error) { return counter.ResponseWriter.(http.Hijacker).Hijack() } // Count function return counted bytes func (counter *ResponseWriterCounter) Count() uint64 { return atomic.LoadUint64(&counter.count) } // Started returns started value func (counter *ResponseWriterCounter) Started() time.Time { return counter.started } // StatusCode returns sent status code func (counter *ResponseWriterCounter) StatusCode() int { return counter.statusCode } golang-github-miolini-datacounter-1.0.2/response_writer_test.go000066400000000000000000000010061364600743300250310ustar00rootroot00000000000000package datacounter import ( "net/http" "net/http/httptest" "testing" ) func TestResponseWriterCounter(t *testing.T) { handler := func(w http.ResponseWriter, r *http.Request) { w.Write(data) } req, err := http.NewRequest("GET", "http://example.com/foo", nil) if err != nil { t.Fatal(err) } w := httptest.NewRecorder() counter := NewResponseWriterCounter(w) handler(counter, req) if counter.Count() != dataLen { t.Fatalf("count mismatch len of test data: %d != %d", counter.Count(), len(data)) } } golang-github-miolini-datacounter-1.0.2/writer.go000066400000000000000000000011221364600743300220530ustar00rootroot00000000000000package datacounter import ( "io" "sync/atomic" ) // WriterCounter is counter for io.Writer type WriterCounter struct { io.Writer count uint64 } // NewWriterCounter function create new WriterCounter func NewWriterCounter(w io.Writer) *WriterCounter { return &WriterCounter{ Writer: w, } } func (counter *WriterCounter) Write(buf []byte) (int, error) { n, err := counter.Writer.Write(buf) atomic.AddUint64(&counter.count, uint64(n)) return n, err } // Count function return counted bytes func (counter *WriterCounter) Count() uint64 { return atomic.LoadUint64(&counter.count) } golang-github-miolini-datacounter-1.0.2/writer_test.go000066400000000000000000000004451364600743300231210ustar00rootroot00000000000000package datacounter import ( "bytes" "testing" ) func TestWriterCounter(t *testing.T) { buf := bytes.Buffer{} counter := NewWriterCounter(&buf) counter.Write(data) if counter.Count() != dataLen { t.Fatalf("count mismatch len of test data: %d != %d", counter.Count(), len(data)) } }