Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,35 @@ jobs:
- name: set up go
uses: actions/setup-go@v5
with:
go-version: "1.21"
go-version: "1.23"
id: go

- name: build and test
run: |
go get -v
go test -timeout=60s -covermode=count -coverprofile=$GITHUB_WORKSPACE/profile.cov_tmp ./...
cat $GITHUB_WORKSPACE/profile.cov_tmp | grep -v "_mock.go" | grep -v "mocks" > $GITHUB_WORKSPACE/profile.cov
go build -race
env:
GO111MODULE: "on"
TZ: "America/Chicago"

- name: build example
working-directory: _example
run: |
go build -race
env:
TZ: "America/Chicago"

- name: golangci-lint
uses: golangci/golangci-lint-action@v7
with:
version: v2.6

- name: golangci-lint example
uses: golangci/golangci-lint-action@v7
with:
version: v2.6
working-directory: _example

- name: install goveralls
run: go install github.com/mattn/goveralls@latest

Expand Down
19 changes: 11 additions & 8 deletions _example/go.mod
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
module github.com/go-pkgz/requester/_example

go 1.19
go 1.23

require (
github.com/go-pkgz/lcw v0.8.1
github.com/go-pkgz/repeater v1.1.3
github.com/go-pkgz/lcw v1.1.0
github.com/go-pkgz/repeater/v2 v2.2.0
github.com/go-pkgz/requester v1.0.0
)

require (
github.com/go-redis/redis/v7 v7.4.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-redis/redis/v7 v7.4.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/redis/go-redis/v9 v9.17.2 // indirect
)

replace github.com/go-pkgz/requester => ../
28 changes: 24 additions & 4 deletions _example/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,46 @@ github.com/alicebob/gopher-json v0.0.0-20180125190556-5a6b3ba71ee6 h1:45bxf7AZMw
github.com/alicebob/gopher-json v0.0.0-20180125190556-5a6b3ba71ee6/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
github.com/alicebob/miniredis/v2 v2.11.4 h1:GsuyeunTx7EllZBU3/6Ji3dhMQZDpC9rLf1luJ+6M5M=
github.com/alicebob/miniredis/v2 v2.11.4/go.mod h1:VL3UDEfAH59bSa7MuHMuFToxkqyHh69s/WUbYlOAuyg=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-pkgz/lcw v0.8.1 h1:Bpt2yYTE1J8hIhz8tjdm1WPOgH13eo5iTNsXyop7cMQ=
github.com/go-pkgz/lcw v0.8.1/go.mod h1:Xw0/ZfApATgbjVPYRZO4XHdWyxAjErDWDWJ7TLlw1Vc=
github.com/go-pkgz/repeater v1.1.3 h1:q6+JQF14ESSy28Dd7F+wRelY4F+41HJ0LEy/szNnMiE=
github.com/go-pkgz/repeater v1.1.3/go.mod h1:hVTavuO5x3Gxnu8zW7d6sQBfAneKV8X2FjU48kGfpKw=
github.com/go-pkgz/lcw v1.1.0 h1:hDJdQJZf4iw19a7cTgQvF6Poz/L7mL4E7FgG5jGs4lA=
github.com/go-pkgz/lcw v1.1.0/go.mod h1:zwT7RSxFskQsHWHJezYq6n0iTn0n4yIYRDijXq3awM0=
github.com/go-pkgz/repeater/v2 v2.2.0 h1:8nZR/NaknmLfx2YMHbr78u9OL4Xj+8+romm9dz4FpMg=
github.com/go-pkgz/repeater/v2 v2.2.0/go.mod h1:RgX5vUbLKq7PV82QUDP5pFbQS1os4Z+U9XzKymK23A8=
github.com/go-redis/redis/v7 v7.4.0 h1:7obg6wUoj05T0EpY0o8B59S9w5yeMWql7sw2kwNW1x4=
github.com/go-redis/redis/v7 v7.4.0/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg=
github.com/go-redis/redis/v7 v7.4.1 h1:PASvf36gyUpr2zdOUS/9Zqc80GbM+9BDyiJSJDDOrTI=
github.com/go-redis/redis/v7 v7.4.1/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/gomodule/redigo v1.7.1-0.20190322064113-39e2c31b7ca3/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI=
github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/golang-lru v1.0.2 h1:dV3g9Z/unq5DpblPpw+Oqcv4dU/1omnb4Ok8iPY6p1c=
github.com/hashicorp/golang-lru v1.0.2/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand All @@ -39,10 +56,12 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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/redis/go-redis/v9 v9.17.2 h1:P2EGsA4qVIM3Pp+aPocCJ7DguDHhqrXNhVcEp4ViluI=
github.com/redis/go-redis/v9 v9.17.2/go.mod h1:u410H11HMLoB+TP67dz8rL9s6QW2j76l0//kSOd3370=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/yuin/gopher-lua v0.0.0-20191220021717-ab39c6098bdb h1:ZkM6LRnq40pR1Ox0hTHlnpkcOTuFIDQpZ1IN8rKKhX0=
github.com/yuin/gopher-lua v0.0.0-20191220021717-ab39c6098bdb/go.mod h1:gqRgreBUhTSL0GeU64rtZ3Uq3wtjOa/TB2YfrtkCbVQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down Expand Up @@ -70,3 +89,4 @@ gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
30 changes: 15 additions & 15 deletions _example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"log"
"math/rand"
"math/rand/v2"
"net/http"
"net/http/httptest"
"strconv"
Expand All @@ -11,7 +11,7 @@ import (
"time"

"github.com/go-pkgz/lcw"
"github.com/go-pkgz/repeater"
"github.com/go-pkgz/repeater/v2"

"github.com/go-pkgz/requester"
"github.com/go-pkgz/requester/middleware"
Expand Down Expand Up @@ -50,7 +50,7 @@ func requestWithHeaders(ts *httptest.Server) {
)

// create http.Request
req, err := http.NewRequest("GET", ts.URL+"/blah", nil)
req, err := http.NewRequest("GET", ts.URL+"/blah", http.NoBody)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -85,7 +85,7 @@ func requestWithLogging(ts *httptest.Server) {
)

// create http.Request
req, err := http.NewRequest("GET", ts.URL+"/blah", nil)
req, err := http.NewRequest("GET", ts.URL+"/blah", http.NoBody)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func requestWithCache(ts *httptest.Server) {
)

// create http.Request
req, err := http.NewRequest("GET", ts.URL+"/blah", nil)
req, err := http.NewRequest("GET", ts.URL+"/blah", http.NoBody)
if err != nil {
panic(err)
}
Expand All @@ -129,7 +129,7 @@ func requestWithCache(ts *httptest.Server) {
log.Printf("status1: %s", resp.Status)

// make another call for cached resource, will be fast as result cached
req2, err := http.NewRequest("GET", ts.URL+"/blah", nil)
req2, err := http.NewRequest("GET", ts.URL+"/blah", http.NoBody)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -160,7 +160,7 @@ func requestWithCustom(ts *httptest.Server) {
)

// create http.Request
req, err := http.NewRequest("GET", ts.URL+"/blah", nil)
req, err := http.NewRequest("GET", ts.URL+"/blah", http.NoBody)
if err != nil {
panic(err)
}
Expand All @@ -173,7 +173,7 @@ func requestWithCustom(ts *httptest.Server) {
log.Printf("status: %s", resp.Status)
}

var inFly int32
var inFly int32 //nolint:gochecknoglobals // used for demo purposes

// requestWithLimitConcurrency example of concurrency limiter
func requestWithLimitConcurrency(ts *httptest.Server) {
Expand All @@ -193,7 +193,7 @@ func requestWithLimitConcurrency(ts *httptest.Server) {
for i := 0; i < 32; i++ {
go func(i int) {
defer wg.Done()
client.Get(ts.URL + "/blah" + strconv.Itoa(i))
_, _ = client.Get(ts.URL + "/blah" + strconv.Itoa(i))
log.Printf("completed: %d, in fly:%d", i, atomic.LoadInt32(&inFly))
}(i)
}
Expand All @@ -204,15 +204,15 @@ func requestWithLimitConcurrency(ts *httptest.Server) {
func requestWithRepeater(ts *httptest.Server) {
log.Printf("requestWithRepeater --------------")

rpt := repeater.NewDefault(10, 500*time.Millisecond) // make a repeater with up to 10 calls, 500ms between calls
rpt := repeater.NewFixed(10, 500*time.Millisecond) // make a repeater with up to 10 calls, 500ms between calls
rq := requester.New(http.Client{},
// repeat failed call up to 10 times with 500ms delay on networking error or given status codes
middleware.Repeater(rpt, http.StatusInternalServerError, http.StatusBadGateway),
logger.New(logger.Std, logger.Prefix("REST REPT"), logger.WithHeaders).Middleware,
)

// create http.Request
req, err := http.NewRequest("GET", ts.URL+"/blah", nil)
req, err := http.NewRequest("GET", ts.URL+"/blah", http.NoBody)
if err != nil {
panic(err)
}
Expand All @@ -228,9 +228,9 @@ func startTestServer() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c := atomic.AddInt32(&inFly, 1)
log.Printf("request: %+v (%d)", r, c)
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) // simulate random network latency
time.Sleep(time.Duration(rand.IntN(100)) * time.Millisecond) //nolint:gosec // simulate random network latency
w.Header().Set("k1", "v1")
w.Write([]byte("something"))
_, _ = w.Write([]byte("something"))
atomic.AddInt32(&inFly, -1)
}))
}
Expand All @@ -240,15 +240,15 @@ func startTestServerFailedFirst() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c := atomic.AddInt32(&inFly, 1)
log.Printf("request: %+v (%d)", r, c)
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) // simulate random network latency
time.Sleep(time.Duration(rand.IntN(100)) * time.Millisecond) //nolint:gosec // simulate random network latency

if atomic.AddInt32(&n, 1) < 5 { // fail 5 first requests
w.WriteHeader(http.StatusInternalServerError)
return
}

w.Header().Set("k1", "v1")
w.Write([]byte("something"))
_, _ = w.Write([]byte("something"))
atomic.AddInt32(&inFly, -1)
}))
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-pkgz/requester

go 1.21
go 1.23

require github.com/stretchr/testify v1.11.1

Expand Down