Skip to content

Commit

Permalink
Remove git-lfs/tools/humanize
Browse files Browse the repository at this point in the history
  • Loading branch information
saracen committed Jul 11, 2020
1 parent 848d795 commit 4b9112b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
26 changes: 23 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"hash"
"io"
"io/ioutil"
"math"
"net"
"net/http"
"net/http/httputil"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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])
}

0 comments on commit 4b9112b

Please sign in to comment.