Skip to content

Commit

Permalink
cmd/atlas/cloudapi: use retryhttp client
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Oct 15, 2024
1 parent cfce708 commit af43fb0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
8 changes: 5 additions & 3 deletions cmd/atlas/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ require (
github.com/aws/aws-sdk-go-v2/config v1.26.1
github.com/aws/aws-sdk-go-v2/feature/rds/auth v1.2.16
github.com/chzyer/readline v1.5.1
github.com/fatih/color v1.13.0
github.com/fatih/color v1.16.0
github.com/go-sql-driver/mysql v1.7.1
github.com/google/uuid v1.6.0
github.com/hashicorp/hcl/v2 v2.13.0
github.com/lib/pq v1.10.9
github.com/libsql/libsql-client-go v0.0.0-20230602133133-5905f0c4f8a5
github.com/mattn/go-isatty v0.0.18
github.com/mattn/go-isatty v0.0.20
github.com/mattn/go-sqlite3 v1.14.16
github.com/mitchellh/go-homedir v1.1.0
github.com/pganalyze/pg_query_go/v5 v5.1.0
Expand Down Expand Up @@ -70,12 +70,14 @@ require (
github.com/google/wire v0.5.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/klauspost/compress v1.15.15 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/libsql/sqlite-antlr4-parser v0.0.0-20230512205400-b2348f0d1196 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
9 changes: 9 additions & 0 deletions cmd/atlas/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
Expand Down Expand Up @@ -174,6 +175,10 @@ github.com/googleapis/gax-go/v2 v2.12.0/go.mod h1:y+aIqrI5eb1YGMVJfuV3185Ts/D7qK
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
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/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU=
github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk=
github.com/hashicorp/hcl/v2 v2.13.0 h1:0Apadu1w6M11dyGFxWnmhhcMjkbAiKCv7G1r/2QgCNc=
github.com/hashicorp/hcl/v2 v2.13.0/go.mod h1:e4z5nxYlWNPdDSNYX+ph14EvWYMFm3eP0zIUqPc2jr0=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand Down Expand Up @@ -208,10 +213,13 @@ github.com/libsql/sqlite-antlr4-parser v0.0.0-20230512205400-b2348f0d1196/go.mod
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
Expand Down Expand Up @@ -319,6 +327,7 @@ golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Expand Down
30 changes: 19 additions & 11 deletions cmd/atlas/internal/cloudapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"ariga.io/atlas/sql/migrate"
"ariga.io/atlas/sql/sqlclient"

"github.com/hashicorp/go-retryablehttp"
"github.com/vektah/gqlparser/v2/gqlerror"
)

Expand All @@ -35,7 +36,7 @@ const (

// Client is a client for the Atlas Cloud API.
type Client struct {
client *http.Client
client *retryablehttp.Client
endpoint string
}

Expand All @@ -44,15 +45,21 @@ func New(endpoint, token string) *Client {
if endpoint == "" {
endpoint = defaultURL
}
var (
client = retryablehttp.NewClient()
transport = client.HTTPClient.Transport
)
client.HTTPClient.Timeout = time.Second * 30
client.HTTPClient.Transport = &roundTripper{
token: token,
transport: transport,
extraHeaders: make(map[string]string),
}
// Disable logging until "ATLAS_DEBUG" option will be added.
client.Logger = nil
return &Client{
endpoint: endpoint,
client: &http.Client{
Transport: &roundTripper{
token: token,
extraHeaders: make(map[string]string),
},
Timeout: time.Second * 30,
},
client: client,
}
}

Expand Down Expand Up @@ -262,7 +269,7 @@ func (c *Client) post(ctx context.Context, query string, vars, data any) error {
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.endpoint, bytes.NewReader(body))
req, err := retryablehttp.NewRequestWithContext(ctx, http.MethodPost, c.endpoint, bytes.NewReader(body))
if err != nil {
return err
}
Expand Down Expand Up @@ -301,7 +308,7 @@ func (c *Client) post(ctx context.Context, query string, vars, data any) error {

// AddHeader adds a header to the client requests.
func (c *Client) AddHeader(key, value string) {
rt, ok := c.client.Transport.(*roundTripper)
rt, ok := c.client.HTTPClient.Transport.(*roundTripper)
if !ok {
return
}
Expand All @@ -316,6 +323,7 @@ type (
roundTripper struct {
token string
extraHeaders map[string]string
transport http.RoundTripper
}
)

Expand All @@ -330,7 +338,7 @@ func (r *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
for k, v := range r.extraHeaders {
req.Header.Set(k, v)
}
return http.DefaultTransport.RoundTrip(req)
return r.transport.RoundTrip(req)
}

// RedactedURL returns a URL string with the userinfo redacted.
Expand Down
20 changes: 20 additions & 0 deletions cmd/atlas/internal/cloudapi/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,23 @@ func TestClient_AddHeader(t *testing.T) {
})
require.NoError(t, err)
}

func TestClient_Retry(t *testing.T) {
var (
calls = []int{http.StatusInternalServerError, http.StatusInternalServerError, http.StatusOK}
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "val", r.Header.Get("key"))
require.Equal(t, "Bearer atlas", r.Header.Get("Authorization"))
w.WriteHeader(calls[0])
calls = calls[1:]
}))
client = New(srv.URL, "atlas")
)
defer srv.Close()
client.AddHeader("key", "val")
_, err := client.ReportMigration(context.Background(), ReportMigrationInput{
EnvName: "foo",
})
require.NoError(t, err)
require.Empty(t, calls)
}

0 comments on commit af43fb0

Please sign in to comment.