Fix Coroutine Leaks and Proxy Handling in http.Client (#304)

* fix: builtin variable conflict

* feat: add pkg/utils/http HttpClientSkipTlsVerify HttpClient

* fix: realtime create http Transport

* fix: http keepalive connection may not reusable

* feat: allow http request use proxy from environment
This commit is contained in:
Mmx
2023-12-05 23:22:25 +08:00
committed by GitHub
parent 3059bf2d49
commit 1dcd899591
3 changed files with 65 additions and 14 deletions

View File

@@ -1,7 +1,6 @@
package model
import (
"crypto/tls"
"errors"
"fmt"
"io"
@@ -117,11 +116,14 @@ func (ns *NotificationServerBundle) Send(message string) error {
verifySSL = true
}
transCfg := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: verifySSL},
var client *http.Client
if verifySSL {
client = utils.HttpClient
} else {
client = utils.HttpClientSkipTlsVerify
}
client := &http.Client{Transport: transCfg, Timeout: time.Minute * 10}
reqBody, err := ns.reqBody(message)
if err != nil {
return err
@@ -147,13 +149,15 @@ func (ns *NotificationServerBundle) Send(message string) error {
if err != nil {
return err
}
defer func() {
_ = resp.Body.Close()
}()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
defer func() {
_ = resp.Body.Close()
}()
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("%d@%s %s", resp.StatusCode, resp.Status, string(body))
} else {
_, _ = io.Copy(io.Discard, resp.Body)
}
return nil