Skip to content

Commit

Permalink
Copy upstream response into http writer
Browse files Browse the repository at this point in the history
As an optimization, the response body for the HTTP runner can
stream data back, instead of buffering it all in memory and
then sending it in one shot.

This change means replacing an io.ReadAll with an io.Copy, as a
result, it's possible that some callers will no longer receive
a content-length, but a stream of data with a length of -1.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Jul 16, 2024
1 parent a6a3f4c commit ef933c8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
11 changes: 9 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM scratch as cache
FROM scratch AS cache

COPY bin .

FROM scratch as ship
FROM scratch AS ship

ARG TARGETPLATFORM
ARG BUILDPLATFORM
Expand All @@ -11,4 +11,11 @@ ARG TARGETARCH

COPY --from=cache /fwatchdog-$TARGETARCH ./fwatchdog

LABEL org.label-schema.license="MIT" \
org.label-schema.vcs-url="https://github.com/openfaas/of-watchdog" \
org.label-schema.vcs-type="Git" \
org.label-schema.name="openfaas/of-watchdog" \
org.label-schema.vendor="openfaas" \
org.label-schema.docker.schema-version="1.0"

ENTRYPOINT ["/fwatchdog"]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ Example usage for testing:
* Forward to an Nginx container:

```
$ go build ; mode=http port=8081 fprocess="docker run -p 80:80 --name nginx -t nginx" upstream_url=http://127.0.0.1:80 ./of-watchdog
$ go build && mode=http port=8081 fprocess="docker run -p 80:80 --name nginx -t nginx" upstream_url=http://127.0.0.1:80 ./of-watchdog
```

* Forward to a Node.js / Express.js hello-world app:

```
$ go build ; mode=http port=8081 fprocess="node expressjs-hello-world.js" upstream_url=http://127.0.0.1:3000 ./of-watchdog
$ go build && mode=http port=8081 fprocess="node expressjs-hello-world.js" upstream_url=http://127.0.0.1:3000 ./of-watchdog
```

Cons:
Expand Down
6 changes: 2 additions & 4 deletions executor/http_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,9 @@ func (f *HTTPFunctionRunner) Run(req FunctionRequest, contentLength int64, r *ht
if res.Body != nil {
defer res.Body.Close()

bodyBytes, bodyErr := io.ReadAll(res.Body)
if bodyErr != nil {
log.Println("read body err", bodyErr)
if _, err := io.Copy(w, res.Body); err != nil {
log.Printf("Error copying response body: %s", err)
}
w.Write(bodyBytes)
}

// Exclude logging for health check probes from the kubelet which can spam
Expand Down

0 comments on commit ef933c8

Please sign in to comment.