summaryrefslogtreecommitdiff
path: root/lbc/lbc.go
diff options
context:
space:
mode:
authorWill Hawkins <[email protected]>2021-12-18 00:55:04 -0500
committerWill Hawkins <[email protected]>2021-12-18 00:55:04 -0500
commit1ee83b005be490ff693be99b4e6359efec642fcd (patch)
treeb28e587b8c4879f9eafe85bdc76a16def9bc54f9 /lbc/lbc.go
parent5b482c7d5087deb970e1cd5b49fc35c4510be146 (diff)
Performance (Take 2): A different attempt at improving perf
Try intercepting a reader.
Diffstat (limited to 'lbc/lbc.go')
-rw-r--r--lbc/lbc.go22
1 files changed, 10 insertions, 12 deletions
diff --git a/lbc/lbc.go b/lbc/lbc.go
index 38210a3..d0014dd 100644
--- a/lbc/lbc.go
+++ b/lbc/lbc.go
@@ -37,18 +37,18 @@ func (lbd *LoadBearingConnectionDownload) Client() *http.Client {
return lbd.client
}
-type syntheticCountingWriter struct {
- n *uint64
- ctx context.Context
+type countingReader struct {
+ n *uint64
+ ctx context.Context
+ readable io.Reader
}
-func (s *syntheticCountingWriter) Write(p []byte) (n int, err error) {
- if s.ctx.Err() != nil {
+func (cr *countingReader) Read(p []byte) (n int, err error) {
+ if cr.ctx.Err() != nil {
return 0, io.EOF
}
- err = nil
- n = len(p)
- atomic.AddUint64(s.n, uint64(n))
+ n, err = cr.readable.Read(p)
+ *cr.n += uint64(n)
return
}
@@ -90,10 +90,8 @@ func (lbd *LoadBearingConnectionDownload) doDownload(ctx context.Context) {
lbd.valid = false
return
}
- s := &syntheticCountingWriter{n: &lbd.downloaded, ctx: ctx}
- // Setup a single buffer!
- buffer := make([]byte, chunkSize)
- _, err = io.CopyBuffer(s, get.Body, buffer)
+ cr := &countingReader{n: &lbd.downloaded, ctx: ctx, readable: get.Body}
+ _, _ = io.ReadAll(cr)
lbd.valid = false
get.Body.Close()
if lbd.debug {