small refactor for retry downloader (#16137)

Signed-off-by: a1012112796 <1012112796@qq.com>
This commit is contained in:
a1012112796 2021-06-13 22:59:19 +08:00 committed by GitHub
parent 5c2f9b3f83
commit a005265718
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -31,6 +31,27 @@ func NewRetryDownloader(ctx context.Context, downloader Downloader, retryTimes,
} }
} }
func (d *RetryDownloader) retry(work func() error) error {
var (
times = d.RetryTimes
err error
)
for ; times > 0; times-- {
if err = work(); err == nil {
return nil
}
if IsErrNotSupported(err) {
return err
}
select {
case <-d.ctx.Done():
return d.ctx.Err()
case <-time.After(time.Second * time.Duration(d.RetryDelay)):
}
}
return err
}
// SetContext set context // SetContext set context
func (d *RetryDownloader) SetContext(ctx context.Context) { func (d *RetryDownloader) SetContext(ctx context.Context) {
d.ctx = ctx d.ctx = ctx
@ -40,208 +61,136 @@ func (d *RetryDownloader) SetContext(ctx context.Context) {
// GetRepoInfo returns a repository information with retry // GetRepoInfo returns a repository information with retry
func (d *RetryDownloader) GetRepoInfo() (*Repository, error) { func (d *RetryDownloader) GetRepoInfo() (*Repository, error) {
var ( var (
times = d.RetryTimes repo *Repository
repo *Repository err error
err error
) )
for ; times > 0; times-- {
if repo, err = d.Downloader.GetRepoInfo(); err == nil { err = d.retry(func() error {
return repo, nil repo, err = d.Downloader.GetRepoInfo()
} return err
if IsErrNotSupported(err) { })
return nil, err
} return repo, err
select {
case <-d.ctx.Done():
return nil, d.ctx.Err()
case <-time.After(time.Second * time.Duration(d.RetryDelay)):
}
}
return nil, err
} }
// GetTopics returns a repository's topics with retry // GetTopics returns a repository's topics with retry
func (d *RetryDownloader) GetTopics() ([]string, error) { func (d *RetryDownloader) GetTopics() ([]string, error) {
var ( var (
times = d.RetryTimes
topics []string topics []string
err error err error
) )
for ; times > 0; times-- {
if topics, err = d.Downloader.GetTopics(); err == nil { err = d.retry(func() error {
return topics, nil topics, err = d.Downloader.GetTopics()
} return err
if IsErrNotSupported(err) { })
return nil, err
} return topics, err
select {
case <-d.ctx.Done():
return nil, d.ctx.Err()
case <-time.After(time.Second * time.Duration(d.RetryDelay)):
}
}
return nil, err
} }
// GetMilestones returns a repository's milestones with retry // GetMilestones returns a repository's milestones with retry
func (d *RetryDownloader) GetMilestones() ([]*Milestone, error) { func (d *RetryDownloader) GetMilestones() ([]*Milestone, error) {
var ( var (
times = d.RetryTimes
milestones []*Milestone milestones []*Milestone
err error err error
) )
for ; times > 0; times-- {
if milestones, err = d.Downloader.GetMilestones(); err == nil { err = d.retry(func() error {
return milestones, nil milestones, err = d.Downloader.GetMilestones()
} return err
if IsErrNotSupported(err) { })
return nil, err
} return milestones, err
select {
case <-d.ctx.Done():
return nil, d.ctx.Err()
case <-time.After(time.Second * time.Duration(d.RetryDelay)):
}
}
return nil, err
} }
// GetReleases returns a repository's releases with retry // GetReleases returns a repository's releases with retry
func (d *RetryDownloader) GetReleases() ([]*Release, error) { func (d *RetryDownloader) GetReleases() ([]*Release, error) {
var ( var (
times = d.RetryTimes
releases []*Release releases []*Release
err error err error
) )
for ; times > 0; times-- {
if releases, err = d.Downloader.GetReleases(); err == nil { err = d.retry(func() error {
return releases, nil releases, err = d.Downloader.GetReleases()
} return err
if IsErrNotSupported(err) { })
return nil, err
} return releases, err
select {
case <-d.ctx.Done():
return nil, d.ctx.Err()
case <-time.After(time.Second * time.Duration(d.RetryDelay)):
}
}
return nil, err
} }
// GetLabels returns a repository's labels with retry // GetLabels returns a repository's labels with retry
func (d *RetryDownloader) GetLabels() ([]*Label, error) { func (d *RetryDownloader) GetLabels() ([]*Label, error) {
var ( var (
times = d.RetryTimes
labels []*Label labels []*Label
err error err error
) )
for ; times > 0; times-- {
if labels, err = d.Downloader.GetLabels(); err == nil { err = d.retry(func() error {
return labels, nil labels, err = d.Downloader.GetLabels()
} return err
if IsErrNotSupported(err) { })
return nil, err
} return labels, err
select {
case <-d.ctx.Done():
return nil, d.ctx.Err()
case <-time.After(time.Second * time.Duration(d.RetryDelay)):
}
}
return nil, err
} }
// GetIssues returns a repository's issues with retry // GetIssues returns a repository's issues with retry
func (d *RetryDownloader) GetIssues(page, perPage int) ([]*Issue, bool, error) { func (d *RetryDownloader) GetIssues(page, perPage int) ([]*Issue, bool, error) {
var ( var (
times = d.RetryTimes
issues []*Issue issues []*Issue
isEnd bool isEnd bool
err error err error
) )
for ; times > 0; times-- {
if issues, isEnd, err = d.Downloader.GetIssues(page, perPage); err == nil { err = d.retry(func() error {
return issues, isEnd, nil issues, isEnd, err = d.Downloader.GetIssues(page, perPage)
} return err
if IsErrNotSupported(err) { })
return nil, false, err
} return issues, isEnd, err
select {
case <-d.ctx.Done():
return nil, false, d.ctx.Err()
case <-time.After(time.Second * time.Duration(d.RetryDelay)):
}
}
return nil, false, err
} }
// GetComments returns a repository's comments with retry // GetComments returns a repository's comments with retry
func (d *RetryDownloader) GetComments(issueNumber int64) ([]*Comment, error) { func (d *RetryDownloader) GetComments(issueNumber int64) ([]*Comment, error) {
var ( var (
times = d.RetryTimes
comments []*Comment comments []*Comment
err error err error
) )
for ; times > 0; times-- {
if comments, err = d.Downloader.GetComments(issueNumber); err == nil { err = d.retry(func() error {
return comments, nil comments, err = d.Downloader.GetComments(issueNumber)
} return err
if IsErrNotSupported(err) { })
return nil, err
} return comments, err
select {
case <-d.ctx.Done():
return nil, d.ctx.Err()
case <-time.After(time.Second * time.Duration(d.RetryDelay)):
}
}
return nil, err
} }
// GetPullRequests returns a repository's pull requests with retry // GetPullRequests returns a repository's pull requests with retry
func (d *RetryDownloader) GetPullRequests(page, perPage int) ([]*PullRequest, bool, error) { func (d *RetryDownloader) GetPullRequests(page, perPage int) ([]*PullRequest, bool, error) {
var ( var (
times = d.RetryTimes
prs []*PullRequest prs []*PullRequest
err error err error
isEnd bool isEnd bool
) )
for ; times > 0; times-- {
if prs, isEnd, err = d.Downloader.GetPullRequests(page, perPage); err == nil { err = d.retry(func() error {
return prs, isEnd, nil prs, isEnd, err = d.Downloader.GetPullRequests(page, perPage)
} return err
if IsErrNotSupported(err) { })
return nil, false, err
} return prs, isEnd, err
select {
case <-d.ctx.Done():
return nil, false, d.ctx.Err()
case <-time.After(time.Second * time.Duration(d.RetryDelay)):
}
}
return nil, false, err
} }
// GetReviews returns pull requests reviews // GetReviews returns pull requests reviews
func (d *RetryDownloader) GetReviews(pullRequestNumber int64) ([]*Review, error) { func (d *RetryDownloader) GetReviews(pullRequestNumber int64) ([]*Review, error) {
var ( var (
times = d.RetryTimes
reviews []*Review reviews []*Review
err error err error
) )
for ; times > 0; times-- {
if reviews, err = d.Downloader.GetReviews(pullRequestNumber); err == nil { err = d.retry(func() error {
return reviews, nil reviews, err = d.Downloader.GetReviews(pullRequestNumber)
} return err
if IsErrNotSupported(err) { })
return nil, err
} return reviews, err
select {
case <-d.ctx.Done():
return nil, d.ctx.Err()
case <-time.After(time.Second * time.Duration(d.RetryDelay)):
}
}
return nil, err
} }