chore(healthy): disable proxy in healthy check (#457)

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
This commit is contained in:
Bo-Yi Wu 2020-01-25 10:16:41 +08:00 committed by GitHub
parent c5a41eda4c
commit 850509e77c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 2 deletions

16
main.go
View File

@ -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