From bbc387a12da7a79a8078c428a2bb49c4d88358ae Mon Sep 17 00:00:00 2001 From: Viacheslav Poturaev Date: Sun, 28 Mar 2021 16:45:15 +0200 Subject: [PATCH] Avoid math.MaxInt64 to support 32 bit platforms (#6) --- .github/workflows/bench.yml | 52 +++++++++++++++++++++-------- .github/workflows/golangci-lint.yml | 2 +- .github/workflows/test-unit.yml | 31 +++++++++++++---- .golangci.yml | 13 ++++++-- Makefile | 9 ++--- dev_test.go | 2 +- go.mod | 2 +- go.sum | 4 +-- server.go | 5 ++- 9 files changed, 85 insertions(+), 35 deletions(-) diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml index 88cef99..46100ea 100644 --- a/.github/workflows/bench.yml +++ b/.github/workflows/bench.yml @@ -1,14 +1,20 @@ name: bench on: - push: - tags: - - v* - branches: - - master - - main pull_request: + workflow_dispatch: + inputs: + old: + description: 'Old Ref' + required: false + default: 'master' + new: + description: 'New Ref' + required: true + env: GO111MODULE: "on" + CACHE_BENCHMARK: "off" # Enables benchmark result reuse between runs, may skew latency results. + RUN_BASE_BENCHMARK: "on" # Runs benchmark for PR base in case benchmark result is missing. jobs: bench: strategy: @@ -22,21 +28,28 @@ jobs: go-version: ${{ matrix.go-version }} - name: Checkout code uses: actions/checkout@v2 - - name: Restore vendor + with: + ref: ${{ (github.event.inputs.new != '') && github.event.inputs.new || github.event.ref }} + - name: Go cache uses: actions/cache@v2 with: + # In order: + # * Module download cache + # * Build cache (Linux) path: | - vendor - key: ${{ runner.os }}-go${{ matrix.go-version }}-vendor-${{ hashFiles('**/go.mod') }} - - name: Populate dependencies - run: | - (test -d vendor && echo vendor found) || (go mod vendor && du -sh vendor && du -sh ~/go/pkg/mod) + ~/go/pkg/mod + ~/.cache/go-build + key: ${{ runner.os }}-go-cache-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go-cache - name: Restore benchstat uses: actions/cache@v2 with: path: ~/go/bin/benchstat key: ${{ runner.os }}-benchstat - name: Restore base benchmark result + if: env.CACHE_BENCHMARK == 'on' + id: benchmark-base uses: actions/cache@v2 with: path: | @@ -44,10 +57,23 @@ jobs: bench-main.txt # Use base sha for PR or new commit hash for master/main push in benchmark result key. key: ${{ runner.os }}-bench-${{ (github.event.pull_request.base.sha != github.event.after) && github.event.pull_request.base.sha || github.event.after }} + - name: Checkout base code + if: env.RUN_BASE_BENCHMARK == 'on' && steps.benchmark-base.outputs.cache-hit != 'true' && (github.event.pull_request.base.sha != '' || github.event.inputs.old != '') + uses: actions/checkout@v2 + with: + ref: ${{ (github.event.pull_request.base.sha != '' ) && github.event.pull_request.base.sha || github.event.inputs.old }} + path: __base + - name: Run base benchmark + if: env.RUN_BASE_BENCHMARK == 'on' && steps.benchmark-base.outputs.cache-hit != 'true' && (github.event.pull_request.base.sha != '' || github.event.inputs.old != '') + run: | + export REF_NAME=master + cd __base + BENCH_COUNT=5 make bench-run bench-stat + cp bench-master.txt ../bench-master.txt - name: Benchmark id: bench run: | - export REF_NAME=${GITHUB_REF##*/} + export REF_NAME=new BENCH_COUNT=5 make bench-run bench-stat OUTPUT=$(make bench-stat) OUTPUT="${OUTPUT//'%'/'%25'}" diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index c6fdd3f..2cc5df3 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -17,7 +17,7 @@ jobs: uses: golangci/golangci-lint-action@v2.5.1 with: # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. - version: v1.37.1 + version: v1.38.0 # Optional: working directory, useful for monorepos # working-directory: somedir diff --git a/.github/workflows/test-unit.yml b/.github/workflows/test-unit.yml index c857562..75c2307 100644 --- a/.github/workflows/test-unit.yml +++ b/.github/workflows/test-unit.yml @@ -7,6 +7,7 @@ on: pull_request: env: GO111MODULE: "on" + RUN_BASE_COVERAGE: "on" # Runs test for PR base in case base test coverage is missing. jobs: test: strategy: @@ -20,22 +21,38 @@ jobs: go-version: ${{ matrix.go-version }} - name: Checkout code uses: actions/checkout@v2 + - name: Go cache + uses: actions/cache@v2 + with: + # In order: + # * Module download cache + # * Build cache (Linux) + path: | + ~/go/pkg/mod + ~/.cache/go-build + key: ${{ runner.os }}-go-cache-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go-cache - name: Restore base test coverage + if: matrix.go-version == '1.16.x' uses: actions/cache@v2 with: path: | unit-base.txt # Use base sha for PR or new commit hash for master/main push in test result key. key: ${{ runner.os }}-unit-test-coverage-${{ (github.event.pull_request.base.sha != github.event.after) && github.event.pull_request.base.sha || github.event.after }} - - name: Restore vendor - uses: actions/cache@v2 + - name: Checkout base code + if: matrix.go-version == '1.16.x' && env.RUN_BASE_COVERAGE == 'on' && steps.benchmark-base.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != '' + uses: actions/checkout@v2 with: - path: | - vendor - key: ${{ runner.os }}-go${{ matrix.go-version }}-vendor-${{ hashFiles('**/go.mod') }} - - name: Populate dependencies + ref: ${{ github.event.pull_request.base.sha }} + path: __base + - name: Run test for base code + if: matrix.go-version == '1.16.x' && env.RUN_BASE_COVERAGE == 'on' && steps.benchmark-base.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != '' run: | - (test -d vendor && echo vendor found) || (go mod vendor && du -sh vendor && du -sh ~/go/pkg/mod) + cd __base + make test-unit + go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > ../unit-base.txt - name: Test id: test run: | diff --git a/.golangci.yml b/.golangci.yml index 4e30af6..c536ee3 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,7 +1,6 @@ # See https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml run: tests: true - deadline: 5m linters-settings: errcheck: @@ -21,12 +20,19 @@ linters-settings: linters: enable-all: true disable: - - gochecknoglobals - testpackage - goerr113 + - lll + - maligned + - gochecknoglobals + - gomnd - wrapcheck + - paralleltest + - forbidigo - exhaustivestruct - cyclop + - interfacer + - forcetypeassert issues: exclude-use-default: false @@ -37,5 +43,6 @@ issues: - goerr113 - noctx - funlen - - paralleltest + - dupl path: "_test.go" + diff --git a/Makefile b/Makefile index 527347c..d019c60 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,4 @@ -BENCH_COUNT ?= 5 -REF_NAME ?= $(shell git symbolic-ref HEAD --short | tr / - 2>/dev/null) +#GOLANGCI_LINT_VERSION := "v1.38.0" # Optional configuration to pinpoint golangci-lint version. # The head of Makefile determines location of dev-go to include standard targets. GO ?= go @@ -29,10 +28,12 @@ ifeq ($(DEVGO_PATH),) endif -include $(DEVGO_PATH)/makefiles/main.mk --include $(DEVGO_PATH)/makefiles/test-unit.mk -include $(DEVGO_PATH)/makefiles/lint.mk +-include $(DEVGO_PATH)/makefiles/test-unit.mk -include $(DEVGO_PATH)/makefiles/bench.mk --include $(DEVGO_PATH)/makefiles/github-actions.mk +-include $(DEVGO_PATH)/makefiles/reset-ci.mk + +# Add your custom targets here. ## Run tests test: test-unit diff --git a/dev_test.go b/dev_test.go index 2ac5345..98534a9 100644 --- a/dev_test.go +++ b/dev_test.go @@ -1,3 +1,3 @@ package statigz_test -import _ "github.com/bool64/dev" +import _ "github.com/bool64/dev" // Include CI/Dev scripts to project. diff --git a/go.mod b/go.mod index 912ce8b..cd9db42 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,6 @@ go 1.16 require ( github.com/andybalholm/brotli v1.0.1 - github.com/bool64/dev v0.1.20 + github.com/bool64/dev v0.1.25 github.com/stretchr/testify v1.4.0 ) diff --git a/go.sum b/go.sum index 1e188bf..4dd2ea6 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ github.com/andybalholm/brotli v1.0.1 h1:KqhlKozYbRtJvsPrrEeXcO+N2l6NYT5A2QAFmSULpEc= github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= -github.com/bool64/dev v0.1.20 h1:PeDScN45cziC5Swn1Xgam4LWDwaa5rsRB28vW9x771U= -github.com/bool64/dev v0.1.20/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU= +github.com/bool64/dev v0.1.25 h1:iSfcaS1FP6F2TlMI1daAvqk6PYl23XDf6oZu7WRXB8Y= +github.com/bool64/dev v0.1.25/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/server.go b/server.go index da17196..5df32d9 100644 --- a/server.go +++ b/server.go @@ -7,7 +7,6 @@ import ( "hash/fnv" "io" "io/fs" - "math" "mime" "net/http" "path" @@ -262,7 +261,7 @@ func (s *Server) serve(rw http.ResponseWriter, req *http.Request, fn, suf, enc s func (s *Server) minEnc(accessEncoding string, fn string) (fileInfo, Encoding) { var ( minEnc Encoding - minInfo = fileInfo{size: math.MaxInt64} + minInfo = fileInfo{size: -1} ) for _, enc := range s.Encodings { @@ -275,7 +274,7 @@ func (s *Server) minEnc(accessEncoding string, fn string) (fileInfo, Encoding) { continue } - if info.size < minInfo.size { + if minInfo.size == -1 || info.size < minInfo.size { minEnc = enc minInfo = info }