Skip to content

Commit 1b7182e

Browse files
authored
Add retry for migration http/https requests (#9019)
* Add retry for migration http/https requests * give the more suitable name for retry configuraion items * fix docs and lint * Only use retryDownloader when setting > 1
1 parent 6fbfffe commit 1b7182e

File tree

7 files changed

+195
-1
lines changed

7 files changed

+195
-1
lines changed

custom/conf/app.ini.sample

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,3 +892,9 @@ QUEUE_LENGTH = 1000
892892
; Task queue connection string, available only when `QUEUE_TYPE` is `redis`.
893893
; If there is a password of redis, use `addrs=127.0.0.1:6379 password=123 db=0`.
894894
QUEUE_CONN_STR = "addrs=127.0.0.1:6379 db=0"
895+
896+
[migrations]
897+
; Max attempts per http/https request on migrations.
898+
MAX_ATTEMPTS = 3
899+
; Backoff time per http/https request retry (seconds)
900+
RETRY_BACKOFF = 3

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,11 @@ Two special environment variables are passed to the render command:
585585
- `QUEUE_LENGTH`: **1000**: Task queue length, available only when `QUEUE_TYPE` is `channel`.
586586
- `QUEUE_CONN_STR`: **addrs=127.0.0.1:6379 db=0**: Task queue connection string, available only when `QUEUE_TYPE` is `redis`. If there redis needs a password, use `addrs=127.0.0.1:6379 password=123 db=0`.
587587

588+
## Migrations (`migrations`)
589+
590+
- `MAX_ATTEMPTS`: **3**: Max attempts per http/https request on migrations.
591+
- `RETRY_BACKOFF`: **3**: Backoff time per http/https request retry (seconds)
592+
588593
## Other (`other`)
589594

590595
- `SHOW_FOOTER_BRANDING`: **false**: Show Gitea branding in the footer.

docs/content/doc/advanced/config-cheat-sheet.zh-cn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@ IS_INPUT_FILE = false
272272
- `QUEUE_LENGTH`: **1000**: 任务队列长度,当 `QUEUE_TYPE``channel` 时有效。
273273
- `QUEUE_CONN_STR`: **addrs=127.0.0.1:6379 db=0**: 任务队列连接字符串,当 `QUEUE_TYPE``redis` 时有效。如果redis有密码,则可以 `addrs=127.0.0.1:6379 password=123 db=0`
274274

275+
## Migrations (`migrations`)
276+
277+
- `MAX_ATTEMPTS`: **3**: 在迁移过程中的 http/https 请求重试次数。
278+
- `RETRY_BACKOFF`: **3**: 等待下一次重试的时间,单位秒。
279+
275280
## Other (`other`)
276281

277282
- `SHOW_FOOTER_BRANDING`: 为真则在页面底部显示Gitea的字样。

modules/migrations/base/downloader.go

Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55

66
package base
77

8-
import "code.gitea.io/gitea/modules/structs"
8+
import (
9+
"time"
10+
11+
"code.gitea.io/gitea/modules/structs"
12+
)
913

1014
// Downloader downloads the site repo informations
1115
type Downloader interface {
@@ -25,3 +29,148 @@ type DownloaderFactory interface {
2529
New(opts MigrateOptions) (Downloader, error)
2630
GitServiceType() structs.GitServiceType
2731
}
32+
33+
// RetryDownloader retry the downloads
34+
type RetryDownloader struct {
35+
Downloader
36+
RetryTimes int // the total execute times
37+
RetryDelay int // time to delay seconds
38+
}
39+
40+
// NewRetryDownloader creates a retry downloader
41+
func NewRetryDownloader(downloader Downloader, retryTimes, retryDelay int) *RetryDownloader {
42+
return &RetryDownloader{
43+
Downloader: downloader,
44+
RetryTimes: retryTimes,
45+
RetryDelay: retryDelay,
46+
}
47+
}
48+
49+
// GetRepoInfo returns a repository information with retry
50+
func (d *RetryDownloader) GetRepoInfo() (*Repository, error) {
51+
var (
52+
times = d.RetryTimes
53+
repo *Repository
54+
err error
55+
)
56+
for ; times > 0; times-- {
57+
if repo, err = d.Downloader.GetRepoInfo(); err == nil {
58+
return repo, nil
59+
}
60+
time.Sleep(time.Second * time.Duration(d.RetryDelay))
61+
}
62+
return nil, err
63+
}
64+
65+
// GetTopics returns a repository's topics with retry
66+
func (d *RetryDownloader) GetTopics() ([]string, error) {
67+
var (
68+
times = d.RetryTimes
69+
topics []string
70+
err error
71+
)
72+
for ; times > 0; times-- {
73+
if topics, err = d.Downloader.GetTopics(); err == nil {
74+
return topics, nil
75+
}
76+
time.Sleep(time.Second * time.Duration(d.RetryDelay))
77+
}
78+
return nil, err
79+
}
80+
81+
// GetMilestones returns a repository's milestones with retry
82+
func (d *RetryDownloader) GetMilestones() ([]*Milestone, error) {
83+
var (
84+
times = d.RetryTimes
85+
milestones []*Milestone
86+
err error
87+
)
88+
for ; times > 0; times-- {
89+
if milestones, err = d.Downloader.GetMilestones(); err == nil {
90+
return milestones, nil
91+
}
92+
time.Sleep(time.Second * time.Duration(d.RetryDelay))
93+
}
94+
return nil, err
95+
}
96+
97+
// GetReleases returns a repository's releases with retry
98+
func (d *RetryDownloader) GetReleases() ([]*Release, error) {
99+
var (
100+
times = d.RetryTimes
101+
releases []*Release
102+
err error
103+
)
104+
for ; times > 0; times-- {
105+
if releases, err = d.Downloader.GetReleases(); err == nil {
106+
return releases, nil
107+
}
108+
time.Sleep(time.Second * time.Duration(d.RetryDelay))
109+
}
110+
return nil, err
111+
}
112+
113+
// GetLabels returns a repository's labels with retry
114+
func (d *RetryDownloader) GetLabels() ([]*Label, error) {
115+
var (
116+
times = d.RetryTimes
117+
labels []*Label
118+
err error
119+
)
120+
for ; times > 0; times-- {
121+
if labels, err = d.Downloader.GetLabels(); err == nil {
122+
return labels, nil
123+
}
124+
time.Sleep(time.Second * time.Duration(d.RetryDelay))
125+
}
126+
return nil, err
127+
}
128+
129+
// GetIssues returns a repository's issues with retry
130+
func (d *RetryDownloader) GetIssues(page, perPage int) ([]*Issue, bool, error) {
131+
var (
132+
times = d.RetryTimes
133+
issues []*Issue
134+
isEnd bool
135+
err error
136+
)
137+
for ; times > 0; times-- {
138+
if issues, isEnd, err = d.Downloader.GetIssues(page, perPage); err == nil {
139+
return issues, isEnd, nil
140+
}
141+
time.Sleep(time.Second * time.Duration(d.RetryDelay))
142+
}
143+
return nil, false, err
144+
}
145+
146+
// GetComments returns a repository's comments with retry
147+
func (d *RetryDownloader) GetComments(issueNumber int64) ([]*Comment, error) {
148+
var (
149+
times = d.RetryTimes
150+
comments []*Comment
151+
err error
152+
)
153+
for ; times > 0; times-- {
154+
if comments, err = d.Downloader.GetComments(issueNumber); err == nil {
155+
return comments, nil
156+
}
157+
time.Sleep(time.Second * time.Duration(d.RetryDelay))
158+
}
159+
return nil, err
160+
}
161+
162+
// GetPullRequests returns a repository's pull requests with retry
163+
func (d *RetryDownloader) GetPullRequests(page, perPage int) ([]*PullRequest, error) {
164+
var (
165+
times = d.RetryTimes
166+
prs []*PullRequest
167+
err error
168+
)
169+
for ; times > 0; times-- {
170+
if prs, err = d.Downloader.GetPullRequests(page, perPage); err == nil {
171+
return prs, nil
172+
}
173+
time.Sleep(time.Second * time.Duration(d.RetryDelay))
174+
}
175+
return nil, err
176+
}

modules/migrations/migrate.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"code.gitea.io/gitea/models"
1212
"code.gitea.io/gitea/modules/log"
1313
"code.gitea.io/gitea/modules/migrations/base"
14+
"code.gitea.io/gitea/modules/setting"
1415
"code.gitea.io/gitea/modules/structs"
1516
)
1617

@@ -63,6 +64,11 @@ func MigrateRepository(doer *models.User, ownerName string, opts base.MigrateOpt
6364
}
6465

6566
uploader.gitServiceType = opts.GitServiceType
67+
68+
if setting.Migrations.MaxAttempts > 1 {
69+
downloader = base.NewRetryDownloader(downloader, setting.Migrations.MaxAttempts, setting.Migrations.RetryBackoff)
70+
}
71+
6672
if err := migrateRepository(downloader, uploader, opts); err != nil {
6773
if err1 := uploader.Rollback(); err1 != nil {
6874
log.Error("rollback failed: %v", err1)

modules/setting/migrations.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package setting
6+
7+
var (
8+
// Migrations settings
9+
Migrations = struct {
10+
MaxAttempts int
11+
RetryBackoff int
12+
}{
13+
MaxAttempts: 3,
14+
RetryBackoff: 3,
15+
}
16+
)
17+
18+
func newMigrationsService() {
19+
sec := Cfg.Section("migrations")
20+
Migrations.MaxAttempts = sec.Key("MAX_ATTEMPTS").MustInt(Migrations.MaxAttempts)
21+
Migrations.RetryBackoff = sec.Key("RETRY_BACKOFF").MustInt(Migrations.RetryBackoff)
22+
}

modules/setting/setting.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,7 @@ func NewServices() {
10621062
newRegisterMailService()
10631063
newNotifyMailService()
10641064
newWebhookService()
1065+
newMigrationsService()
10651066
newIndexerService()
10661067
newTaskService()
10671068
}

0 commit comments

Comments
 (0)