Skip to content

Commit 77a037f

Browse files
Reuse httpClient in ilp_offloading_policy.go
1 parent 4a93b53 commit 77a037f

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

internal/workflow/ilp_offloading_policy.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ func initParams() ilpParams {
5959
const CLOUD = "CLOUD"
6060
const LOCAL = "LOCAL"
6161

62+
var httpClient = &http.Client{Timeout: 10 * time.Second}
63+
6264
func tupleKey(s1, s2 string) string {
6365
keyBytes, _ := json.Marshal([]string{s1, s2})
6466
return string(keyBytes)
@@ -281,16 +283,15 @@ func (policy *IlpOffloadingPolicy) Evaluate(r *Request, p *Progress) (Offloading
281283
url := "http://localhost:8080/" // TODO: configurable
282284
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
283285
if err != nil {
284-
panic(err)
286+
return OffloadingDecision{Offload: false}, fmt.Errorf("creating request: %w", err)
285287
}
286288
req.Header.Set("Content-Type", "application/json")
287289

288290
// Send the request
289-
client := &http.Client{Timeout: 10 * time.Second} // TODO: check if http client is cached
290-
resp, err := client.Do(req)
291+
resp, err := httpClient.Do(req)
291292
if err != nil {
292293
fmt.Println(err)
293-
return OffloadingDecision{Offload: false}, err
294+
return OffloadingDecision{Offload: false}, fmt.Errorf("sending request: %w", err)
294295
}
295296
defer resp.Body.Close()
296297

@@ -299,7 +300,7 @@ func (policy *IlpOffloadingPolicy) Evaluate(r *Request, p *Progress) (Offloading
299300
err = json.NewDecoder(resp.Body).Decode(&placement)
300301
if err != nil {
301302
fmt.Println(err)
302-
return OffloadingDecision{Offload: false}, err
303+
return OffloadingDecision{Offload: false}, fmt.Errorf("decoding response: %w", err)
303304
}
304305

305306
for k, v := range placement {

0 commit comments

Comments
 (0)