Skip to content

Commit

Permalink
Merge pull request #4334 from aledbf/refactor-http-client
Browse files Browse the repository at this point in the history
Refactor http client for unix sockets
  • Loading branch information
k8s-ci-robot authored Jul 19, 2019
2 parents 589c9a2 + 2f124e4 commit 05d3aa8
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions internal/nginx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 05d3aa8

Please sign in to comment.