From 850509e77c470cee0fe741085395c3ef0d194f41 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 25 Jan 2020 10:16:41 +0800 Subject: [PATCH] chore(healthy): disable proxy in healthy check (#457) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixed: https://github.com/appleboy/gorush/issues/456 Don’t use Go’s default HTTP client (in production) ref: https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779 --- main.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 15f1582..1f40772 100644 --- a/main.go +++ b/main.go @@ -4,10 +4,12 @@ import ( "flag" "fmt" "log" + "net" "net/http" "os" "path/filepath" "strconv" + "time" "github.com/appleboy/gorush/config" "github.com/appleboy/gorush/gorush" @@ -291,12 +293,22 @@ func usage() { // handles pinging the endpoint and returns an error if the // agent is in an unhealthy state. func pinger() error { - resp, err := http.Get("http://localhost:" + gorush.PushConf.Core.Port + gorush.PushConf.API.HealthURI) + var transport = &http.Transport{ + Dial: (&net.Dialer{ + Timeout: 5 * time.Second, + }).Dial, + TLSHandshakeTimeout: 5 * time.Second, + } + var client = &http.Client{ + Timeout: time.Second * 10, + Transport: transport, + } + resp, err := client.Get("http://localhost:" + gorush.PushConf.Core.Port + gorush.PushConf.API.HealthURI) if err != nil { return err } defer resp.Body.Close() - if resp.StatusCode != 200 { + if resp.StatusCode != http.StatusOK { return fmt.Errorf("server returned non-200 status code") } return nil