Skip to content

Commit

Permalink
Refactor context
Browse files Browse the repository at this point in the history
  • Loading branch information
myleshorton committed Dec 9, 2024
1 parent bbc1ac4 commit cce1b17
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions services/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,10 @@ func (cs *configService) fetch() (*apipb.ConfigResponse, int64, error) {
resp *http.Response
sleep int64
)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
for {
req, err := cs.newRequest()
req, err := cs.newRequest(ctx)
if err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -226,7 +228,7 @@ func (cs *configService) fetch() (*apipb.ConfigResponse, int64, error) {

// newRequest returns a new ConfigRequest with the current client info, proxy ids, and the last
// time the config was fetched.
func (cs *configService) newRequest() (*http.Request, error) {
func (cs *configService) newRequest(ctx context.Context) (*http.Request, error) {
conf := cs.configHandler.GetConfig()
proxies := []*apipb.ProxyConnectConfig{}
if conf != nil { // not the first request
Expand All @@ -251,7 +253,6 @@ func (cs *configService) newRequest() (*http.Request, error) {
return nil, fmt.Errorf("unable to marshal config request: %w", err)
}

ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, cs.opts.OriginURL, bytes.NewReader(buf))
if err != nil {
return nil, fmt.Errorf("unable to create request")
Expand Down

0 comments on commit cce1b17

Please sign in to comment.