|
1 | 1 | package provider |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "encoding/json" |
5 | 6 | "fmt" |
6 | 7 | "io" |
@@ -186,36 +187,42 @@ func (provider *Sidecar) sidecarWatcher(stop chan bool, pool *safe.Pool) { |
186 | 187 | } |
187 | 188 |
|
188 | 189 | func (provider *Sidecar) recycleConn(stop chan bool, pool *safe.Pool) { |
189 | | - var err error |
190 | | - var resp *http.Response |
191 | | - var req *http.Request |
192 | 190 | for { |
193 | 191 | select { |
194 | 192 | case <-stop: |
195 | 193 | return |
196 | 194 | default: |
197 | | - //use refresh interval to occasionally reconnect to Sidecar in case the stream connection is lost |
198 | | - req, err = http.NewRequest("GET", provider.Endpoint+"/watch?by_service=false", nil) |
199 | | - if err != nil { |
200 | | - log.Errorf("Error creating http request to Sidecar: %s, Error: %s", provider.Endpoint, err) |
201 | | - continue |
202 | | - } |
203 | | - resp, err = watcherHTTPClient.Do(req) |
204 | | - if err != nil { |
205 | | - log.Errorf("Error connecting to Sidecar: %s, Error: %s", provider.Endpoint, err) |
206 | | - time.Sleep(5 * time.Second) |
207 | | - continue |
208 | | - } |
| 195 | + // Wrap logic in an anonymous function because the defer statement has function scope |
| 196 | + func() { |
| 197 | + // Use refresh interval to occasionally reconnect to Sidecar in case the stream connection is lost |
| 198 | + req, err := http.NewRequest("GET", provider.Endpoint+"/watch?by_service=false", nil) |
| 199 | + if err != nil { |
| 200 | + log.Errorf("Error creating watch request for Sidecar instance '%s': %s", provider.Endpoint, err) |
| 201 | + time.Sleep(5 * time.Second) |
| 202 | + return |
| 203 | + } |
209 | 204 |
|
210 | | - safe.Go(func() { decodeStream(resp.Body, provider.callbackLoader) }) |
| 205 | + ctx, cancel := context.WithCancel(context.Background()) |
| 206 | + // Cancel the infinite timeout request automatically after we reset connTimer |
| 207 | + defer cancel() |
| 208 | + |
| 209 | + req = req.WithContext(ctx) |
| 210 | + |
| 211 | + resp, err := watcherHTTPClient.Do(req) |
| 212 | + if err != nil { |
| 213 | + log.Errorf("Error connecting to Sidecar instance '%s': %s", provider.Endpoint, err) |
| 214 | + time.Sleep(5 * time.Second) |
| 215 | + return |
| 216 | + } |
| 217 | + defer resp.Body.Close() |
211 | 218 |
|
212 | | - //wait on refresh connection timer. If this expires we haven't seen an update in a |
213 | | - //while and should cancel the request, reset the time, and reconnect just in case |
214 | | - <-provider.connTimer.C |
215 | | - provider.connTimer.Reset(time.Duration(provider.RefreshConn)) |
| 219 | + safe.Go(func() { decodeStream(resp.Body, provider.callbackLoader) }) |
216 | 220 |
|
217 | | - //TODO: Deprecated method. Refactor this to use a context. |
218 | | - watcherHTTPTransport.CancelRequest(req) |
| 221 | + // Wait on refresh connection timer. If this expires we haven't seen an update in a |
| 222 | + // while and should cancel the request, reset the time, and reconnect just in case |
| 223 | + <-provider.connTimer.C |
| 224 | + provider.connTimer.Reset(time.Duration(provider.RefreshConn)) |
| 225 | + }() |
219 | 226 | } |
220 | 227 | } |
221 | 228 | } |
|
0 commit comments