Skip to content

Commit b5f8c4a

Browse files
GiteaBotMik4sawxiaoguang
authored
Drop timeout for requests made to the internal hook api (#33947) (#33970)
Backport #33947 by Mik4sa Co-authored-by: Kai Leonhardt <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent d6cee7c commit b5f8c4a

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

modules/private/hook.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"context"
88
"fmt"
99
"net/url"
10-
"time"
1110

1211
"code.gitea.io/gitea/modules/git"
12+
"code.gitea.io/gitea/modules/httplib"
1313
"code.gitea.io/gitea/modules/repository"
1414
"code.gitea.io/gitea/modules/setting"
1515
)
@@ -82,29 +82,32 @@ type HookProcReceiveRefResult struct {
8282
HeadBranch string
8383
}
8484

85+
func newInternalRequestAPIForHooks(ctx context.Context, hookName, ownerName, repoName string, opts HookOptions) *httplib.Request {
86+
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/%s/%s/%s", hookName, url.PathEscape(ownerName), url.PathEscape(repoName))
87+
req := newInternalRequestAPI(ctx, reqURL, "POST", opts)
88+
// This "timeout" applies to http.Client's timeout: A Timeout of zero means no timeout.
89+
// This "timeout" was previously set to `time.Duration(60+len(opts.OldCommitIDs))` seconds, but it caused unnecessary timeout failures.
90+
// It should be good enough to remove the client side timeout, only respect the "ctx" and server side timeout.
91+
req.SetReadWriteTimeout(0)
92+
return req
93+
}
94+
8595
// HookPreReceive check whether the provided commits are allowed
8696
func HookPreReceive(ctx context.Context, ownerName, repoName string, opts HookOptions) ResponseExtra {
87-
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/pre-receive/%s/%s", url.PathEscape(ownerName), url.PathEscape(repoName))
88-
req := newInternalRequestAPI(ctx, reqURL, "POST", opts)
89-
req.SetReadWriteTimeout(time.Duration(60+len(opts.OldCommitIDs)) * time.Second)
97+
req := newInternalRequestAPIForHooks(ctx, "pre-receive", ownerName, repoName, opts)
9098
_, extra := requestJSONResp(req, &ResponseText{})
9199
return extra
92100
}
93101

94102
// HookPostReceive updates services and users
95103
func HookPostReceive(ctx context.Context, ownerName, repoName string, opts HookOptions) (*HookPostReceiveResult, ResponseExtra) {
96-
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/post-receive/%s/%s", url.PathEscape(ownerName), url.PathEscape(repoName))
97-
req := newInternalRequestAPI(ctx, reqURL, "POST", opts)
98-
req.SetReadWriteTimeout(time.Duration(60+len(opts.OldCommitIDs)) * time.Second)
104+
req := newInternalRequestAPIForHooks(ctx, "post-receive", ownerName, repoName, opts)
99105
return requestJSONResp(req, &HookPostReceiveResult{})
100106
}
101107

102108
// HookProcReceive proc-receive hook
103109
func HookProcReceive(ctx context.Context, ownerName, repoName string, opts HookOptions) (*HookProcReceiveResult, ResponseExtra) {
104-
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/proc-receive/%s/%s", url.PathEscape(ownerName), url.PathEscape(repoName))
105-
106-
req := newInternalRequestAPI(ctx, reqURL, "POST", opts)
107-
req.SetReadWriteTimeout(time.Duration(60+len(opts.OldCommitIDs)) * time.Second)
110+
req := newInternalRequestAPIForHooks(ctx, "proc-receive", ownerName, repoName, opts)
108111
return requestJSONResp(req, &HookProcReceiveResult{})
109112
}
110113

0 commit comments

Comments
 (0)