Fetcher retry (#403)
Before this change, poolmgr tried to hit fetcher just once. However if the pod has just started, fetcher may not be up yet, causing the initial request to fail with a connection refused error. This change adds a similar retry loop that we currently use on the specialize endpoint of the runtime environment container.
This commit is contained in:
@@ -4,12 +4,14 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
//"time"
|
"net/url"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/fission/fission"
|
"github.com/fission/fission"
|
||||||
"github.com/fission/fission/environments/fetcher"
|
"github.com/fission/fission/environments/fetcher"
|
||||||
//"github.com/fission/fission/router"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@@ -30,21 +32,35 @@ func (c *Client) Fetch(fr *fetcher.FetchRequest) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// client := http.Client{
|
maxRetries := 20
|
||||||
// Transport: router.MakeRetryingRoundTripper(10, 50*time.Millisecond),
|
var resp *http.Response
|
||||||
// }
|
|
||||||
|
|
||||||
resp, err := http.Post(c.url, "application/json", bytes.NewReader(body))
|
for i := 0; i < maxRetries; i++ {
|
||||||
if err != nil {
|
resp, err := http.Post(c.url, "application/json", bytes.NewReader(body))
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
if resp.StatusCode != 200 {
|
if err == nil && resp.StatusCode == 200 {
|
||||||
return fission.MakeErrorFromHTTP(resp)
|
defer resp.Body.Close()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only retry for the specific case of a connection error.
|
||||||
|
if urlErr, ok := err.(*url.Error); ok {
|
||||||
|
if netErr, ok := urlErr.Err.(*net.OpError); ok {
|
||||||
|
if netErr.Op == "dial" {
|
||||||
|
if i < maxRetries-1 {
|
||||||
|
time.Sleep(50 * time.Duration(2*i) * time.Millisecond)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
if err == nil {
|
||||||
|
err = fission.MakeErrorFromHTTP(resp)
|
||||||
|
}
|
||||||
|
log.Printf("Failed to fetch: %v", err)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Upload(fr *fetcher.UploadRequest) (*fetcher.UploadResponse, error) {
|
func (c *Client) Upload(fr *fetcher.UploadRequest) (*fetcher.UploadResponse, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user