From 4b9112b7d95204ca83a2a9abdb5e08cb65d2bef5 Mon Sep 17 00:00:00 2001 From: Arran Walker Date: Sat, 11 Jul 2020 23:21:46 +0100 Subject: [PATCH] Remove git-lfs/tools/humanize --- go.mod | 2 -- go.sum | 4 ---- server/server.go | 26 +++++++++++++++++++++++--- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index cc6710e..f4de51d 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,6 @@ module github.com/saracen/lfscache go 1.14 require ( - github.com/git-lfs/git-lfs v2.5.2+incompatible github.com/go-kit/kit v0.10.0 - github.com/pkg/errors v0.9.1 // indirect github.com/stretchr/testify v1.4.0 ) diff --git a/go.sum b/go.sum index 0e012d7..0cf206e 100644 --- a/go.sum +++ b/go.sum @@ -54,8 +54,6 @@ github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVB github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8/go.mod h1:ZhphrRTfi2rbfLwlschooIH4+wKKDR4Pdxhh+TRoA20= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= -github.com/git-lfs/git-lfs v2.5.2+incompatible h1:HZyuAiTCvBbNQa1tgrpBzmb9iRn9GKD8MGiLgdqRs/8= -github.com/git-lfs/git-lfs v2.5.2+incompatible/go.mod h1:OWQmiHEcQlu45gp/CzUQAR/VYwm1Pcxb3IB/RaB176U= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/kit v0.9.0 h1:wDJmvq38kDhkVxi50ni9ykkdUr1PKgqKOoi01fa0Mdk= github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= @@ -190,8 +188,6 @@ github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/server/server.go b/server/server.go index b514dee..90f15bd 100644 --- a/server/server.go +++ b/server/server.go @@ -14,6 +14,7 @@ import ( "hash" "io" "io/ioutil" + "math" "net" "net/http" "net/http/httputil" @@ -23,7 +24,6 @@ import ( "strings" "time" - "github.com/git-lfs/git-lfs/tools/humanize" "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" "github.com/saracen/lfscache/cache" @@ -344,7 +344,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request) { if err != nil { level.Error(logger).Log("err", err) } else { - rate := humanize.FormatByteRate(uint64(size), time.Since(begin)) + rate := formatByteRate(uint64(size), time.Since(begin)) level.Info(logger).Log("size", size, "rate", rate) } @@ -402,7 +402,7 @@ func (s *Server) fetch(w io.Writer, oid, url string, size int, header http.Heade begin := time.Now() var beginTransfer time.Time defer func() { - rate := humanize.FormatByteRate(uint64(hcw.n), time.Since(beginTransfer)) + rate := formatByteRate(uint64(hcw.n), time.Since(beginTransfer)) logger := log.With(s.logger, "event", "fetched", "oid", oid, "took", time.Since(begin), "downloaded", fmt.Sprintf("%d/%d", hcw.n, size), "rate", rate) if err != nil { @@ -469,3 +469,23 @@ func (hcw *hashCountWriter) Write(p []byte) (n int, err error) { hcw.h.Write(p[:n]) return } + +func formatByteRate(s uint64, d time.Duration) string { + const ( + unit = 1000 + prefixes = "KMGTPE" + ) + + b := uint64(float64(s) / math.Max(time.Nanosecond.Seconds(), d.Seconds())) + if b < unit { + return fmt.Sprintf("%d B/s", b) + } + + div, exp := int64(unit), 0 + for n := b / unit; n >= unit; n /= unit { + div *= unit + exp++ + } + + return fmt.Sprintf("%.1f %cB/s", float64(b)/float64(div), prefixes[exp]) +}