@@ -2,14 +2,18 @@ package executor
22
33import (
44 "context"
5+ "fmt"
56 "io"
67 "io/ioutil"
78 "log"
89 "net"
910 "net/http"
1011 "net/url"
12+ "os"
1113 "os/exec"
14+ "os/signal"
1215 "sync"
16+ "syscall"
1317 "time"
1418)
1519
@@ -82,11 +86,21 @@ func (f *HTTPFunctionRunner) Start() error {
8286
8387 f .Client = makeProxyClient (f .ExecTimeout )
8488
89+ go func () {
90+ sig := make (chan os.Signal , 1 )
91+ signal .Notify (sig , syscall .SIGTERM )
92+
93+ <- sig
94+ cmd .Process .Signal (syscall .SIGTERM )
95+
96+ }()
97+
8598 return cmd .Start ()
8699}
87100
88101// Run a function with a long-running process with a HTTP protocol for communication
89102func (f * HTTPFunctionRunner ) Run (req FunctionRequest , contentLength int64 , r * http.Request , w http.ResponseWriter ) error {
103+ startedTime := time .Now ()
90104
91105 upstreamURL := f .UpstreamURL .String ()
92106
@@ -113,6 +127,8 @@ func (f *HTTPFunctionRunner) Run(req FunctionRequest, contentLength int64, r *ht
113127
114128 // Error unrelated to context / deadline
115129 if ctx .Err () == nil {
130+ w .Header ().Set ("X-Duration-Seconds" , fmt .Sprintf ("%f" , time .Since (startedTime ).Seconds ()))
131+
116132 w .WriteHeader (http .StatusInternalServerError )
117133
118134 return nil
@@ -124,6 +140,7 @@ func (f *HTTPFunctionRunner) Run(req FunctionRequest, contentLength int64, r *ht
124140 if ctx .Err () != nil {
125141 // Error due to timeout / deadline
126142 log .Printf ("Upstream HTTP killed due to exec_timeout: %s\n " , f .ExecTimeout )
143+ w .Header ().Set ("X-Duration-Seconds" , fmt .Sprintf ("%f" , time .Since (startedTime ).Seconds ()))
127144
128145 w .WriteHeader (http .StatusGatewayTimeout )
129146 return nil
@@ -132,12 +149,15 @@ func (f *HTTPFunctionRunner) Run(req FunctionRequest, contentLength int64, r *ht
132149 }
133150 }
134151
152+ w .Header ().Set ("X-Duration-Seconds" , fmt .Sprintf ("%f" , time .Since (startedTime ).Seconds ()))
135153 w .WriteHeader (http .StatusInternalServerError )
136154 return err
137155 }
138156
139157 copyHeaders (w .Header (), & res .Header )
140158
159+ w .Header ().Set ("X-Duration-Seconds" , fmt .Sprintf ("%f" , time .Since (startedTime ).Seconds ()))
160+
141161 w .WriteHeader (res .StatusCode )
142162 if res .Body != nil {
143163 defer res .Body .Close ()
0 commit comments