From fb2f666535d96b06dc4df901375defdfac0ac43d Mon Sep 17 00:00:00 2001 From: Nils Goroll Date: Mon, 26 Jan 2026 14:47:08 +0100 Subject: [PATCH] fix: Wait & retry when primary rate limit are hit for Github migration (#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. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10846 Reviewed-by: Gusted Co-authored-by: Nils Goroll Co-committed-by: Nils Goroll --- assets/go-licenses.json | 4 ++-- go.mod | 2 +- go.sum | 4 ++-- services/migrations/error.go | 2 +- services/migrations/github.go | 6 ++++-- services/migrations/github_test.go | 33 +++++++++++++++++++++++++++++- 6 files changed, 42 insertions(+), 9 deletions(-) diff --git a/assets/go-licenses.json b/assets/go-licenses.json index 4db7e9638d..80523116cc 100644 --- a/assets/go-licenses.json +++ b/assets/go-licenses.json @@ -695,8 +695,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" }, { diff --git a/go.mod b/go.mod index fafb1e93eb..21ea81336c 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index 692a9e9cbd..68961f91d6 100644 --- a/go.sum +++ b/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= diff --git a/services/migrations/error.go b/services/migrations/error.go index 2dd9647d95..dee008a071 100644 --- a/services/migrations/error.go +++ b/services/migrations/error.go @@ -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 diff --git a/services/migrations/github.go b/services/migrations/github.go index 87e8248cab..569d9c5780 100644 --- a/services/migrations/github.go +++ b/services/migrations/github.go @@ -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 } diff --git a/services/migrations/github_test.go b/services/migrations/github_test.go index 786becd420..f87d38b121 100644 --- a/services/migrations/github_test.go +++ b/services/migrations/github_test.go @@ -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)