mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-02-19 01:17:50 -05:00
[v14.0/forgejo] fix: migrations/github: Wait & retry when primary rate limit is hit (#11054)
**Backport:** https://codeberg.org/forgejo/forgejo/pulls/10846 This is a successor to #10805, which simply did not work. It is also much simpler and basically a one line change to enable an existing feature in [go-github](https://github.com/google/go-github). Fixes #10845 With this fix and #10798 in place, a migration of a repo with ~3K issues and ~1.3k pull requests finally completed successfully. ## Patch We use SleepUntilPrimaryRateLimitResetWhenRateLimited to instruct the go-github code to wait until the retry time and retry the request when the primary rate limit gets hit. ## Test case TestGitHubDownloadRepo() has been modified such that 403 rate limit errors are injected every 7 requests with a retry time of one second, resulting in the rate limit condition being hit twice with the current tests. The test case confirms that the migration code itself is in fact unaffected by the rate limit being hit. ## Scope This change does not affect secondary rate limits. If the server is restarted during the wait for the rate limit refresh, the migration likely still fails when retried, because inserts for already present database objects will be attempted. This approach effectively puts the task's goroutine to sleep until the retry time, which implies that the respective resources stay allocated. A better approach might be to add the necessary infrastructure to support restarts of migration tasks at a later time, but this is much more involved, because the migration state would need to be saved and/or re-created based on already pulled data. This would also require adding support for database upserts. Co-authored-by: Nils Goroll <nils.goroll@uplex.de> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11054 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org> Co-committed-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
This commit is contained in:
parent
4e1468a1b5
commit
4bb2d68a10
6 changed files with 42 additions and 9 deletions
4
assets/go-licenses.json
generated
4
assets/go-licenses.json
generated
|
|
@ -700,8 +700,8 @@
|
|||
"licenseText": "Copyright (c) 2017 The Go Authors. All rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are\nmet:\n\n * Redistributions of source code must retain the above copyright\nnotice, this list of conditions and the following disclaimer.\n * Redistributions in binary form must reproduce the above\ncopyright notice, this list of conditions and the following disclaimer\nin the documentation and/or other materials provided with the\ndistribution.\n * Neither the name of Google Inc. nor the names of its\ncontributors may be used to endorse or promote products derived from\nthis software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\nLIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\nA PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\nOWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\nSPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\nLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\nDATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\nTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
|
||||
},
|
||||
{
|
||||
"name": "github.com/google/go-github/v74/github",
|
||||
"path": "github.com/google/go-github/v74/github/LICENSE",
|
||||
"name": "github.com/google/go-github/v81/github",
|
||||
"path": "github.com/google/go-github/v81/github/LICENSE",
|
||||
"licenseText": "Copyright (c) 2013 The go-github AUTHORS. All rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are\nmet:\n\n * Redistributions of source code must retain the above copyright\nnotice, this list of conditions and the following disclaimer.\n * Redistributions in binary form must reproduce the above\ncopyright notice, this list of conditions and the following disclaimer\nin the documentation and/or other materials provided with the\ndistribution.\n * Neither the name of Google Inc. nor the names of its\ncontributors may be used to endorse or promote products derived from\nthis software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\nLIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\nA PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\nOWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\nSPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\nLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\nDATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\nTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
|
||||
},
|
||||
{
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -58,7 +58,7 @@ require (
|
|||
github.com/gogs/go-gogs-client v0.0.0-20210131175652-1d7215cd8d85
|
||||
github.com/golang-jwt/jwt/v5 v5.3.0
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
|
||||
github.com/google/go-github/v74 v74.0.0
|
||||
github.com/google/go-github/v81 v81.0.0
|
||||
github.com/google/pprof v0.0.0-20251114195745-4902fdda35c8
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/gorilla/feeds v1.2.0
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -384,8 +384,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
|
|||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/go-github/v74 v74.0.0 h1:yZcddTUn8DPbj11GxnMrNiAnXH14gNs559AsUpNpPgM=
|
||||
github.com/google/go-github/v74 v74.0.0/go.mod h1:ubn/YdyftV80VPSI26nSJvaEsTOnsjrxG3o9kJhcyak=
|
||||
github.com/google/go-github/v81 v81.0.0 h1:hTLugQRxSLD1Yei18fk4A5eYjOGLUBKAl/VCqOfFkZc=
|
||||
github.com/google/go-github/v81 v81.0.0/go.mod h1:upyjaybucIbBIuxgJS7YLOZGziyvvJ92WX6WEBNE3sM=
|
||||
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
|
||||
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
|
||||
github.com/google/go-tpm v0.9.5 h1:ocUmnDebX54dnW+MQWGQRbdaAcJELsa6PqZhJ48KwVU=
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ package migrations
|
|||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/google/go-github/v74/github"
|
||||
"github.com/google/go-github/v81/github"
|
||||
)
|
||||
|
||||
// ErrRepoNotCreated returns the error that repository not created
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ import (
|
|||
"forgejo.org/modules/proxy"
|
||||
"forgejo.org/modules/structs"
|
||||
|
||||
"github.com/google/go-github/v74/github"
|
||||
"github.com/google/go-github/v81/github"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
|
|
@ -98,7 +98,6 @@ func NewGithubDownloaderV3(ctx context.Context, baseURL string, getPullRequests,
|
|||
userName: userName,
|
||||
baseURL: baseURL,
|
||||
password: password,
|
||||
ctx: ctx,
|
||||
repoOwner: repoOwner,
|
||||
repoName: repoName,
|
||||
maxPerPage: 100,
|
||||
|
|
@ -106,6 +105,8 @@ func NewGithubDownloaderV3(ctx context.Context, baseURL string, getPullRequests,
|
|||
getIssues: getIssues,
|
||||
}
|
||||
|
||||
downloader.SetContext(ctx)
|
||||
|
||||
if token != "" {
|
||||
tokens := strings.Split(token, ",")
|
||||
for _, token := range tokens {
|
||||
|
|
@ -159,6 +160,7 @@ func (g *GithubDownloaderV3) addClient(client *http.Client, baseURL string) {
|
|||
|
||||
// SetContext set context
|
||||
func (g *GithubDownloaderV3) SetContext(ctx context.Context) {
|
||||
ctx = context.WithValue(ctx, github.SleepUntilPrimaryRateLimitResetWhenRateLimited, true)
|
||||
g.ctx = ctx
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,14 +5,18 @@
|
|||
package migrations
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"forgejo.org/models/unittest"
|
||||
"forgejo.org/modules/log"
|
||||
base "forgejo.org/modules/migration"
|
||||
|
||||
"github.com/google/go-github/v74/github"
|
||||
"github.com/google/go-github/v81/github"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
|
@ -97,6 +101,29 @@ func TestGithubDownloaderFilterComments(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func ratelimitInjectHandler(handler http.Handler, urlpattern *regexp.Regexp, every int) http.HandlerFunc {
|
||||
var requestCount int
|
||||
// because we also count the rate limit response
|
||||
every++
|
||||
|
||||
return (http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
match := urlpattern.MatchString(r.URL.Path)
|
||||
if match {
|
||||
requestCount++
|
||||
}
|
||||
|
||||
if match && requestCount%every == 0 {
|
||||
log.Info("ratelimitInject %s", r.URL)
|
||||
w.Header().Set("X-Ratelimit-Reset",
|
||||
strconv.FormatInt(time.Now().Add(time.Second).Unix(), 10))
|
||||
w.Header().Set("X-Ratelimit-Remaining", "0")
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
} else {
|
||||
handler.ServeHTTP(w, r)
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
func TestGitHubDownloadRepo(t *testing.T) {
|
||||
GithubLimitRateRemaining = 3 // Wait at 3 remaining since we could have 3 CI in //
|
||||
|
||||
|
|
@ -105,6 +132,10 @@ func TestGitHubDownloadRepo(t *testing.T) {
|
|||
server := unittest.NewMockWebServer(t, "https://api.github.com", fixturePath, false)
|
||||
defer server.Close()
|
||||
|
||||
urlpattern := regexp.MustCompile("test_repo/")
|
||||
|
||||
server.Config.Handler = ratelimitInjectHandler(server.Config.Handler, urlpattern, 7)
|
||||
|
||||
downloader := NewGithubDownloaderV3(t.Context(), server.URL, true, true, "", "", token, "forgejo", "test_repo")
|
||||
err := downloader.RefreshRate()
|
||||
require.NoError(t, err)
|
||||
|
|
|
|||
Loading…
Reference in a new issue