Skip to content

Commit 4679f27

Browse files
committed
Support streaming responses from functions
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent 02205b8 commit 4679f27

File tree

8 files changed

+121
-19
lines changed

8 files changed

+121
-19
lines changed

gateway/Dockerfile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM --platform=${BUILDPLATFORM:-linux/amd64} ghcr.io/openfaas/license-check:0.4.1 as license-check
22

3-
FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:1.20 as build
3+
FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:1.21 as build
44

55
ENV GO111MODULE=on
66
ENV CGO_ENABLED=0
@@ -42,9 +42,9 @@ RUN CGO_ENABLED=${CGO_ENABLED} GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build --
4242
-X \"github.com/openfaas/faas/gateway/version.GitCommitSHA=${GIT_COMMIT}\" \
4343
-X \"github.com/openfaas/faas/gateway/version.Version=${VERSION}\" \
4444
-X github.com/openfaas/faas/gateway/types.Arch=${TARGETARCH}" \
45-
-a -installsuffix cgo -o gateway .
45+
-o gateway .
4646

47-
FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.18.3 as ship
47+
FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.19.0 as ship
4848

4949
LABEL org.label-schema.license="MIT" \
5050
org.label-schema.vcs-url="https://github.com/openfaas/faas" \

gateway/Makefile

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export DOCKER_CLI_EXPERIMENTAL=enabled
22

3-
PLATFORM := "linux/amd64,linux/arm/v7,linux/arm64"
3+
PLATFORM?="linux/amd64,linux/arm/v7,linux/arm64"
44

55
TAG?=dev
66
SERVER?=ttl.sh
@@ -19,6 +19,16 @@ buildx-local:
1919

2020
.PHONY: buildx-push
2121
buildx-push:
22+
@echo $(SERVER)/$(OWNER)/$(NAME):$(TAG) \
23+
&& docker buildx create --use --name=multiarch --node multiarch \
24+
&& docker buildx build \
25+
--progress=plain \
26+
--platform linux/amd64 \
27+
--output "type=image,push=true" \
28+
--tag $(SERVER)/$(OWNER)/$(NAME):$(TAG) .
29+
30+
.PHONY: buildx-push-all
31+
buildx-push-all:
2232
@echo $(SERVER)/$(OWNER)/$(NAME):$(TAG) \
2333
&& docker buildx create --use --name=multiarch --node multiarch \
2434
&& docker buildx build \

gateway/go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
module github.com/openfaas/faas/gateway
22

3-
go 1.20
3+
go 1.21
44

55
require (
66
github.com/docker/distribution v2.8.3+incompatible
77
github.com/gorilla/mux v1.8.0
8-
github.com/openfaas/faas-provider v0.24.4
8+
github.com/openfaas/faas-provider v0.25.2
99
github.com/openfaas/nats-queue-worker v0.0.0-20231023101743-fa54e89c9db2
1010
github.com/prometheus/client_golang v1.17.0
1111
github.com/prometheus/client_model v0.5.0

gateway/go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ github.com/openfaas/faas-provider v0.19.1 h1:xH8lTWabfDZwzIvC0u1AO48ghD3BNw6Vo23
192192
github.com/openfaas/faas-provider v0.19.1/go.mod h1:Farrp+9Med8LeK3aoYpqplMP8f5ebTILbCSLg2LPLZk=
193193
github.com/openfaas/faas-provider v0.24.4 h1:Zzbkabgd0PoQmnRjy53NbMXjhLaIyoIiwP3qaLkm9rE=
194194
github.com/openfaas/faas-provider v0.24.4/go.mod h1:NsETIfEndZn4mn/w/XnBTcDTwKqULCziphLp7KgeRcA=
195+
github.com/openfaas/faas-provider v0.25.2 h1:sAyL96CzAk/YnuXQZiRJcHo7UrcYMaf7RDvKxsQb/2o=
196+
github.com/openfaas/faas-provider v0.25.2/go.mod h1:NsETIfEndZn4mn/w/XnBTcDTwKqULCziphLp7KgeRcA=
195197
github.com/openfaas/nats-queue-worker v0.0.0-20230303171817-9dfe6fa61387 h1:D4xbdy309Wdyhlm6PgJqUV/aR77VQQG8UTF+q0ay71c=
196198
github.com/openfaas/nats-queue-worker v0.0.0-20230303171817-9dfe6fa61387/go.mod h1:s86POyW6C8S4CALFRhO8ax5sR2uaQUJQ0HaQGvbTpTc=
197199
github.com/openfaas/nats-queue-worker v0.0.0-20231023101743-fa54e89c9db2 h1:I8U2kq2h7Wl6pkd4hjRK6P0/o3AcCNdfmNJS5gdgxKU=

gateway/handlers/forwarding_proxy.go

+36-10
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ import (
99
"io"
1010
"log"
1111
"net/http"
12+
"net/http/httputil"
1213
"os"
14+
"strings"
1315
"time"
1416

17+
fhttputil "github.com/openfaas/faas-provider/httputil"
1518
"github.com/openfaas/faas/gateway/pkg/middleware"
1619
"github.com/openfaas/faas/gateway/types"
1720
)
@@ -28,7 +31,10 @@ func MakeForwardingProxyHandler(proxy *types.HTTPClientReverseProxy,
2831
writeRequestURI = exists
2932
}
3033

34+
reverseProxy := makeRewriteProxy(baseURLResolver, urlPathTransformer)
35+
3136
return func(w http.ResponseWriter, r *http.Request) {
37+
3238
baseURL := baseURLResolver.Resolve(r)
3339
originalURL := r.URL.String()
3440
requestURL := urlPathTransformer.Transform(r)
@@ -39,13 +45,13 @@ func MakeForwardingProxyHandler(proxy *types.HTTPClientReverseProxy,
3945

4046
start := time.Now()
4147

42-
statusCode, err := forwardRequest(w, r, proxy.Client, baseURL, requestURL, proxy.Timeout, writeRequestURI, serviceAuthInjector)
43-
44-
seconds := time.Since(start)
48+
statusCode, err := forwardRequest(w, r, proxy.Client, baseURL, requestURL, proxy.Timeout, writeRequestURI, serviceAuthInjector, reverseProxy)
4549
if err != nil {
4650
log.Printf("error with upstream request to: %s, %s\n", requestURL, err.Error())
4751
}
4852

53+
seconds := time.Since(start)
54+
4955
for _, notifier := range notifiers {
5056
notifier.Notify(r.Method, requestURL, originalURL, statusCode, "completed", seconds)
5157
}
@@ -86,7 +92,12 @@ func forwardRequest(w http.ResponseWriter,
8692
requestURL string,
8793
timeout time.Duration,
8894
writeRequestURI bool,
89-
serviceAuthInjector middleware.AuthInjector) (int, error) {
95+
serviceAuthInjector middleware.AuthInjector,
96+
reverseProxy *httputil.ReverseProxy) (int, error) {
97+
98+
if r.Body != nil {
99+
defer r.Body.Close()
100+
}
90101

91102
upstreamReq := buildUpstreamRequest(r, baseURL, requestURL)
92103
if upstreamReq.Body != nil {
@@ -101,14 +112,20 @@ func forwardRequest(w http.ResponseWriter,
101112
log.Printf("forwardRequest: %s %s\n", upstreamReq.Host, upstreamReq.URL.String())
102113
}
103114

115+
if strings.HasPrefix(r.Header.Get("Accept"), "text/event-stream") {
116+
ww := fhttputil.NewHttpWriteInterceptor(w)
117+
reverseProxy.ServeHTTP(ww, upstreamReq)
118+
return ww.Status(), nil
119+
}
120+
104121
ctx, cancel := context.WithTimeout(r.Context(), timeout)
105122
defer cancel()
106123

107-
res, resErr := proxyClient.Do(upstreamReq.WithContext(ctx))
108-
if resErr != nil {
124+
res, err := proxyClient.Do(upstreamReq.WithContext(ctx))
125+
if err != nil {
109126
badStatus := http.StatusBadGateway
110127
w.WriteHeader(badStatus)
111-
return badStatus, resErr
128+
return badStatus, err
112129
}
113130

114131
if res.Body != nil {
@@ -117,12 +134,10 @@ func forwardRequest(w http.ResponseWriter,
117134

118135
copyHeaders(w.Header(), &res.Header)
119136

120-
// Write status code
121137
w.WriteHeader(res.StatusCode)
122138

123139
if res.Body != nil {
124-
// Copy the body over
125-
io.CopyBuffer(w, res.Body, nil)
140+
io.Copy(w, res.Body)
126141
}
127142

128143
return res.StatusCode, nil
@@ -159,3 +174,14 @@ var hopHeaders = []string{
159174
"Transfer-Encoding",
160175
"Upgrade",
161176
}
177+
178+
func makeRewriteProxy(baseURLResolver middleware.BaseURLResolver, urlPathTransformer middleware.URLPathTransformer) *httputil.ReverseProxy {
179+
return &httputil.ReverseProxy{
180+
ErrorLog: log.New(io.Discard, "proxy:", 0),
181+
Transport: http.DefaultClient.Transport,
182+
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
183+
},
184+
Director: func(r *http.Request) {
185+
},
186+
}
187+
}

gateway/vendor/github.com/openfaas/faas-provider/httputil/write_interceptor.go

+14-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gateway/vendor/github.com/openfaas/faas-provider/types/system_events.go

+52
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gateway/vendor/modules.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ github.com/nats-io/nuid
4040
## explicit; go 1.14
4141
github.com/nats-io/stan.go
4242
github.com/nats-io/stan.go/pb
43-
# github.com/openfaas/faas-provider v0.24.4
43+
# github.com/openfaas/faas-provider v0.25.2
4444
## explicit; go 1.20
4545
github.com/openfaas/faas-provider/auth
4646
github.com/openfaas/faas-provider/httputil

0 commit comments

Comments
 (0)