Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor http client for unix sockets #4334

Merged
merged 1 commit into from
Jul 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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