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

Add context to http calls #3233

Merged
merged 2 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions internal/nginx/manager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nginx

import (
"context"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -362,7 +363,7 @@ func (lm *LocalManager) SetPlusClients(plusClient *client.NginxClient, plusConfi

// UpdateServersInPlus updates NGINX Plus servers of the given upstream.
func (lm *LocalManager) UpdateServersInPlus(upstream string, servers []string, config ServerConfig) error {
err := verifyConfigVersion(lm.plusConfigVersionCheckClient, lm.configVersion)
err := verifyConfigVersion(lm.plusConfigVersionCheckClient, lm.configVersion, lm.verifyClient.timeout)
if err != nil {
return fmt.Errorf("error verifying config version: %w", err)
}
Expand Down Expand Up @@ -393,7 +394,7 @@ func (lm *LocalManager) UpdateServersInPlus(upstream string, servers []string, c

// UpdateStreamServersInPlus updates NGINX Plus stream servers of the given upstream.
func (lm *LocalManager) UpdateStreamServersInPlus(upstream string, servers []string) error {
err := verifyConfigVersion(lm.plusConfigVersionCheckClient, lm.configVersion)
err := verifyConfigVersion(lm.plusConfigVersionCheckClient, lm.configVersion, lm.verifyClient.timeout)
if err != nil {
return fmt.Errorf("error verifying config version: %w", err)
}
Expand Down Expand Up @@ -432,8 +433,11 @@ func (lm *LocalManager) CreateOpenTracingTracerConfig(content string) error {
// verifyConfigVersion is used to check if the worker process that the API client is connected
// to is using the latest version of nginx config. This way we avoid making changes on
// a worker processes that is being shut down.
func verifyConfigVersion(httpClient *http.Client, configVersion int) error {
req, err := http.NewRequest("GET", "http://nginx-plus-api/configVersionCheck", nil)
func verifyConfigVersion(httpClient *http.Client, configVersion int, timeout time.Duration) error {
ctx := context.Background()
reqContext, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
req, err := http.NewRequestWithContext(reqContext, "GET", "http://nginx-plus-api/configVersionCheck", nil)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}
Expand Down
10 changes: 9 additions & 1 deletion internal/nginx/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ func newVerifyClient(timeout time.Duration) *verifyClient {
// GetConfigVersion get version number that we put in the nginx config to verify that we're using
// the correct config.
func (c *verifyClient) GetConfigVersion() (int, error) {
resp, err := c.client.Get("http://config-version/configVersion")
ctx := context.Background()
reqContext, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
req, err := http.NewRequestWithContext(reqContext, "GET", "http://config-version/configVersion", nil)
if err != nil {
return 0, fmt.Errorf("error creating request: %w", err)
}

resp, err := c.client.Do(req)
if err != nil {
return 0, fmt.Errorf("error getting client: %w", err)
}
Expand Down