Skip to content

Commit 47d01b0

Browse files
authored
add SandboxRequests to fastlyMeta (#215)
1 parent 9f38fd1 commit 47d01b0

File tree

5 files changed

+35
-13
lines changed

5 files changed

+35
-13
lines changed

_examples/reusable-sandbox/main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ import (
1111
)
1212

1313
func main() {
14-
var requestCount int
1514
fsthttp.ServeMany(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
16-
requestCount++
1715
meta, err := r.FastlyMeta()
1816
if err != nil {
1917
fsthttp.Error(w, err.Error(), fsthttp.StatusInternalServerError)
2018
return
2119
}
22-
fmt.Fprintf(w, "Request %v, Hello, %s (sandbox: %q, request: %q)!\n", requestCount, r.RemoteAddr, meta.SandboxID, meta.RequestID)
20+
fmt.Fprintf(w, "Request %v, Hello, %s (sandbox: %q, request: %q)!\n", meta.SandboxRequests, r.RemoteAddr, meta.SandboxID, meta.RequestID)
2321
}, &fsthttp.ServeManyOptions{
2422
NextTimeout: 1 * time.Second,
2523
MaxRequests: 100,

end_to_end_tests/serve-many/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func main() {
3535
w.Header().Set("Content-Type", "text/plain")
3636
w.Header().Set("Sandbox-ID", sandboxID)
3737
w.Header().Set("Request-ID", requestID)
38+
w.Header().Set("Sandbox-Requests", fmt.Sprintf("%d", meta.SandboxRequests))
3839
w.Write([]byte("OK"))
3940
}
4041

end_to_end_tests/serve-many/main_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ func TestSandboxReuse(t *testing.T) {
2222
defer resp.Body.Close()
2323

2424
sandboxID, requestID := resp.Header.Get("Sandbox-ID"), resp.Header.Get("Request-ID")
25+
sandboxRequests := resp.Header.Get("Sandbox-Requests")
2526

2627
if sandboxID == "" || requestID == "" {
2728
t.Fatalf("Sandbox-ID and/or Request-ID are empty: %s, %s", sandboxID, requestID)
2829
}
2930
if sandboxID != requestID {
3031
t.Errorf("sandboxID = %s, requestID = %s; expected them to match", sandboxID, requestID)
3132
}
33+
if sandboxRequests != "1" {
34+
t.Errorf("sandboxRequests = %s; expected 1", sandboxRequests)
35+
}
3236
prevSandboxID := sandboxID
3337

3438
// Second request. This should reuse the sandbox, so the sandbox ID
@@ -49,13 +53,17 @@ func TestSandboxReuse(t *testing.T) {
4953
defer resp.Body.Close()
5054

5155
sandboxID, requestID = resp.Header.Get("Sandbox-ID"), resp.Header.Get("Request-ID")
56+
sandboxRequests = resp.Header.Get("Sandbox-Requests")
5257

5358
if sandboxID != prevSandboxID {
5459
t.Errorf("sandboxID = %s, previous sandboxID = %s; expected them to match", sandboxID, sandboxID)
5560
}
5661
if sandboxID == requestID {
5762
t.Errorf("sandboxID = %s, requestID = %s; expected them to differ", sandboxID, requestID)
5863
}
64+
if sandboxRequests != "2" {
65+
t.Errorf("sandboxRequests = %s; expected 2", sandboxRequests)
66+
}
5967
prevSandboxID = sandboxID
6068

6169
// Third request, we should have a new sandbox ID and it should match the request ID
@@ -70,11 +78,15 @@ func TestSandboxReuse(t *testing.T) {
7078
defer resp.Body.Close()
7179

7280
sandboxID, requestID = resp.Header.Get("Sandbox-ID"), resp.Header.Get("Request-ID")
81+
sandboxRequests = resp.Header.Get("Sandbox-Requests")
7382

7483
if sandboxID == prevSandboxID {
7584
t.Errorf("sandboxID = %s, previous sandboxID = %s; expected them to differ", sandboxID, sandboxID)
7685
}
7786
if sandboxID != requestID {
7887
t.Errorf("sandboxID = %s, requestID = %s; expected them to match", sandboxID, requestID)
7988
}
89+
if sandboxRequests != "1" {
90+
t.Errorf("sandboxRequests = %s; expected 1", sandboxRequests)
91+
}
8092
}

fsthttp/handle.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,22 @@ func Serve(h Handler) {
2121
panic(fmt.Errorf("get client handles: %w", err))
2222
}
2323

24-
serve(h, abireq, abibody)
24+
serve(h, abireq, abibody, 1)
2525

2626
// wait for any stale-while-revalidate goroutines to complete.
2727
guestCacheSWRPending.Wait()
2828
}
2929

30-
func serve(h Handler, abireq *fastly.HTTPRequest, abibody *fastly.HTTPBody) {
30+
func serve(h Handler, abireq *fastly.HTTPRequest, abibody *fastly.HTTPBody, sandboxRequests int) {
3131
ctx, cancel := context.WithCancel(context.Background())
3232
defer cancel()
3333

3434
clientRequest, err := newClientRequest(abireq, abibody)
3535
if err != nil {
3636
panic(fmt.Errorf("create client Request: %w", err))
3737
}
38+
clientRequest.sandboxRequests = sandboxRequests
39+
3840
clientResponseWriter, err := newResponseWriter()
3941
if err != nil {
4042
panic(fmt.Errorf("create client ResponseWriter: %w", err))
@@ -65,18 +67,18 @@ type ServeManyOptions struct {
6567
// ServeMany allows a single Compute instance to handle multiple requests.
6668
func ServeMany(h HandlerFunc, serveOpts *ServeManyOptions) {
6769
start := time.Now()
70+
requestCount := 1
6871

6972
abireq, abibody, err := fastly.BodyDownstreamGet()
7073
if err != nil {
7174
panic(fmt.Errorf("get client handles: %w", err))
7275
}
73-
serve(h, abireq, abibody)
76+
serve(h, abireq, abibody, requestCount)
7477

7578
// Serve the rest
76-
var requests int
7779
for {
78-
requests++
79-
if serveOpts.MaxRequests != 0 && requests >= serveOpts.MaxRequests {
80+
requestCount++
81+
if serveOpts.MaxRequests != 0 && requestCount > serveOpts.MaxRequests {
8082
break
8183
}
8284

@@ -106,7 +108,7 @@ func ServeMany(h HandlerFunc, serveOpts *ServeManyOptions) {
106108
panic(fmt.Errorf("get client handles: %w", err))
107109
}
108110

109-
serve(h, abireq, abibody)
111+
serve(h, abireq, abibody, requestCount)
110112
}
111113

112114
// wait for any stale-while-revalidate goroutines to complete.

fsthttp/request.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ type Request struct {
121121

122122
abi reqAbi
123123
downstream reqAbi
124+
125+
sandboxRequests int
124126
}
125127

126128
type reqAbi struct {
@@ -363,9 +365,11 @@ func (req *Request) FastlyMeta() (*FastlyMeta, error) {
363365
}
364366

365367
var err error
366-
var fastlyMeta FastlyMeta
367368

368-
fastlyMeta.SandboxID = os.Getenv("FASTLY_TRACE_ID")
369+
fastlyMeta := &FastlyMeta{
370+
SandboxID: os.Getenv("FASTLY_TRACE_ID"),
371+
SandboxRequests: req.sandboxRequests,
372+
}
369373

370374
fastlyMeta.RequestID, err = req.downstream.req.DownstreamRequestID()
371375
if err != nil {
@@ -392,7 +396,7 @@ func (req *Request) FastlyMeta() (*FastlyMeta, error) {
392396
return nil, fmt.Errorf("get fastly key is valid: %w", err)
393397
}
394398

395-
req.fastlyMeta = &fastlyMeta
399+
req.fastlyMeta = fastlyMeta
396400

397401
return req.fastlyMeta, nil
398402
}
@@ -1093,6 +1097,11 @@ type FastlyMeta struct {
10931097
// FastlyKeyIsValid is true if the request contains a valid Fastly API token.
10941098
// This is for services to restrict authenticating PURGE requests for the readthrough cache.
10951099
FastlyKeyIsValid bool
1100+
1101+
// SandboxRequests is the number of requests handled by the sandbox so far.
1102+
// For example, if this is were 3, it means that this is the 3rd request handled by the sandbox.
1103+
// This will be zero if this is not a client request.
1104+
SandboxRequests int
10961105
}
10971106

10981107
// DecompressResponseOptions control the auto decompress response behaviour.

0 commit comments

Comments
 (0)