@@ -79,13 +79,21 @@ func printEndReports(invokeId string, initDuration string, memorySize string, in
7979 invokeId , invokeDuration , math .Ceil (invokeDuration ), memorySize , memorySize )
8080}
8181
82- func formatInitDuration (sandbox Sandbox , initStart time.Time , timeoutDuration time.Duration ) string {
83- if initStart .IsZero () {
84- return ""
82+ func startInitOnce (sandbox Sandbox , functionVersion string , timeout int64 , bs interop.Bootstrap ) time.Time {
83+ initMutex .Lock ()
84+ defer initMutex .Unlock ()
85+
86+ if initDone {
87+ return time.Time {}
8588 }
8689
87- initEnd := sandbox .AwaitInitCompletion ()
88- if initEnd .IsZero () {
90+ initStart := InitHandler (sandbox , functionVersion , timeout , bs )
91+ initDone = true
92+ return initStart
93+ }
94+
95+ func formatInitDuration (initStart time.Time , initEnd time.Time , timeoutDuration time.Duration ) string {
96+ if initStart .IsZero () || initEnd .IsZero () {
8997 return ""
9098 }
9199
@@ -121,14 +129,7 @@ func InvokeHandler(w http.ResponseWriter, r *http.Request, sandbox Sandbox, bs i
121129 functionVersion := GetenvWithDefault ("AWS_LAMBDA_FUNCTION_VERSION" , "$LATEST" )
122130 memorySize := GetenvWithDefault ("AWS_LAMBDA_FUNCTION_MEMORY_SIZE" , "3008" )
123131
124- var initStart time.Time
125- initMutex .Lock ()
126- if ! initDone {
127- initStart = InitHandler (sandbox , functionVersion , timeout , bs )
128- // Set initDone so next invokes do not try to Init the function again
129- initDone = true
130- }
131- initMutex .Unlock ()
132+ initStart := startInitOnce (sandbox , functionVersion , timeout , bs )
132133
133134 invokeStart := time .Now ()
134135 invokeID := r .Header .Get ("X-Amzn-RequestId" )
@@ -211,7 +212,8 @@ func InvokeHandler(w http.ResponseWriter, r *http.Request, sandbox Sandbox, bs i
211212 w .WriteHeader (http .StatusGatewayTimeout )
212213 return
213214 case rapidcore .ErrInvokeTimeout :
214- printEndReports (invokePayload .ID , formatInitDuration (sandbox , initStart , timeoutDuration ), memorySize , invokeStart , timeoutDuration )
215+ initEnd := sandbox .AwaitInitCompletion ()
216+ printEndReports (invokePayload .ID , formatInitDuration (initStart , initEnd , timeoutDuration ), memorySize , invokeStart , timeoutDuration )
215217
216218 w .Write ([]byte (fmt .Sprintf ("Task timed out after %d.00 seconds" , timeout )))
217219 time .Sleep (100 * time .Millisecond )
@@ -220,7 +222,12 @@ func InvokeHandler(w http.ResponseWriter, r *http.Request, sandbox Sandbox, bs i
220222 }
221223 }
222224
223- printEndReports (invokePayload .ID , formatInitDuration (sandbox , initStart , timeoutDuration ), memorySize , invokeStart , timeoutDuration )
225+ initEnd := sandbox .AwaitInitCompletion ()
226+ initDuration := formatInitDuration (initStart , initEnd , timeoutDuration )
227+ if ! initStart .IsZero () && initEnd .After (invokeStart ) {
228+ invokeStart = initEnd
229+ }
230+ printEndReports (invokePayload .ID , initDuration , memorySize , invokeStart , timeoutDuration )
224231
225232 if invokeResp .StatusCode != 0 {
226233 w .WriteHeader (invokeResp .StatusCode )
0 commit comments