fix webhook timeout bug (#15613)

* Also fix the potential problem in httplib
This commit is contained in:
Lunny Xiao 2021-04-26 02:48:12 +08:00 committed by GitHub
parent 6ea6e2b4eb
commit 3d5bb3e6a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 10 deletions

View File

@ -325,7 +325,7 @@ func (r *Request) getResponse() (*http.Response, error) {
trans = &http.Transport{ trans = &http.Transport{
TLSClientConfig: r.setting.TLSClientConfig, TLSClientConfig: r.setting.TLSClientConfig,
Proxy: proxy, Proxy: proxy,
Dial: TimeoutDialer(r.setting.ConnectTimeout, r.setting.ReadWriteTimeout), Dial: TimeoutDialer(r.setting.ConnectTimeout),
} }
} else if t, ok := trans.(*http.Transport); ok { } else if t, ok := trans.(*http.Transport); ok {
if t.TLSClientConfig == nil { if t.TLSClientConfig == nil {
@ -335,7 +335,7 @@ func (r *Request) getResponse() (*http.Response, error) {
t.Proxy = r.setting.Proxy t.Proxy = r.setting.Proxy
} }
if t.Dial == nil { if t.Dial == nil {
t.Dial = TimeoutDialer(r.setting.ConnectTimeout, r.setting.ReadWriteTimeout) t.Dial = TimeoutDialer(r.setting.ConnectTimeout)
} }
} }
@ -352,6 +352,7 @@ func (r *Request) getResponse() (*http.Response, error) {
client := &http.Client{ client := &http.Client{
Transport: trans, Transport: trans,
Jar: jar, Jar: jar,
Timeout: r.setting.ReadWriteTimeout,
} }
if len(r.setting.UserAgent) > 0 && len(r.req.Header.Get("User-Agent")) == 0 { if len(r.setting.UserAgent) > 0 && len(r.req.Header.Get("User-Agent")) == 0 {
@ -457,12 +458,12 @@ func (r *Request) Response() (*http.Response, error) {
} }
// TimeoutDialer returns functions of connection dialer with timeout settings for http.Transport Dial field. // TimeoutDialer returns functions of connection dialer with timeout settings for http.Transport Dial field.
func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error) { func TimeoutDialer(cTimeout time.Duration) func(net, addr string) (c net.Conn, err error) {
return func(netw, addr string) (net.Conn, error) { return func(netw, addr string) (net.Conn, error) {
conn, err := net.DialTimeout(netw, addr, cTimeout) conn, err := net.DialTimeout(netw, addr, cTimeout)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return conn, conn.SetDeadline(time.Now().Add(rwTimeout)) return conn, nil
} }
} }

View File

@ -271,14 +271,10 @@ func InitDeliverHooks() {
TLSClientConfig: &tls.Config{InsecureSkipVerify: setting.Webhook.SkipTLSVerify}, TLSClientConfig: &tls.Config{InsecureSkipVerify: setting.Webhook.SkipTLSVerify},
Proxy: webhookProxy(), Proxy: webhookProxy(),
Dial: func(netw, addr string) (net.Conn, error) { Dial: func(netw, addr string) (net.Conn, error) {
conn, err := net.DialTimeout(netw, addr, timeout) return net.DialTimeout(netw, addr, timeout) // dial timeout
if err != nil {
return nil, err
}
return conn, conn.SetDeadline(time.Now().Add(timeout))
}, },
}, },
Timeout: timeout, // request timeout
} }
go graceful.GetManager().RunWithShutdownContext(DeliverHooks) go graceful.GetManager().RunWithShutdownContext(DeliverHooks)