From 2f124e4b767a98294f562629a56d7fc520696ba6 Mon Sep 17 00:00:00 2001 From: Manuel Alejandro de Brito Fontes Date: Fri, 19 Jul 2019 10:42:44 -0400 Subject: [PATCH] Refactor http client for unix sockets --- internal/nginx/main.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/internal/nginx/main.go b/internal/nginx/main.go index 967b9f1566..95236b7447 100644 --- a/internal/nginx/main.go +++ b/internal/nginx/main.go @@ -50,11 +50,17 @@ var StreamSocket = "/tmp/ingress-stream.sock" var statusLocation = "nginx-status" +var httpClient *http.Client + +func init() { + httpClient = buildUnixSocketClient(HealthCheckTimeout) +} + // NewGetStatusRequest creates a new GET request to the internal NGINX status server func NewGetStatusRequest(path string) (int, []byte, error) { - url := fmt.Sprintf("http+unix://%v%v", statusLocation, path) + url := fmt.Sprintf("%v://%v%v", httpunix.Scheme, statusLocation, path) - res, err := buildUnixSocketClient(HealthCheckTimeout).Get(url) + res, err := httpClient.Get(url) if err != nil { return 0, nil, err } @@ -70,14 +76,14 @@ func NewGetStatusRequest(path string) (int, []byte, error) { // NewPostStatusRequest creates a new POST request to the internal NGINX status server func NewPostStatusRequest(path, contentType string, data interface{}) (int, []byte, error) { - url := fmt.Sprintf("http+unix://%v%v", statusLocation, path) + url := fmt.Sprintf("%v://%v%v", httpunix.Scheme, statusLocation, path) buf, err := json.Marshal(data) if err != nil { return 0, nil, err } - res, err := buildUnixSocketClient(HealthCheckTimeout).Post(url, contentType, bytes.NewReader(buf)) + res, err := httpClient.Post(url, contentType, bytes.NewReader(buf)) if err != nil { return 0, nil, err } @@ -112,11 +118,11 @@ func GetServerBlock(conf string, host string) (string, error) { // ReadNginxConf reads the nginx configuration file into a string func ReadNginxConf() (string, error) { - return ReadFileToString("/etc/nginx/nginx.conf") + return readFileToString("/etc/nginx/nginx.conf") } -// ReadFileToString reads any file into a string -func ReadFileToString(path string) (string, error) { +// readFileToString reads any file into a string +func readFileToString(path string) (string, error) { f, err := os.Open(path) if err != nil { return "", err