Skip to content

Commit 0b39b3c

Browse files
authored
Merge pull request #168 from fastly/dgryski/httpcache-swr-wait
fsthttp: ensure swr goroutines have completed before exiting
2 parents 5014f26 + 2f77f3d commit 0b39b3c

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

fsthttp/handle.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ func Serve(h Handler) {
3737

3838
h.ServeHTTP(ctx, clientResponseWriter, clientRequest)
3939
clientResponseWriter.Close()
40+
// wait for any stale-while-revalidate goroutines to complete.
41+
guestCacheSWRPending.Wait()
4042
}
4143

4244
// ServeFunc is sugar for Serve(HandlerFunc(f)).

fsthttp/request.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
"net/url"
1212
"strings"
13+
"sync"
1314
"time"
1415

1516
"github.com/fastly/compute-sdk-go/internal/abi/fastly"
@@ -489,6 +490,8 @@ func pendingToABIResponse(ctx context.Context, errc chan error, abiPending *fast
489490
}
490491
}
491492

493+
var guestCacheSWRPending sync.WaitGroup
494+
492495
func (req *Request) sendWithGuestCache(ctx context.Context, backend string) (*Response, error) {
493496
// use guest cache
494497

@@ -550,7 +553,9 @@ func (req *Request) sendWithGuestCache(ctx context.Context, backend string) (*Re
550553
}
551554

552555
// Wait for the pending respond, then call any after-end hooks
556+
guestCacheSWRPending.Add(1)
553557
go func(p *pendingBackendRequestForCaching, h *fastly.HTTPCacheHandle) {
558+
defer guestCacheSWRPending.Done()
554559
candidate, err := newCandidateFromPendingBackendCaching(p)
555560
if err != nil {
556561
// nowhere to log error

internal/abi/fastly/httpcache_guest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ func HTTPCacheTransactionInsert(h *HTTPCacheHandle, resp *HTTPResponse, opts *HT
334334
return nil, err
335335
}
336336

337-
return &HTTPBody{h: body}, nil
337+
return &HTTPBody{h: body, closable: true}, nil
338338
}
339339

340340
// witx:

0 commit comments

Comments
 (0)