Skip to content

Commit e4d6c4c

Browse files
authored
Merge pull request #8 from stacklok/fix-upstream
fix: add changes requested upstream
2 parents f9d28d3 + 96a8a2c commit e4d6c4c

File tree

1 file changed

+46
-28
lines changed

1 file changed

+46
-28
lines changed

pkg/feeds/maven/maven.go

+46-28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package maven
33
import (
44
"bytes"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"net/http"
89
"time"
@@ -20,6 +21,8 @@ type Feed struct {
2021
options feeds.FeedOptions
2122
}
2223

24+
var ErrMaxRetriesReached = errors.New("maximum retries reached due to rate limiting")
25+
2326
func New(feedOptions feeds.FeedOptions) (*Feed, error) {
2427
if feedOptions.Packages != nil {
2528
return nil, feeds.UnsupportedOptionError{
@@ -52,39 +55,54 @@ type Response struct {
5255

5356
// fetchPackages fetches packages from Sonatype API for the given page.
5457
func (feed Feed) fetchPackages(page int) ([]Package, error) {
55-
// Define the request payload
56-
payload := map[string]interface{}{
57-
"page": page,
58-
"size": 20,
59-
"sortField": "publishedDate",
60-
"sortDirection": "desc",
61-
}
58+
maxRetries := 5
59+
retryDelay := 5 * time.Second
60+
61+
for attempt := 0; attempt <= maxRetries; attempt++ {
62+
// Define the request payload
63+
payload := map[string]interface{}{
64+
"page": page,
65+
"size": 20,
66+
"sortField": "publishedDate",
67+
"sortDirection": "desc",
68+
}
6269

63-
jsonPayload, err := json.Marshal(payload)
64-
if err != nil {
65-
return nil, fmt.Errorf("error encoding JSON: %w", err)
66-
}
70+
jsonPayload, err := json.Marshal(payload)
71+
if err != nil {
72+
return nil, fmt.Errorf("error encoding JSON: %w", err)
73+
}
6774

68-
// Send POST request to Sonatype API.
69-
resp, err := http.Post(feed.baseURL+"?repository=maven-central", "application/json", bytes.NewBuffer(jsonPayload))
70-
if err != nil {
71-
return nil, fmt.Errorf("error sending request: %w", err)
72-
}
73-
defer resp.Body.Close()
75+
// Send POST request to Sonatype API.
76+
resp, err := http.Post(feed.baseURL+"?repository=maven-central", "application/json", bytes.NewBuffer(jsonPayload))
77+
if err != nil {
78+
// Check if maximum retries have been reached
79+
if attempt == maxRetries {
80+
return nil, fmt.Errorf("error sending request: %w", err)
81+
}
82+
time.Sleep(retryDelay) // Wait before retrying
83+
continue
84+
}
85+
defer resp.Body.Close()
7486

75-
// Handle rate limiting (HTTP status code 429).
76-
if resp.StatusCode == http.StatusTooManyRequests {
77-
time.Sleep(5 * time.Second)
78-
return feed.fetchPackages(page) // Retry the request
79-
}
87+
// Handle rate limiting (HTTP status code 429).
88+
if resp.StatusCode == http.StatusTooManyRequests {
89+
// Check if maximum retries have been reached
90+
if attempt == maxRetries {
91+
return nil, ErrMaxRetriesReached
92+
}
93+
time.Sleep(retryDelay) // Wait before retrying
94+
continue
95+
}
8096

81-
// Decode response.
82-
var response Response
83-
err = json.NewDecoder(resp.Body).Decode(&response)
84-
if err != nil {
85-
return nil, fmt.Errorf("error decoding response: %w", err)
97+
// Decode response.
98+
var response Response
99+
err = json.NewDecoder(resp.Body).Decode(&response)
100+
if err != nil {
101+
return nil, fmt.Errorf("error decoding response: %w", err)
102+
}
103+
return response.Components, nil
86104
}
87-
return response.Components, nil
105+
return nil, ErrMaxRetriesReached
88106
}
89107

90108
func (feed Feed) Latest(cutoff time.Time) ([]*feeds.Package, time.Time, []error) {

0 commit comments

Comments
 (0)