Skip to content

Commit a0adb65

Browse files
authored
feat: modernize codebase (#147)
* set the golang version to 1.24 * replace go vet & golint with golangci-lint * fixed data race issue. * ci: replace travis-ci with github actions * feat: drain response body
1 parent 4651232 commit a0adb65

File tree

17 files changed

+178
-107
lines changed

17 files changed

+178
-107
lines changed

.github/workflows/ci.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
test:
11+
name: Test
12+
runs-on: ubuntu-latest
13+
strategy:
14+
matrix:
15+
go-version: ['1.24.x', '1.25.x', 'stable']
16+
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v4
20+
21+
- name: Set up Go
22+
uses: actions/setup-go@v5
23+
with:
24+
go-version: ${{ matrix.go-version }}
25+
26+
- name: Cache Go modules
27+
uses: actions/cache@v4
28+
with:
29+
path: |
30+
~/.cache/go-build
31+
~/go/pkg/mod
32+
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
33+
restore-keys: |
34+
${{ runner.os }}-go-${{ matrix.go-version }}-
35+
36+
- name: Install dependencies
37+
run: make setup
38+
39+
- name: Build and test
40+
run: make test
41+
42+
- name: Upload coverage to Coveralls
43+
uses: coverallsapp/github-action@v2
44+
with:
45+
github-token: ${{ secrets.GITHUB_TOKEN }}
46+
file: coverage.out
47+
format: golang

.golangci.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
version: "2"
2+
run:
3+
tests: true
4+
linters:
5+
default: none
6+
enable:
7+
- govet
8+
- ineffassign
9+
- staticcheck
10+
- unused
11+
- modernize
12+
exclusions:
13+
generated: lax
14+
paths:
15+
- vendor
16+
- third_party$
17+
- builtin$
18+
- examples/
19+
issues:
20+
max-issues-per-linter: 0
21+
max-same-issues: 0
22+
formatters:
23+
enable:
24+
- gofmt
25+
exclusions:
26+
generated: lax
27+
paths:
28+
- vendor
29+
- third_party$
30+
- builtin$
31+
- examples/

.travis.yml

Lines changed: 0 additions & 10 deletions
This file was deleted.

Makefile

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
1-
.PHONYthub.com/mattn/goveralls: all
2-
all: build test coverage
1+
.PHONY: all
2+
all: build test
33

44
ALL_PACKAGES=$(shell go list ./... | grep -v "vendor")
55

66
setup:
77
mkdir -p $(GOPATH)/bin
8-
go get -u golang.org/x/lint/golint
9-
go get github.com/mattn/goveralls
8+
go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.8.0
9+
go install github.com/mattn/goveralls@v0.0.12
1010

1111
compile:
1212
mkdir -p out/
1313
go build -race ./...
1414

15-
build: compile fmt vet lint
15+
build: compile fmt lint
1616

1717
fmt:
1818
go fmt ./...
1919

20-
vet:
21-
go vet ./...
22-
2320
lint:
24-
golint -set_exit_status $(ALL_PACKAGES)
25-
26-
test: fmt vet build
27-
ENVIRONMENT=test go test -race ./...
21+
golangci-lint run ./...
2822

29-
coverage:
30-
ENVIRONMENT=test goveralls -service=travis-ci
23+
test: fmt build
24+
ENVIRONMENT=test go test -race -covermode=atomic -coverprofile=coverage.out ./...
3125

3226
test-cover-html:
3327
@echo "mode: count" > coverage-all.out

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<p align="center"><img src="doc/heimdall-logo.png" width="360"></p>
44
<p align="center">
5-
<a href="https://travis-ci.com/gojek/heimdall"><img src="https://travis-ci.com/gojek/heimdall.svg?branch=master" alt="Build Status"></img></a>
5+
<a href="https://github.com/gojek/heimdall/actions"><img src="https://github.com/gojek/heimdall/actions/workflows/ci.yml/badge.svg" alt="Build Status"></img></a>
66
<a href="https://goreportcard.com/report/github.com/gojek/heimdall"><img src="https://goreportcard.com/badge/github.com/gojek/heimdall"></img></a>
77
<a href="https://golangci.com"><img src="https://golangci.com/badges/github.com/gojek/heimdall.svg"></img></a>
88
<a href="https://coveralls.io/github/gojek/heimdall?branch=master"><img src="https://coveralls.io/repos/github/gojek/heimdall/badge.svg?branch=master"></img></a>

backoff.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package heimdall
22

33
import (
44
"math"
5-
"math/rand"
5+
"math/rand/v2"
66
"time"
77
)
88

@@ -16,10 +16,6 @@ type constantBackoff struct {
1616
maximumJitterInterval int64
1717
}
1818

19-
func init() {
20-
rand.Seed(time.Now().UnixNano())
21-
}
22-
2319
// NewConstantBackoff returns an instance of ConstantBackoff
2420
func NewConstantBackoff(backoffInterval, maximumJitterInterval time.Duration) Backoff {
2521
// protect against panic when generating random jitter
@@ -35,7 +31,7 @@ func NewConstantBackoff(backoffInterval, maximumJitterInterval time.Duration) Ba
3531

3632
// Next returns next time for retrying operation with constant strategy
3733
func (cb *constantBackoff) Next(retry int) time.Duration {
38-
return (time.Duration(cb.backoffInterval) * time.Millisecond) + (time.Duration(rand.Int63n(cb.maximumJitterInterval+1)) * time.Millisecond)
34+
return (time.Duration(cb.backoffInterval) * time.Millisecond) + (time.Duration(rand.Int64N(cb.maximumJitterInterval+1)) * time.Millisecond)
3935
}
4036

4137
type exponentialBackoff struct {
@@ -65,5 +61,5 @@ func (eb *exponentialBackoff) Next(retry int) time.Duration {
6561
if retry < 0 {
6662
retry = 0
6763
}
68-
return time.Duration(math.Min(eb.initialTimeout*math.Pow(eb.exponentFactor, float64(retry)), eb.maxTimeout)+float64(rand.Int63n(eb.maximumJitterInterval+1))) * time.Millisecond
64+
return time.Duration(math.Min(eb.initialTimeout*math.Pow(eb.exponentFactor, float64(retry)), eb.maxTimeout)+float64(rand.Int64N(eb.maximumJitterInterval+1))) * time.Millisecond
6965
}

backoff_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,21 @@ func TestExponentialBackoffWhenRetryIsLessThanZero(t *testing.T) {
4545

4646
func TestExponentialBackoffJitter0(t *testing.T) {
4747
exponentialBackoff := NewExponentialBackoff(100*time.Millisecond, 1000*time.Millisecond, 2.0, 0*time.Millisecond)
48-
for i := 0; i < 10000; i++ {
48+
for range 10000 {
4949
assert.Equal(t, 200*time.Millisecond, exponentialBackoff.Next(1))
5050
}
5151
}
5252

5353
func TestExponentialBackoffJitter1(t *testing.T) {
5454
exponentialBackoff := NewExponentialBackoff(100*time.Millisecond, 1000*time.Millisecond, 2.0, 1*time.Millisecond)
55-
for i := 0; i < 10000; i++ {
55+
for range 10000 {
5656
assert.True(t, 200*time.Millisecond <= exponentialBackoff.Next(1) && exponentialBackoff.Next(1) <= 201*time.Millisecond)
5757
}
5858
}
5959

6060
func TestExponentialBackoffJitter50(t *testing.T) {
6161
exponentialBackoff := NewExponentialBackoff(100*time.Millisecond, 1000*time.Millisecond, 2.0, 50*time.Millisecond)
62-
for i := 0; i < 10000; i++ {
62+
for range 10000 {
6363
assert.True(t, 200*time.Millisecond <= exponentialBackoff.Next(1) && exponentialBackoff.Next(1) <= 250*time.Millisecond)
6464
}
6565
}
@@ -90,21 +90,21 @@ func TestConstantBackoffWhenRetryIsLessThanZero(t *testing.T) {
9090

9191
func TestConstantBackoffJitter0(t *testing.T) {
9292
constantBackoff := NewConstantBackoff(100*time.Millisecond, 0*time.Millisecond)
93-
for i := 0; i < 10000; i++ {
93+
for i := range 10000 {
9494
assert.Equal(t, 100*time.Millisecond, constantBackoff.Next(i))
9595
}
9696
}
9797

9898
func TestConstantBackoffJitter1(t *testing.T) {
9999
constantBackoff := NewConstantBackoff(100*time.Millisecond, 1*time.Millisecond)
100-
for i := 0; i < 10000; i++ {
100+
for i := range 10000 {
101101
assert.True(t, 100*time.Millisecond <= constantBackoff.Next(i) && constantBackoff.Next(1) <= 101*time.Millisecond)
102102
}
103103
}
104104

105105
func TestConstantBackoffJitter50(t *testing.T) {
106106
constantBackoff := NewConstantBackoff(100*time.Millisecond, 50*time.Millisecond)
107-
for i := 0; i < 10000; i++ {
107+
for i := range 10000 {
108108
assert.True(t, 100*time.Millisecond <= constantBackoff.Next(i) && constantBackoff.Next(1) <= 150*time.Millisecond)
109109
}
110110
}

examples/client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package examples
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"io"
66
"net/http"
77
"time"
88

@@ -34,7 +34,7 @@ func httpClientUsage() error {
3434

3535
defer response.Body.Close()
3636

37-
respBody, err := ioutil.ReadAll(response.Body)
37+
respBody, err := io.ReadAll(response.Body)
3838
if err != nil {
3939
return errors.Wrap(err, "failed to read response body")
4040
}
@@ -63,7 +63,7 @@ func hystrixClientUsage() error {
6363

6464
defer response.Body.Close()
6565

66-
respBody, err := ioutil.ReadAll(response.Body)
66+
respBody, err := io.ReadAll(response.Body)
6767
if err != nil {
6868
return errors.Wrap(err, "failed to read response body")
6969
}
@@ -102,7 +102,7 @@ func customHTTPClientUsage() error {
102102

103103
defer response.Body.Close()
104104

105-
respBody, err := ioutil.ReadAll(response.Body)
105+
respBody, err := io.ReadAll(response.Body)
106106
if err != nil {
107107
return errors.Wrap(err, "failed to read response body")
108108
}
@@ -136,7 +136,7 @@ func customHystrixClientUsage() error {
136136

137137
defer response.Body.Close()
138138

139-
respBody, err := ioutil.ReadAll(response.Body)
139+
respBody, err := io.ReadAll(response.Body)
140140
if err != nil {
141141
return errors.Wrap(err, "failed to read response body")
142142
}

go.mod

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
module github.com/gojek/heimdall/v7
22

3-
go 1.14
3+
go 1.24
44

55
require (
6-
github.com/DataDog/datadog-go v3.7.1+incompatible // indirect
76
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5
8-
github.com/cactus/go-statsd-client/statsd v0.0.0-20200423205355-cb0885a1018c // indirect
97
github.com/gojek/valkyrie v0.0.0-20180215180059-6aee720afcdf
108
github.com/pkg/errors v0.9.1
9+
github.com/stretchr/testify v1.11.1
10+
)
11+
12+
require (
13+
github.com/DataDog/datadog-go v3.7.1+incompatible // indirect
14+
github.com/cactus/go-statsd-client/statsd v0.0.0-20200423205355-cb0885a1018c // indirect
15+
github.com/davecgh/go-spew v1.1.1 // indirect
16+
github.com/pmezard/go-difflib v1.0.0 // indirect
1117
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
1218
github.com/smartystreets/goconvey v1.6.4 // indirect
13-
github.com/stretchr/objx v0.3.0 // indirect
14-
github.com/stretchr/testify v1.3.0
19+
github.com/stretchr/objx v0.5.2 // indirect
20+
gopkg.in/yaml.v3 v3.0.1 // indirect
1521
)

go.sum

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5 h1:rFw4nCn9iMW+Vaj
44
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
55
github.com/cactus/go-statsd-client/statsd v0.0.0-20200423205355-cb0885a1018c h1:HIGF0r/56+7fuIZw2V4isE22MK6xpxWx7BbV8dJ290w=
66
github.com/cactus/go-statsd-client/statsd v0.0.0-20200423205355-cb0885a1018c/go.mod h1:l/bIBLeOl9eX+wxJAzxS4TveKRtAqlyDpHjhkfO0MEI=
7-
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
8-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
97
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
108
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
119
github.com/gojek/valkyrie v0.0.0-20180215180059-6aee720afcdf h1:5xRGbUdOmZKoDXkGx5evVLehuCMpuO1hl701bEQqXOM=
@@ -24,13 +22,16 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykE
2422
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
2523
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
2624
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
27-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
28-
github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As=
29-
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
30-
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
31-
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
25+
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
26+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
27+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
28+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
3229
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
3330
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
3431
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
3532
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
3633
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
34+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
35+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
36+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
37+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)