Skip to content

Commit 72863f5

Browse files
authored
Browser: Return 408 status code on readiness timeout (#862)
1 parent b941a17 commit 72863f5

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

pkg/api/render.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ func HandleGetRender(browser *service.BrowserService) http.Handler {
237237
span.SetStatus(codes.Error, "rendering failed")
238238
span.RecordError(err)
239239
if errors.Is(err, context.DeadlineExceeded) ||
240-
errors.Is(err, context.Canceled) {
240+
errors.Is(err, context.Canceled) ||
241+
errors.Is(err, service.ErrBrowserReadinessTimeout) {
241242
http.Error(w, "Request timed out", http.StatusRequestTimeout)
242243
return
243244
} else if errors.Is(err, service.ErrInvalidBrowserOption) {

pkg/service/browser.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ var (
7777
}, []string{"mime_type"})
7878
)
7979

80-
var ErrInvalidBrowserOption = errors.New("invalid browser option")
80+
var (
81+
ErrInvalidBrowserOption = errors.New("invalid browser option")
82+
ErrBrowserReadinessTimeout = errors.New("timed out waiting for readiness")
83+
)
8184

8285
type BrowserService struct {
8386
cfg config.BrowserConfig
@@ -1001,8 +1004,8 @@ func waitForReady(browserCtx context.Context, cfg config.BrowserConfig) chromedp
10011004
span.SetStatus(codes.Error, "context completed before readiness detected")
10021005
return ctx.Err()
10031006
case <-readinessTimeout:
1004-
span.SetStatus(codes.Error, "timed out waiting for readiness")
1005-
return fmt.Errorf("timed out waiting for readiness")
1007+
span.SetStatus(codes.Error, ErrBrowserReadinessTimeout.Error())
1008+
return ErrBrowserReadinessTimeout
10061009

10071010
case <-time.After(cfg.ReadinessIterationInterval):
10081011
// Continue with the rest of the code; this is waiting for the next time we can do work.

tests/acceptance/expected_failures_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,23 @@ func TestInvalidQueryParameters(t *testing.T) {
9898
})
9999
}
100100
}
101+
102+
func TestBrowserTimeout(t *testing.T) {
103+
t.Parallel()
104+
105+
t.Run("if the browser readiness timeout is exceeded, it returns a 408 status code", func(t *testing.T) {
106+
t.Parallel()
107+
108+
// extremely low that no request could ever be server in the alloted time
109+
svc := StartImageRenderer(t, WithEnv("BROWSER_READINESS_TIMEOUT", "1ns"))
110+
111+
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, svc.HTTPEndpoint+"/render?url=http://localhost:8081/&encoding=pdf", nil)
112+
require.NoError(t, err, "could not construct HTTP request to /render")
113+
req.Header.Set("Accept", "application/pdf")
114+
req.Header.Set("X-Auth-Token", "-")
115+
116+
resp, err := http.DefaultClient.Do(req)
117+
require.NoError(t, err, "could not make HTTP request to /render")
118+
require.Equal(t, http.StatusRequestTimeout, resp.StatusCode, "expected HTTP 408 Request Timeout from /render")
119+
})
120+
}

0 commit comments

Comments
 (0)