From dfcf961f7528e0ba138cbc7017824a96fd498aa5 Mon Sep 17 00:00:00 2001 From: Gaurav Gahlot Date: Thu, 19 Aug 2021 19:13:33 +0530 Subject: [PATCH] Change default function timeout to 60 seconds and pass context to executor from router (#2169) Patch changes default function timeout to 60 seconds. Also, we are propagating context from router to executor in a proper way. --- pkg/fission-cli/flag/flag.go | 2 +- pkg/router/functionHandler.go | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/pkg/fission-cli/flag/flag.go b/pkg/fission-cli/flag/flag.go index 30db02e4..81c44a88 100644 --- a/pkg/fission-cli/flag/flag.go +++ b/pkg/fission-cli/flag/flag.go @@ -113,7 +113,7 @@ var ( FnLogReverseQuery = Flag{Type: Bool, Name: flagkey.FnLogReverseQuery, Short: "r", Usage: "Specify the log reverse query base on time, it will be invalid if the 'follow' flag is specified"} FnLogCount = Flag{Type: Int, Name: flagkey.FnLogCount, Usage: "Get N most recent log records", DefaultValue: 20} FnTestBody = Flag{Type: String, Name: flagkey.FnTestBody, Short: "b", Usage: "Request body"} - FnTestTimeout = Flag{Type: Duration, Name: flagkey.FnTestTimeout, Short: "t", Usage: "Length of time to wait for the response. If set to zero or negative number, no timeout is set", DefaultValue: 30 * time.Second} + FnTestTimeout = Flag{Type: Duration, Name: flagkey.FnTestTimeout, Short: "t", Usage: "Length of time to wait for the response. If set to zero or negative number, no timeout is set", DefaultValue: 60 * time.Second} FnTestHeader = Flag{Type: StringSlice, Name: flagkey.FnTestHeader, Short: "H", Usage: "Request headers"} FnTestQuery = Flag{Type: StringSlice, Name: flagkey.FnTestQuery, Short: "q", Usage: "Request query parameters: -q key1=value1 -q key2=value2"} FnIdleTimeout = Flag{Type: Int, Name: flagkey.FnIdleTimeout, Usage: "The length of time (in seconds) that a function is idle before pod(s) are eligible for recycling", DefaultValue: 120} diff --git a/pkg/router/functionHandler.go b/pkg/router/functionHandler.go index 7b19c079..d32ff2bf 100644 --- a/pkg/router/functionHandler.go +++ b/pkg/router/functionHandler.go @@ -651,16 +651,21 @@ func (fh functionHandler) removeServiceEntryFromCache() { } } -func (fh functionHandler) getServiceEntryFromExecutor() (serviceUrl *url.URL, err error) { +func (fh functionHandler) getServiceEntryFromExecutor(ctx context.Context) (serviceUrl *url.URL, err error) { // send a request to executor to specialize a new pod fh.logger.Debug("function timeout specified", zap.Int("timeout", fh.function.Spec.FunctionTimeout)) - timeout := 30 * time.Second + + var fContext context.Context if fh.function.Spec.FunctionTimeout > 0 { - timeout = time.Second * time.Duration(fh.function.Spec.FunctionTimeout) + timeout := time.Second * time.Duration(fh.function.Spec.FunctionTimeout) + f, cancel := context.WithTimeout(ctx, timeout) + fContext = f + defer cancel() + } else { + fContext = ctx } - ctx, cancel := context.WithTimeout(context.Background(), timeout) - defer cancel() - service, err := fh.executor.GetServiceForFunction(ctx, fh.function) + + service, err := fh.executor.GetServiceForFunction(fContext, fh.function) if err != nil { statusCode, errMsg := ferror.GetHTTPError(err) fh.logger.Error("error from GetServiceForFunction", @@ -684,7 +689,7 @@ func (fh functionHandler) getServiceEntryFromExecutor() (serviceUrl *url.URL, er // getServiceEntryFromExecutor returns service url entry returns from executor func (fh functionHandler) getServiceEntry(ctx context.Context) (svcURL *url.URL, cacheHit bool, err error) { if fh.function.Spec.InvokeStrategy.ExecutionStrategy.ExecutorType == fv1.ExecutorTypePoolmgr { - svcURL, err = fh.getServiceEntryFromExecutor() + svcURL, err = fh.getServiceEntryFromExecutor(ctx) return svcURL, false, err } // Check if service URL present in cache @@ -706,7 +711,7 @@ func (fh functionHandler) getServiceEntry(ctx context.Context) (svcURL *url.URL, } return svcEntryRecord{svcURL: svcURL, cacheHit: true}, err } - svcURL, err = fh.getServiceEntryFromExecutor() + svcURL, err = fh.getServiceEntryFromExecutor(ctx) if err != nil { return nil, err }