gitea源码

retry_downloader.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package migration
  4. import (
  5. "context"
  6. "time"
  7. )
  8. var _ Downloader = &RetryDownloader{}
  9. // RetryDownloader retry the downloads
  10. type RetryDownloader struct {
  11. Downloader
  12. RetryTimes int // the total execute times
  13. RetryDelay int // time to delay seconds
  14. }
  15. // NewRetryDownloader creates a retry downloader
  16. func NewRetryDownloader(downloader Downloader, retryTimes, retryDelay int) *RetryDownloader {
  17. return &RetryDownloader{
  18. Downloader: downloader,
  19. RetryTimes: retryTimes,
  20. RetryDelay: retryDelay,
  21. }
  22. }
  23. func (d *RetryDownloader) retry(ctx context.Context, work func(context.Context) error) error {
  24. var (
  25. times = d.RetryTimes
  26. err error
  27. )
  28. for ; times > 0; times-- {
  29. if err = work(ctx); err == nil {
  30. return nil
  31. }
  32. if IsErrNotSupported(err) {
  33. return err
  34. }
  35. select {
  36. case <-ctx.Done():
  37. return ctx.Err()
  38. case <-time.After(time.Second * time.Duration(d.RetryDelay)):
  39. }
  40. }
  41. return err
  42. }
  43. // GetRepoInfo returns a repository information with retry
  44. func (d *RetryDownloader) GetRepoInfo(ctx context.Context) (*Repository, error) {
  45. var (
  46. repo *Repository
  47. err error
  48. )
  49. err = d.retry(ctx, func(ctx context.Context) error {
  50. repo, err = d.Downloader.GetRepoInfo(ctx)
  51. return err
  52. })
  53. return repo, err
  54. }
  55. // GetTopics returns a repository's topics with retry
  56. func (d *RetryDownloader) GetTopics(ctx context.Context) ([]string, error) {
  57. var (
  58. topics []string
  59. err error
  60. )
  61. err = d.retry(ctx, func(ctx context.Context) error {
  62. topics, err = d.Downloader.GetTopics(ctx)
  63. return err
  64. })
  65. return topics, err
  66. }
  67. // GetMilestones returns a repository's milestones with retry
  68. func (d *RetryDownloader) GetMilestones(ctx context.Context) ([]*Milestone, error) {
  69. var (
  70. milestones []*Milestone
  71. err error
  72. )
  73. err = d.retry(ctx, func(ctx context.Context) error {
  74. milestones, err = d.Downloader.GetMilestones(ctx)
  75. return err
  76. })
  77. return milestones, err
  78. }
  79. // GetReleases returns a repository's releases with retry
  80. func (d *RetryDownloader) GetReleases(ctx context.Context) ([]*Release, error) {
  81. var (
  82. releases []*Release
  83. err error
  84. )
  85. err = d.retry(ctx, func(ctx context.Context) error {
  86. releases, err = d.Downloader.GetReleases(ctx)
  87. return err
  88. })
  89. return releases, err
  90. }
  91. // GetLabels returns a repository's labels with retry
  92. func (d *RetryDownloader) GetLabels(ctx context.Context) ([]*Label, error) {
  93. var (
  94. labels []*Label
  95. err error
  96. )
  97. err = d.retry(ctx, func(ctx context.Context) error {
  98. labels, err = d.Downloader.GetLabels(ctx)
  99. return err
  100. })
  101. return labels, err
  102. }
  103. // GetIssues returns a repository's issues with retry
  104. func (d *RetryDownloader) GetIssues(ctx context.Context, page, perPage int) ([]*Issue, bool, error) {
  105. var (
  106. issues []*Issue
  107. isEnd bool
  108. err error
  109. )
  110. err = d.retry(ctx, func(ctx context.Context) error {
  111. issues, isEnd, err = d.Downloader.GetIssues(ctx, page, perPage)
  112. return err
  113. })
  114. return issues, isEnd, err
  115. }
  116. // GetComments returns a repository's comments with retry
  117. func (d *RetryDownloader) GetComments(ctx context.Context, commentable Commentable) ([]*Comment, bool, error) {
  118. var (
  119. comments []*Comment
  120. isEnd bool
  121. err error
  122. )
  123. err = d.retry(ctx, func(context.Context) error {
  124. comments, isEnd, err = d.Downloader.GetComments(ctx, commentable)
  125. return err
  126. })
  127. return comments, isEnd, err
  128. }
  129. // GetPullRequests returns a repository's pull requests with retry
  130. func (d *RetryDownloader) GetPullRequests(ctx context.Context, page, perPage int) ([]*PullRequest, bool, error) {
  131. var (
  132. prs []*PullRequest
  133. err error
  134. isEnd bool
  135. )
  136. err = d.retry(ctx, func(ctx context.Context) error {
  137. prs, isEnd, err = d.Downloader.GetPullRequests(ctx, page, perPage)
  138. return err
  139. })
  140. return prs, isEnd, err
  141. }
  142. // GetReviews returns pull requests reviews
  143. func (d *RetryDownloader) GetReviews(ctx context.Context, reviewable Reviewable) ([]*Review, error) {
  144. var (
  145. reviews []*Review
  146. err error
  147. )
  148. err = d.retry(ctx, func(ctx context.Context) error {
  149. reviews, err = d.Downloader.GetReviews(ctx, reviewable)
  150. return err
  151. })
  152. return reviews, err
  153. }