@@ -31,217 +31,166 @@ func NewRetryDownloader(ctx context.Context, downloader Downloader, retryTimes,
31
31
}
32
32
}
33
33
34
- // SetContext set context
35
- func (d * RetryDownloader ) SetContext (ctx context.Context ) {
36
- d .ctx = ctx
37
- d .Downloader .SetContext (ctx )
38
- }
39
-
40
- // GetRepoInfo returns a repository information with retry
41
- func (d * RetryDownloader ) GetRepoInfo () (* Repository , error ) {
34
+ func (d * RetryDownloader ) retry (work func () error ) error {
42
35
var (
43
36
times = d .RetryTimes
44
- repo * Repository
45
37
err error
46
38
)
47
39
for ; times > 0 ; times -- {
48
- if repo , err = d . Downloader . GetRepoInfo (); err == nil {
49
- return repo , nil
40
+ if err = work (); err == nil {
41
+ return nil
50
42
}
51
43
if IsErrNotSupported (err ) {
52
- return nil , err
44
+ return err
53
45
}
54
46
select {
55
47
case <- d .ctx .Done ():
56
- return nil , d .ctx .Err ()
48
+ return d .ctx .Err ()
57
49
case <- time .After (time .Second * time .Duration (d .RetryDelay )):
58
50
}
59
51
}
60
- return nil , err
52
+ return err
53
+ }
54
+
55
+ // SetContext set context
56
+ func (d * RetryDownloader ) SetContext (ctx context.Context ) {
57
+ d .ctx = ctx
58
+ d .Downloader .SetContext (ctx )
59
+ }
60
+
61
+ // GetRepoInfo returns a repository information with retry
62
+ func (d * RetryDownloader ) GetRepoInfo () (* Repository , error ) {
63
+ var (
64
+ repo * Repository
65
+ err error
66
+ )
67
+
68
+ err = d .retry (func () error {
69
+ repo , err = d .Downloader .GetRepoInfo ()
70
+ return err
71
+ })
72
+
73
+ return repo , err
61
74
}
62
75
63
76
// GetTopics returns a repository's topics with retry
64
77
func (d * RetryDownloader ) GetTopics () ([]string , error ) {
65
78
var (
66
- times = d .RetryTimes
67
79
topics []string
68
80
err error
69
81
)
70
- for ; times > 0 ; times -- {
71
- if topics , err = d .Downloader .GetTopics (); err == nil {
72
- return topics , nil
73
- }
74
- if IsErrNotSupported (err ) {
75
- return nil , err
76
- }
77
- select {
78
- case <- d .ctx .Done ():
79
- return nil , d .ctx .Err ()
80
- case <- time .After (time .Second * time .Duration (d .RetryDelay )):
81
- }
82
- }
83
- return nil , err
82
+
83
+ err = d .retry (func () error {
84
+ topics , err = d .Downloader .GetTopics ()
85
+ return err
86
+ })
87
+
88
+ return topics , err
84
89
}
85
90
86
91
// GetMilestones returns a repository's milestones with retry
87
92
func (d * RetryDownloader ) GetMilestones () ([]* Milestone , error ) {
88
93
var (
89
- times = d .RetryTimes
90
94
milestones []* Milestone
91
95
err error
92
96
)
93
- for ; times > 0 ; times -- {
94
- if milestones , err = d .Downloader .GetMilestones (); err == nil {
95
- return milestones , nil
96
- }
97
- if IsErrNotSupported (err ) {
98
- return nil , err
99
- }
100
- select {
101
- case <- d .ctx .Done ():
102
- return nil , d .ctx .Err ()
103
- case <- time .After (time .Second * time .Duration (d .RetryDelay )):
104
- }
105
- }
106
- return nil , err
97
+
98
+ err = d .retry (func () error {
99
+ milestones , err = d .Downloader .GetMilestones ()
100
+ return err
101
+ })
102
+
103
+ return milestones , err
107
104
}
108
105
109
106
// GetReleases returns a repository's releases with retry
110
107
func (d * RetryDownloader ) GetReleases () ([]* Release , error ) {
111
108
var (
112
- times = d .RetryTimes
113
109
releases []* Release
114
110
err error
115
111
)
116
- for ; times > 0 ; times -- {
117
- if releases , err = d .Downloader .GetReleases (); err == nil {
118
- return releases , nil
119
- }
120
- if IsErrNotSupported (err ) {
121
- return nil , err
122
- }
123
- select {
124
- case <- d .ctx .Done ():
125
- return nil , d .ctx .Err ()
126
- case <- time .After (time .Second * time .Duration (d .RetryDelay )):
127
- }
128
- }
129
- return nil , err
112
+
113
+ err = d .retry (func () error {
114
+ releases , err = d .Downloader .GetReleases ()
115
+ return err
116
+ })
117
+
118
+ return releases , err
130
119
}
131
120
132
121
// GetLabels returns a repository's labels with retry
133
122
func (d * RetryDownloader ) GetLabels () ([]* Label , error ) {
134
123
var (
135
- times = d .RetryTimes
136
124
labels []* Label
137
125
err error
138
126
)
139
- for ; times > 0 ; times -- {
140
- if labels , err = d .Downloader .GetLabels (); err == nil {
141
- return labels , nil
142
- }
143
- if IsErrNotSupported (err ) {
144
- return nil , err
145
- }
146
- select {
147
- case <- d .ctx .Done ():
148
- return nil , d .ctx .Err ()
149
- case <- time .After (time .Second * time .Duration (d .RetryDelay )):
150
- }
151
- }
152
- return nil , err
127
+
128
+ err = d .retry (func () error {
129
+ labels , err = d .Downloader .GetLabels ()
130
+ return err
131
+ })
132
+
133
+ return labels , err
153
134
}
154
135
155
136
// GetIssues returns a repository's issues with retry
156
137
func (d * RetryDownloader ) GetIssues (page , perPage int ) ([]* Issue , bool , error ) {
157
138
var (
158
- times = d .RetryTimes
159
139
issues []* Issue
160
140
isEnd bool
161
141
err error
162
142
)
163
- for ; times > 0 ; times -- {
164
- if issues , isEnd , err = d .Downloader .GetIssues (page , perPage ); err == nil {
165
- return issues , isEnd , nil
166
- }
167
- if IsErrNotSupported (err ) {
168
- return nil , false , err
169
- }
170
- select {
171
- case <- d .ctx .Done ():
172
- return nil , false , d .ctx .Err ()
173
- case <- time .After (time .Second * time .Duration (d .RetryDelay )):
174
- }
175
- }
176
- return nil , false , err
143
+
144
+ err = d .retry (func () error {
145
+ issues , isEnd , err = d .Downloader .GetIssues (page , perPage )
146
+ return err
147
+ })
148
+
149
+ return issues , isEnd , err
177
150
}
178
151
179
152
// GetComments returns a repository's comments with retry
180
153
func (d * RetryDownloader ) GetComments (issueNumber int64 ) ([]* Comment , error ) {
181
154
var (
182
- times = d .RetryTimes
183
155
comments []* Comment
184
156
err error
185
157
)
186
- for ; times > 0 ; times -- {
187
- if comments , err = d .Downloader .GetComments (issueNumber ); err == nil {
188
- return comments , nil
189
- }
190
- if IsErrNotSupported (err ) {
191
- return nil , err
192
- }
193
- select {
194
- case <- d .ctx .Done ():
195
- return nil , d .ctx .Err ()
196
- case <- time .After (time .Second * time .Duration (d .RetryDelay )):
197
- }
198
- }
199
- return nil , err
158
+
159
+ err = d .retry (func () error {
160
+ comments , err = d .Downloader .GetComments (issueNumber )
161
+ return err
162
+ })
163
+
164
+ return comments , err
200
165
}
201
166
202
167
// GetPullRequests returns a repository's pull requests with retry
203
168
func (d * RetryDownloader ) GetPullRequests (page , perPage int ) ([]* PullRequest , bool , error ) {
204
169
var (
205
- times = d .RetryTimes
206
170
prs []* PullRequest
207
171
err error
208
172
isEnd bool
209
173
)
210
- for ; times > 0 ; times -- {
211
- if prs , isEnd , err = d .Downloader .GetPullRequests (page , perPage ); err == nil {
212
- return prs , isEnd , nil
213
- }
214
- if IsErrNotSupported (err ) {
215
- return nil , false , err
216
- }
217
- select {
218
- case <- d .ctx .Done ():
219
- return nil , false , d .ctx .Err ()
220
- case <- time .After (time .Second * time .Duration (d .RetryDelay )):
221
- }
222
- }
223
- return nil , false , err
174
+
175
+ err = d .retry (func () error {
176
+ prs , isEnd , err = d .Downloader .GetPullRequests (page , perPage )
177
+ return err
178
+ })
179
+
180
+ return prs , isEnd , err
224
181
}
225
182
226
183
// GetReviews returns pull requests reviews
227
184
func (d * RetryDownloader ) GetReviews (pullRequestNumber int64 ) ([]* Review , error ) {
228
185
var (
229
- times = d .RetryTimes
230
186
reviews []* Review
231
187
err error
232
188
)
233
- for ; times > 0 ; times -- {
234
- if reviews , err = d .Downloader .GetReviews (pullRequestNumber ); err == nil {
235
- return reviews , nil
236
- }
237
- if IsErrNotSupported (err ) {
238
- return nil , err
239
- }
240
- select {
241
- case <- d .ctx .Done ():
242
- return nil , d .ctx .Err ()
243
- case <- time .After (time .Second * time .Duration (d .RetryDelay )):
244
- }
245
- }
246
- return nil , err
189
+
190
+ err = d .retry (func () error {
191
+ reviews , err = d .Downloader .GetReviews (pullRequestNumber )
192
+ return err
193
+ })
194
+
195
+ return reviews , err
247
196
}
0 commit comments