Skip to content

Commit b899713

Browse files
committed
Add X-Duration-Seconds for HTTP mode
- adds X-Duration-Seconds header to the HTTP mode only to bring parity with the Classic Watchdog. Tested with error and success HTTP codes. Signed-off-by: Alex Ellis <[email protected]>
1 parent 4f66f5a commit b899713

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

executor/http_runner.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ package executor
22

33
import (
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
89102
func (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()

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func main() {
5454
}
5555

5656
listenUntilShutdown(shutdownTimeout, s, watchdogConfig.SuppressLock)
57+
5758
}
5859

5960
func markUnhealthy() error {

0 commit comments

Comments
 (0)