From f4061d7d30bdb10b8a82a781733c3c02c9a9b9c2 Mon Sep 17 00:00:00 2001 From: Kai Leonhardt <8343141+Mik4sa@users.noreply.github.com> Date: Fri, 21 Mar 2025 08:26:47 +0100 Subject: [PATCH 1/3] Drop timeout for requests made to the internal hook api --- modules/private/hook.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/modules/private/hook.go b/modules/private/hook.go index 87d6549f9c503..5fccd8be74b13 100644 --- a/modules/private/hook.go +++ b/modules/private/hook.go @@ -7,7 +7,6 @@ import ( "context" "fmt" "net/url" - "time" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/repository" @@ -86,7 +85,6 @@ type HookProcReceiveRefResult struct { func HookPreReceive(ctx context.Context, ownerName, repoName string, opts HookOptions) ResponseExtra { reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/pre-receive/%s/%s", url.PathEscape(ownerName), url.PathEscape(repoName)) req := newInternalRequestAPI(ctx, reqURL, "POST", opts) - req.SetReadWriteTimeout(time.Duration(60+len(opts.OldCommitIDs)) * time.Second) _, extra := requestJSONResp(req, &ResponseText{}) return extra } @@ -95,7 +93,6 @@ func HookPreReceive(ctx context.Context, ownerName, repoName string, opts HookOp func HookPostReceive(ctx context.Context, ownerName, repoName string, opts HookOptions) (*HookPostReceiveResult, ResponseExtra) { reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/post-receive/%s/%s", url.PathEscape(ownerName), url.PathEscape(repoName)) req := newInternalRequestAPI(ctx, reqURL, "POST", opts) - req.SetReadWriteTimeout(time.Duration(60+len(opts.OldCommitIDs)) * time.Second) return requestJSONResp(req, &HookPostReceiveResult{}) } @@ -104,7 +101,6 @@ func HookProcReceive(ctx context.Context, ownerName, repoName string, opts HookO reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/proc-receive/%s/%s", url.PathEscape(ownerName), url.PathEscape(repoName)) req := newInternalRequestAPI(ctx, reqURL, "POST", opts) - req.SetReadWriteTimeout(time.Duration(60+len(opts.OldCommitIDs)) * time.Second) return requestJSONResp(req, &HookProcReceiveResult{}) } From bb3560c83bc02a51ee9973cac7622e089a224b76 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Fri, 21 Mar 2025 15:42:30 +0800 Subject: [PATCH 2/3] fix --- modules/private/hook.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/modules/private/hook.go b/modules/private/hook.go index 5fccd8be74b13..840b0282148b1 100644 --- a/modules/private/hook.go +++ b/modules/private/hook.go @@ -9,6 +9,7 @@ import ( "net/url" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/httplib" "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" ) @@ -81,26 +82,32 @@ type HookProcReceiveRefResult struct { HeadBranch string } +func newInternalRequestAPIForHooks(ctx context.Context, hookName, ownerName, repoName string, opts HookOptions) *httplib.Request { + reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/%s/%s/%s", hookName, url.PathEscape(ownerName), url.PathEscape(repoName)) + req := newInternalRequestAPI(ctx, reqURL, "POST", opts) + // This "timeout" applies to http.Client's timeout: A Timeout of zero means no timeout. + // This "timeout" was ever set to `time.Duration(60+len(opts.OldCommitIDs))` seconds, but it caused unnecessary timeout failures. + // It should be good enough to remove the client side timeout, only respect the "ctx" and server side timeout. + req.SetReadWriteTimeout(0) + return req +} + // HookPreReceive check whether the provided commits are allowed func HookPreReceive(ctx context.Context, ownerName, repoName string, opts HookOptions) ResponseExtra { - reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/pre-receive/%s/%s", url.PathEscape(ownerName), url.PathEscape(repoName)) - req := newInternalRequestAPI(ctx, reqURL, "POST", opts) + req := newInternalRequestAPIForHooks(ctx, "pre-receive", ownerName, repoName, opts) _, extra := requestJSONResp(req, &ResponseText{}) return extra } // HookPostReceive updates services and users func HookPostReceive(ctx context.Context, ownerName, repoName string, opts HookOptions) (*HookPostReceiveResult, ResponseExtra) { - reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/post-receive/%s/%s", url.PathEscape(ownerName), url.PathEscape(repoName)) - req := newInternalRequestAPI(ctx, reqURL, "POST", opts) + req := newInternalRequestAPIForHooks(ctx, "post-receive", ownerName, repoName, opts) return requestJSONResp(req, &HookPostReceiveResult{}) } // HookProcReceive proc-receive hook func HookProcReceive(ctx context.Context, ownerName, repoName string, opts HookOptions) (*HookProcReceiveResult, ResponseExtra) { - reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/proc-receive/%s/%s", url.PathEscape(ownerName), url.PathEscape(repoName)) - - req := newInternalRequestAPI(ctx, reqURL, "POST", opts) + req := newInternalRequestAPIForHooks(ctx, "proc-receive", ownerName, repoName, opts) return requestJSONResp(req, &HookProcReceiveResult{}) } From bd80d6a40cd9a85ad72c1f9907d7fcd1891872e9 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Fri, 21 Mar 2025 16:42:22 +0800 Subject: [PATCH 3/3] Update modules/private/hook.go --- modules/private/hook.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/private/hook.go b/modules/private/hook.go index 840b0282148b1..215996b9b9936 100644 --- a/modules/private/hook.go +++ b/modules/private/hook.go @@ -86,7 +86,7 @@ func newInternalRequestAPIForHooks(ctx context.Context, hookName, ownerName, rep reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/%s/%s/%s", hookName, url.PathEscape(ownerName), url.PathEscape(repoName)) req := newInternalRequestAPI(ctx, reqURL, "POST", opts) // This "timeout" applies to http.Client's timeout: A Timeout of zero means no timeout. - // This "timeout" was ever set to `time.Duration(60+len(opts.OldCommitIDs))` seconds, but it caused unnecessary timeout failures. + // This "timeout" was previously set to `time.Duration(60+len(opts.OldCommitIDs))` seconds, but it caused unnecessary timeout failures. // It should be good enough to remove the client side timeout, only respect the "ctx" and server side timeout. req.SetReadWriteTimeout(0) return req