Skip to content

Commit 689ef46

Browse files
LucasRoesleralexellis
authored andcommitted
Update to of-watchdog 0.7.7 and the docs
**What** - Update to the latest of-watchdog 0.7.7, ensureing the request context is propagated correctly - Add examples of how to use the context to abort requests early Signed-off-by: Lucas Roesler <[email protected]>
1 parent a762b29 commit 689ef46

File tree

5 files changed

+49
-8
lines changed

5 files changed

+49
-8
lines changed

README.md

+45-4
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,42 @@ func Handle(req handler.Request) (handler.Response, error) {
144144
}
145145
```
146146

147+
Example responding to an aborted request.
148+
149+
The `Request` object provides access to the request context. This allows you to check if the request has been cancelled by using the context's done channel `req.Context().Done()` or the context's error `req.Context().Err()`
150+
151+
```go
152+
package function
153+
154+
import (
155+
"fmt"
156+
"net/http"
157+
158+
handler "github.com/openfaas-incubator/go-function-sdk"
159+
)
160+
161+
// Handle a function invocation
162+
func Handle(req handler.Request) (handler.Response, error) {
163+
var err error
164+
165+
for i := 0; i < 10000; i++ {
166+
if req.Context().Err() != nil {
167+
return handler.Response{}, fmt.Errorf("request cancelled")
168+
}
169+
fmt.Printf("count %d\n", i)
170+
}
171+
172+
message := fmt.Sprintf("Hello world, input was: %s", string(req.Body))
173+
return handler.Response{
174+
Body: []byte(message),
175+
StatusCode: http.StatusOK,
176+
}, err
177+
}
178+
```
179+
180+
This context can also be passed to other methods so that they can respond to the cancellation as well, for example [`db.ExecContext(req.Context())`](https://golang.org/pkg/database/sql/#DB.ExecContext)
181+
182+
147183
## 2.0 golang-middleware
148184

149185
This template uses the [http.HandlerFunc](https://golang.org/pkg/net/http/#HandlerFunc) as entry point.
@@ -254,6 +290,7 @@ func init() {
254290

255291
func Handle(w http.ResponseWriter, r *http.Request) {
256292
var query string
293+
ctx := r.Context()
257294

258295
if r.Body != nil {
259296
defer r.Body.Close()
@@ -272,15 +309,19 @@ func Handle(w http.ResponseWriter, r *http.Request) {
272309
// log to stdout
273310
fmt.Printf("Executing query: %s", query)
274311

275-
rows, err := db.Query(query)
312+
rows, err := db.QueryContext(ctx, query)
276313
if err != nil {
277314
http.Error(w, err.Error(), http.StatusInternalServerError)
278315
return
279316
}
280317
defer rows.Close()
281-
318+
282319
ids := make([]string, 0)
283320
for rows.Next() {
321+
if e := ctx.Err(); e != nil {
322+
http.Error(w, e, http.StatusBadRequest)
323+
return
324+
}
284325
var id int
285326
if err := rows.Scan(&id); err != nil {
286327
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -292,7 +333,7 @@ func Handle(w http.ResponseWriter, r *http.Request) {
292333
http.Error(w, err.Error(), http.StatusInternalServerError)
293334
return
294335
}
295-
336+
296337
result := fmt.Sprintf("ids %s", strings.Join(ids, ", "))
297338

298339
// write result
@@ -311,7 +352,7 @@ import (
311352
)
312353
func Handle(w http.ResponseWriter, r *http.Request) {
313354
// Parses RawQuery and returns the corresponding
314-
// values as a map[string][]string
355+
// values as a map[string][]string
315356
// for more info https://golang.org/pkg/net/url/#URL.Query
316357
query := r.URL.Query()
317358
w.WriteHeader(http.StatusOK)

template/golang-http-armhf/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM openfaas/of-watchdog:0.7.6 as watchdog
1+
FROM openfaas/of-watchdog:0.7.7 as watchdog
22
FROM golang:1.13-alpine3.11 as build
33

44
RUN apk --no-cache add git

template/golang-http/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM openfaas/of-watchdog:0.7.6 as watchdog
1+
FROM openfaas/of-watchdog:0.7.7 as watchdog
22
FROM golang:1.13-alpine3.11 as build
33

44
RUN apk --no-cache add git

template/golang-middleware-armhf/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM openfaas/of-watchdog:0.7.6 as watchdog
1+
FROM openfaas/of-watchdog:0.7.7 as watchdog
22
FROM golang:1.13-alpine3.11 as build
33

44
RUN apk --no-cache add git

template/golang-middleware/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM openfaas/of-watchdog:0.7.6 as watchdog
1+
FROM openfaas/of-watchdog:0.7.7 as watchdog
22
FROM golang:1.13-alpine3.11 as build
33

44
RUN apk --no-cache add git

0 commit comments

Comments
 (0)