Skip to content

Commit a8c2c56

Browse files
authored
Merge pull request #1 from wxiaoguang/fix-git-hook-timeout
use 0 as timeout
2 parents f4061d7 + bb3560c commit a8c2c56

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

modules/private/hook.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/url"
1010

1111
"code.gitea.io/gitea/modules/git"
12+
"code.gitea.io/gitea/modules/httplib"
1213
"code.gitea.io/gitea/modules/repository"
1314
"code.gitea.io/gitea/modules/setting"
1415
)
@@ -81,26 +82,32 @@ type HookProcReceiveRefResult struct {
8182
HeadBranch string
8283
}
8384

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 ever 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+
8495
// HookPreReceive check whether the provided commits are allowed
8596
func HookPreReceive(ctx context.Context, ownerName, repoName string, opts HookOptions) ResponseExtra {
86-
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/pre-receive/%s/%s", url.PathEscape(ownerName), url.PathEscape(repoName))
87-
req := newInternalRequestAPI(ctx, reqURL, "POST", opts)
97+
req := newInternalRequestAPIForHooks(ctx, "pre-receive", ownerName, repoName, opts)
8898
_, extra := requestJSONResp(req, &ResponseText{})
8999
return extra
90100
}
91101

92102
// HookPostReceive updates services and users
93103
func HookPostReceive(ctx context.Context, ownerName, repoName string, opts HookOptions) (*HookPostReceiveResult, ResponseExtra) {
94-
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/post-receive/%s/%s", url.PathEscape(ownerName), url.PathEscape(repoName))
95-
req := newInternalRequestAPI(ctx, reqURL, "POST", opts)
104+
req := newInternalRequestAPIForHooks(ctx, "post-receive", ownerName, repoName, opts)
96105
return requestJSONResp(req, &HookPostReceiveResult{})
97106
}
98107

99108
// HookProcReceive proc-receive hook
100109
func HookProcReceive(ctx context.Context, ownerName, repoName string, opts HookOptions) (*HookProcReceiveResult, ResponseExtra) {
101-
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/proc-receive/%s/%s", url.PathEscape(ownerName), url.PathEscape(repoName))
102-
103-
req := newInternalRequestAPI(ctx, reqURL, "POST", opts)
110+
req := newInternalRequestAPIForHooks(ctx, "proc-receive", ownerName, repoName, opts)
104111
return requestJSONResp(req, &HookProcReceiveResult{})
105112
}
106113

0 commit comments

Comments
 (0)