Skip to content

Commit 15577fa

Browse files
lucacomecoolbry95
authored andcommitted
Add context to http calls (nginx#3233)
1 parent 795aea1 commit 15577fa

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

internal/nginx/manager.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package nginx
22

33
import (
4+
"context"
45
"fmt"
56
"net/http"
67
"os"
@@ -362,7 +363,7 @@ func (lm *LocalManager) SetPlusClients(plusClient *client.NginxClient, plusConfi
362363

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

394395
// UpdateStreamServersInPlus updates NGINX Plus stream servers of the given upstream.
395396
func (lm *LocalManager) UpdateStreamServersInPlus(upstream string, servers []string) error {
396-
err := verifyConfigVersion(lm.plusConfigVersionCheckClient, lm.configVersion)
397+
err := verifyConfigVersion(lm.plusConfigVersionCheckClient, lm.configVersion, lm.verifyClient.timeout)
397398
if err != nil {
398399
return fmt.Errorf("error verifying config version: %w", err)
399400
}
@@ -432,8 +433,11 @@ func (lm *LocalManager) CreateOpenTracingTracerConfig(content string) error {
432433
// verifyConfigVersion is used to check if the worker process that the API client is connected
433434
// to is using the latest version of nginx config. This way we avoid making changes on
434435
// a worker processes that is being shut down.
435-
func verifyConfigVersion(httpClient *http.Client, configVersion int) error {
436-
req, err := http.NewRequest("GET", "http://nginx-plus-api/configVersionCheck", nil)
436+
func verifyConfigVersion(httpClient *http.Client, configVersion int, timeout time.Duration) error {
437+
ctx := context.Background()
438+
reqContext, cancel := context.WithTimeout(ctx, timeout)
439+
defer cancel()
440+
req, err := http.NewRequestWithContext(reqContext, "GET", "http://nginx-plus-api/configVersionCheck", nil)
437441
if err != nil {
438442
return fmt.Errorf("error creating request: %w", err)
439443
}

internal/nginx/verify.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@ func newVerifyClient(timeout time.Duration) *verifyClient {
3737
// GetConfigVersion get version number that we put in the nginx config to verify that we're using
3838
// the correct config.
3939
func (c *verifyClient) GetConfigVersion() (int, error) {
40-
resp, err := c.client.Get("http://config-version/configVersion")
40+
ctx := context.Background()
41+
reqContext, cancel := context.WithTimeout(ctx, c.timeout)
42+
defer cancel()
43+
req, err := http.NewRequestWithContext(reqContext, "GET", "http://config-version/configVersion", nil)
44+
if err != nil {
45+
return 0, fmt.Errorf("error creating request: %w", err)
46+
}
47+
48+
resp, err := c.client.Do(req)
4149
if err != nil {
4250
return 0, fmt.Errorf("error getting client: %w", err)
4351
}

0 commit comments

Comments
 (0)