@@ -3,6 +3,7 @@ package maven
3
3
import (
4
4
"bytes"
5
5
"encoding/json"
6
+ "errors"
6
7
"fmt"
7
8
"net/http"
8
9
"time"
@@ -20,6 +21,8 @@ type Feed struct {
20
21
options feeds.FeedOptions
21
22
}
22
23
24
+ var ErrMaxRetriesReached = errors .New ("maximum retries reached due to rate limiting" )
25
+
23
26
func New (feedOptions feeds.FeedOptions ) (* Feed , error ) {
24
27
if feedOptions .Packages != nil {
25
28
return nil , feeds.UnsupportedOptionError {
@@ -52,39 +55,54 @@ type Response struct {
52
55
53
56
// fetchPackages fetches packages from Sonatype API for the given page.
54
57
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
+ }
62
69
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
+ }
67
74
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 ()
74
86
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
+ }
80
96
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
86
104
}
87
- return response . Components , nil
105
+ return nil , ErrMaxRetriesReached
88
106
}
89
107
90
108
func (feed Feed ) Latest (cutoff time.Time ) ([]* feeds.Package , time.Time , []error ) {
0 commit comments