Round Tripper of Fission Router: parameters be configurable (#713)

The Round Tripper parameters of timeout, keep alive time and Max retry
times is configured in router system environment.

And they are set to Round Tripper when router service initializing.

This setup new nested structure `tsRoundTripperParams` to transfer them.
This commit is contained in:
xiekeyang
2018-07-12 12:08:52 -07:00
committed by Soam Vasani
parent 2982efebb8
commit 3707b95edb
7 changed files with 103 additions and 33 deletions
+24 -17
View File
@@ -33,18 +33,24 @@ import (
executorClient "github.com/fission/fission/executor/client"
)
type tsRoundTripperParams struct {
timeout time.Duration
timeoutExponent int
keepAlive time.Duration
maxRetries int
}
type functionHandler struct {
fmap *functionServiceMap
executor *executorClient.Client
function *metav1.ObjectMeta
httpTrigger *crd.HTTPTrigger
fmap *functionServiceMap
executor *executorClient.Client
function *metav1.ObjectMeta
httpTrigger *crd.HTTPTrigger
tsRoundTripperParams *tsRoundTripperParams
}
// A layer on top of http.DefaultTransport, with retries.
type RetryingRoundTripper struct {
maxRetries int
initialTimeout time.Duration
funcHandler *functionHandler
funcHandler *functionHandler
}
// RoundTrip is a custom transport with retries for http requests that forwards the request to the right serviceUrl, obtained
@@ -92,7 +98,6 @@ func (roundTripper RetryingRoundTripper) RoundTrip(req *http.Request) (resp *htt
}
// set the timeout for transport context
timeout := roundTripper.initialTimeout
transport := http.DefaultTransport.(*http.Transport)
// Disables caching, Please refer to issue and specifically comment: https://github.com/fission/fission/issues/723#issuecomment-398781995
transport.DisableKeepAlives = true
@@ -104,7 +109,9 @@ func (roundTripper RetryingRoundTripper) RoundTrip(req *http.Request) (resp *htt
needExecutor = true
}
for i := 0; i < roundTripper.maxRetries-1; i++ {
executingTimeout := roundTripper.funcHandler.tsRoundTripperParams.timeout
for i := 0; i < roundTripper.funcHandler.tsRoundTripperParams.maxRetries-1; i++ {
if needExecutor {
log.Printf("Calling getServiceForFunction for function: %s", roundTripper.funcHandler.function.Name)
@@ -149,8 +156,8 @@ func (roundTripper RetryingRoundTripper) RoundTrip(req *http.Request) (resp *htt
// over-riding default settings.
transport.DialContext = (&net.Dialer{
Timeout: timeout,
KeepAlive: 30 * time.Second,
Timeout: executingTimeout,
KeepAlive: roundTripper.funcHandler.tsRoundTripperParams.keepAlive,
}).DialContext
overhead := time.Since(startTime)
@@ -183,9 +190,11 @@ func (roundTripper RetryingRoundTripper) RoundTrip(req *http.Request) (resp *htt
// just retry after backing off for timeout period.
if serviceUrlFromExecutor {
log.Printf("request to %s errored out. backing off for %v before retrying",
req.URL.Host, timeout)
timeout *= time.Duration(2)
time.Sleep(timeout)
req.URL.Host, executingTimeout)
executingTimeout = executingTimeout * time.Duration(roundTripper.funcHandler.tsRoundTripperParams.timeoutExponent)
time.Sleep(executingTimeout)
needExecutor = false
continue
} else {
@@ -231,9 +240,7 @@ func (fh *functionHandler) handler(responseWriter http.ResponseWriter, request *
proxy := &httputil.ReverseProxy{
Director: director,
Transport: &RetryingRoundTripper{
initialTimeout: 50 * time.Millisecond,
maxRetries: 10,
funcHandler: fh,
funcHandler: fh,
},
}