Skip to content

Commit

Permalink
Merge pull request #38 from superfly/cleanhttp
Browse files Browse the repository at this point in the history
cleanhttp; fix race
  • Loading branch information
btoews authored Sep 13, 2024
2 parents f04e4cb + b805b3f commit 07f7fc8
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 5 deletions.
3 changes: 2 additions & 1 deletion flyio/machinesapi/machinesapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"sync"

"github.com/hashicorp/go-cleanhttp"
"github.com/superfly/macaroon"
"github.com/superfly/macaroon/bundle"
"github.com/superfly/macaroon/flyio"
Expand Down Expand Up @@ -224,7 +225,7 @@ type verifyResult struct {
func (c *Client) post(ctx context.Context, path string, req any, resp any) error {
c.setDefaultsOnce.Do(func() {
if c.HTTP == nil {
c.HTTP = http.DefaultTransport
c.HTTP = cleanhttp.DefaultTransport()
}

if c.BaseURL == nil {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (

require (
github.com/alecthomas/repr v0.2.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
Expand Down
9 changes: 6 additions & 3 deletions tp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sync"
"time"

"github.com/hashicorp/go-cleanhttp"
"github.com/superfly/macaroon"
"github.com/superfly/macaroon/bundle"
)
Expand Down Expand Up @@ -57,14 +58,16 @@ func WithAuthentication(tpLocation, token string) ClientOption {

return func(c *Client) {
if c.http == nil {
cpy := *http.DefaultClient
c.http = &cpy
c.http = cleanhttp.DefaultClient()
}

switch t := c.http.Transport.(type) {
case *authenticatedHTTP:
t.auth[tpLocation] = token
default:
cpy := *c.http
c.http = &cpy

c.http.Transport = &authenticatedHTTP{
t: t,
auth: map[string]string{tpLocation: token},
Expand Down Expand Up @@ -123,7 +126,7 @@ func NewClient(firstPartyLocation string, opts ...ClientOption) *Client {
}

if client.http == nil {
client.http = http.DefaultClient
client.http = cleanhttp.DefaultClient()
}

if client.pollBackoffNext == nil {
Expand Down
18 changes: 18 additions & 0 deletions tp/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tp

import (
"testing"

"github.com/alecthomas/assert/v2"
"github.com/hashicorp/go-cleanhttp"
)

func TestClient(t *testing.T) {
h := cleanhttp.DefaultClient()

c1 := NewClient("http://foo", WithHTTP(h), WithAuthentication("foo", "bar"))
c2 := NewClient("http://foo", WithHTTP(h), WithAuthentication("foo", "baz"))

assert.Equal(t, "bar", c1.http.Transport.(*authenticatedHTTP).auth["foo"])
assert.Equal(t, "baz", c2.http.Transport.(*authenticatedHTTP).auth["foo"])
}
3 changes: 2 additions & 1 deletion tp/tp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/alecthomas/assert/v2"
"github.com/hashicorp/go-cleanhttp"
"github.com/sirupsen/logrus"
"github.com/superfly/macaroon"
)
Expand Down Expand Up @@ -261,7 +262,7 @@ func checkFP(tb testing.TB, hdr string) []string {
func basicAuthClient(username, password string) *http.Client {
return &http.Client{
Transport: &basicAuthTransport{
t: http.DefaultTransport.(*http.Transport).Clone(),
t: cleanhttp.DefaultTransport(),
username: username,
password: password,
},
Expand Down

0 comments on commit 07f7fc8

Please sign in to comment.