Skip to content

Commit 9df3692

Browse files
authored
Consistently use retriable HTTP client (#2703)
Update all locations to use the retriable HTTP client which automatically retries on 429 status codes with support for `Retry-After`. Update the Maven URL to be the same one used in the latest super POM. Fixes #2702.
1 parent f3245a8 commit 9df3692

4 files changed

Lines changed: 16 additions & 19 deletions

File tree

‎go.mod‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ require (
1414
github.com/spf13/pflag v1.0.10
1515
github.com/stretchr/testify v1.12.1
1616
golang.org/x/mod v0.40.0
17-
golang.org/x/oauth2 v0.36.0
1817
golang.org/x/sync v0.22.0
1918
gopkg.in/yaml.v3 v3.0.1
2019
)

‎go.sum‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ golang.org/x/crypto v0.54.0 h1:YLIA59K4fiNzHzjnZt2tUJQjQtUWfWbeHBqKtk3eScw=
6666
golang.org/x/crypto v0.54.0/go.mod h1:KWL8ny2AZdGR2cWmzeHrp2azQPGogOv+HeQaVEXC2dk=
6767
golang.org/x/mod v0.40.0 h1:hUv+3cXcdRHz08UmSiOob7sadHig73uo5bkXxQ/tvUs=
6868
golang.org/x/mod v0.40.0/go.mod h1:0/weTWkPWGBikyTWAX3dkjVztMmBA5hM0DH6BElSupE=
69-
golang.org/x/oauth2 v0.36.0 h1:peZ/1z27fi9hUOFCAZaHyrpWG5lwe0RJEEEeH0ThlIs=
70-
golang.org/x/oauth2 v0.36.0/go.mod h1:YDBUJMTkDnJS+A4BP4eZBjCqtokkg1hODuPjwiGPO7Q=
7169
golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek=
7270
golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
7371
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=

‎internal/cmd/fetcher/main.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ func newRootCommand(name string) *appcmd.Command {
109109
Short: "Fetches latest plugin versions from external sources.",
110110
Args: appcmd.MaximumNArgs(1),
111111
Run: builder.NewRunFunc(func(ctx context.Context, container appext.Container) error {
112-
client := fetchclient.New(ctx)
112+
client := fetchclient.New()
113113
created, err := run(ctx, container, client, f)
114114
if err != nil {
115115
return fmt.Errorf("failed to fetch versions: %w", err)
116116
}
117-
if err := postProcessCreatedPlugins(ctx, container.Logger(), http.DefaultClient, created); err != nil {
117+
if err := postProcessCreatedPlugins(ctx, container.Logger(), fetchclient.NewHTTPClient(), created); err != nil {
118118
return fmt.Errorf("failed to run post-processing on plugins: %w", err)
119119
}
120120
if err := writeGitHubOutput("pr_title", generatePRTitle(created)); err != nil {

‎internal/fetchclient/fetchclient.go‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/google/go-github/v72/github"
1616
"github.com/hashicorp/go-retryablehttp"
1717
"golang.org/x/mod/semver"
18-
"golang.org/x/oauth2"
1918

2019
"github.com/bufbuild/plugins/internal/source"
2120
)
@@ -26,7 +25,7 @@ const (
2625
dartFlutterAPIURL = "https://pub.dev/api/packages"
2726
goProxyURL = "https://proxy.golang.org"
2827
npmRegistryURL = "https://registry.npmjs.org"
29-
mavenURL = "https://repo1.maven.org/maven2"
28+
mavenURL = "https://repo.maven.apache.org/maven2"
3029
// docs: https://packaging.python.org/en/latest/specifications/simple-repository-api/
3130
pypiURL = "https://pypi.org/simple"
3231
)
@@ -44,25 +43,26 @@ type Client struct {
4443
}
4544

4645
// New returns a new client.
47-
func New(ctx context.Context) *Client {
48-
var client *http.Client
46+
func New() *Client {
47+
httpClient := NewHTTPClient()
48+
ghClient := github.NewClient(httpClient)
4949
if ghToken := os.Getenv("GITHUB_TOKEN"); ghToken != "" {
50-
ts := oauth2.StaticTokenSource(
51-
&oauth2.Token{AccessToken: ghToken},
52-
)
53-
client = oauth2.NewClient(ctx, ts)
54-
} else {
55-
retryableClient := retryablehttp.NewClient()
56-
retryableClient.Logger = nil
57-
client = retryableClient.StandardClient()
50+
ghClient = ghClient.WithAuthToken(ghToken)
5851
}
5952
return &Client{
60-
httpClient: client,
61-
ghClient: github.NewClient(client),
53+
httpClient: httpClient,
54+
ghClient: ghClient,
6255
pypiBaseURL: pypiURL,
6356
}
6457
}
6558

59+
// NewHTTPClient returns an HTTP client which retries transient failures.
60+
func NewHTTPClient() *http.Client {
61+
retryableClient := retryablehttp.NewClient()
62+
retryableClient.Logger = nil
63+
return retryableClient.StandardClient()
64+
}
65+
6666
// Fetch fetches new versions based on the given config and returns a valid semver version
6767
// that can be used with the Go semver package. The version is guaranteed to contain a "v" prefix.
6868
func (c *Client) Fetch(ctx context.Context, config *source.Config) (string, error) {

0 commit comments

Comments
 (0)