Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hooks: Apply hooks suggestions #146

Merged
merged 12 commits into from
Nov 6, 2024
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.idea/
.idea/
29 changes: 29 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-yaml

- repo: https://github.com/tekwizely/pre-commit-golang
rev: v1.0.0-rc.1
hooks:
# Go Test
- id: go-test-repo-mod # targets all modules in repo

# Go Vet
- id: go-vet-repo-mod

# GoSec
- id: go-sec-repo-mod # go install github.com/securego/gosec/v2/cmd/gosec@latest

# Formatters
- id: go-imports-repo
- id: go-fumpt-repo # go install mvdan.cc/gofumpt@latest

# Style Checkers
- id: go-critic # go install github.com/go-critic/go-critic/cmd/gocritic@latest

# Go Mod Tidy
- id: go-mod-tidy-repo
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.PHONY: devenv-init

devenv-init:
go install github.com/securego/gosec/v2/cmd/gosec@latest
go install mvdan.cc/gofumpt@latest
go install github.com/go-critic/go-critic/cmd/gocritic@latest
pip install pre-commit
pre-commit install
7 changes: 5 additions & 2 deletions _examples/readonly/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strconv"
"time"

luno "github.com/luno/luno-go"
"github.com/luno/luno-go"
)

var (
Expand All @@ -21,7 +21,10 @@ func main() {

cl := luno.NewClient()
cl.SetDebug(*debug)
cl.SetAuth(*apiKeyID, *apiKeySecret)
err := cl.SetAuth(*apiKeyID, *apiKeySecret)
if err != nil {
log.Println(err)
}

ctx := context.Background()

Expand Down
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,12 @@ go 1.21

require (
github.com/gorilla/websocket v1.5.3
github.com/stretchr/testify v1.9.0
golang.org/x/time v0.7.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/time v0.7.0 h1:ntUhktv3OPE6TgYxXWv9vKvUSJyIFJlyohwbkEwPrKQ=
golang.org/x/time v0.7.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
5 changes: 2 additions & 3 deletions luno.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"runtime"
Expand Down Expand Up @@ -114,7 +113,7 @@ func (cl *Client) do(ctx context.Context, method, path string,
if req != nil {
values := makeURLValues(req)
if strings.Contains(path, "{id}") {
url = strings.Replace(url, "{id}", values.Get("id"), -1)
url = strings.ReplaceAll(url, "{id}", values.Get("id"))
values.Del("id")
}
if method == http.MethodGet {
Expand Down Expand Up @@ -151,7 +150,7 @@ func (cl *Client) do(ctx context.Context, method, path string,

body = httpRes.Body
if cl.debug {
b, err := ioutil.ReadAll(body)
b, err := io.ReadAll(body)
if err != nil {
log.Printf("luno: Error reading response body: %v", err)
} else {
Expand Down
10 changes: 8 additions & 2 deletions streaming/backoff.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package streaming

import (
"crypto/rand"
"math"
"math/rand"
"math/big"
"time"
)

Expand All @@ -12,7 +13,12 @@ type backoffParams struct {
}

func defaultBackoffHandler(attempts int) time.Duration {
jitter := time.Duration(rand.Intn(200)-100) * time.Millisecond // ±100ms
randInt, err := rand.Int(rand.Reader, big.NewInt(200))
if err != nil {
panic(err)
}
jitter := time.Duration(randInt.Int64()-100) * time.Millisecond // ±100ms
print(jitter)
backoff := time.Duration(math.Min(math.Pow(2, float64(attempts)), 60)) * time.Second // Exponential backoff up to 60s
return backoff + jitter
}
9 changes: 6 additions & 3 deletions streaming/streaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,11 +435,14 @@ func (c *Conn) processCreate(u CreateUpdate) error {
Volume: u.Volume,
}

if u.Type == string(luno.OrderTypeBid) {
switch u.Type {
case string(luno.OrderTypeBid):
c.bids[o.ID] = o
} else if u.Type == string(luno.OrderTypeAsk) {

case string(luno.OrderTypeAsk):
c.asks[o.ID] = o
} else {

default:
return errors.New("streaming: unknown order type")
}

Expand Down
50 changes: 27 additions & 23 deletions streaming/streaming_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package streaming

import (
"math/rand"
"log"
"testing"
"time"

"github.com/luno/luno-go"
"github.com/luno/luno-go/decimal"
"github.com/stretchr/testify/require"
)

func TestFlatten(t *testing.T) {
Expand Down Expand Up @@ -131,25 +132,24 @@ func TestFlatten(t *testing.T) {

func TestBackoff(t *testing.T) {
tcs := []struct {
name string
fn BackoffHandler
attemptReset time.Duration
p *backoffParams
seed func()
reqTS time.Time
expBackoff time.Duration
expParams backoffParams
name string
fn BackoffHandler
attemptReset time.Duration
p *backoffParams
seed func()
reqTS time.Time
expBackoff time.Duration
expBackoffLowerLimit *time.Duration
expBackoffUpperLimit *time.Duration // Used when randomly generating
expParams backoffParams
}{
{
name: "default",
fn: defaultBackoffHandler,
p: &backoffParams{},
seed: func() {
// Seed random generator to test default backoff handler
rand.Seed(123)
},
reqTS: time.Date(2024, 2, 1, 0, 0, 0, 0, time.UTC),
expBackoff: 1935000000,
name: "default",
fn: defaultBackoffHandler,
p: &backoffParams{},
reqTS: time.Date(2024, 2, 1, 0, 0, 0, 0, time.UTC),
expBackoffLowerLimit: ptr[time.Duration](1900000000),
expBackoffUpperLimit: ptr[time.Duration](2100000000),
expParams: backoffParams{
attempts: 1,
lastAttempt: time.Date(2024, 2, 1, 0, 0, 0, 0, time.UTC),
Expand Down Expand Up @@ -206,17 +206,17 @@ func TestBackoff(t *testing.T) {

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
if tc.seed != nil {
tc.seed()
}

c := &Conn{
backoffHandler: tc.fn,
attemptReset: tc.attemptReset,
}

dt := c.calculateBackoff(tc.p, tc.reqTS)
if dt != tc.expBackoff {
if tc.expBackoffUpperLimit != nil {
log.Println("backoff = " + dt.String())
require.LessOrEqual(t, dt, *tc.expBackoffUpperLimit)
require.GreaterOrEqual(t, dt, *tc.expBackoffLowerLimit)
} else if dt != tc.expBackoff {
t.Errorf("backoff %d doesn't match expect backoff %d", dt, tc.expBackoff)
}

Expand All @@ -226,3 +226,7 @@ func TestBackoff(t *testing.T) {
})
}
}

func ptr[T any](t T) *T {
return &t
}