Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not just make two requests to the same endpoint?

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// <reference path="../../../../../types/index.d.ts" />
import { Backend } from 'fastly:backend';
import { assert } from './assertions.js';
import { isRunningLocally, routes } from './routes.js';

routes.set('/backend/ephemeral1', async () => {
if (isRunningLocally()) {
return;
}
assert(!Backend.exists('ephemeral'));
new Backend({
name: 'ephemeral',
target: 'http-me.fastly.dev',
hostOverride: 'http-me.fastly.dev',
useSSL: true,
});
assert(Backend.exists('ephemeral'));
});

routes.set('/backend/ephemeral2', async () => {
if (isRunningLocally()) {
return;
}
assert(!Backend.exists('ephemeral'));
new Backend({
name: 'ephemeral',
target: 'http-me.fastly.dev',
hostOverride: 'http-me.fastly.dev',
useSSL: true,
});
assert(Backend.exists('ephemeral'));
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { setReusableSandboxOptions } from 'fastly:experimental';

setReusableSandboxOptions({ maxRequests: 9001 });

import './dynamic-backend.js';
import './interleave.js';

addEventListener('fetch', (event) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,18 @@
"QuuxName": "QuuxValue"
}
}
},

"session #3, request #1: GET /backend/ephemeral1": {
"downstream_request": {
"method": "GET",
"pathname": "/backend/ephemeral1"
}
},
"session #3, request #2: GET /backend/ephemeral2": {
"downstream_request": {
"method": "GET",
"pathname": "/backend/ephemeral2"
}
}
}
15 changes: 15 additions & 0 deletions runtime/fastly/builtins/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1808,6 +1808,21 @@ void Backend::finalize(JS::GCContext *gcx, JSObject *obj) {
free(backend);
}

bool Backend::restore_global_state(JSContext *cx) {
JS::Rooted<JS::IdVector> props(cx, cx);
if (!JS_Enumerate(cx, Backend::backends, &props)) {
return false;
}
JS::RootedValue backend(cx);
for (uint32_t i = 0, len = props.length(); i < len; i++) {
if (!JS_GetPropertyById(cx, Backend::backends, props[i], &backend)) {
return false;
}
JS_DeletePropertyById(cx, Backend::backends, props[i]);
}
return true;
}

bool set_default_backend_config(JSContext *cx, unsigned argc, JS::Value *vp) {
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
if (!args.requireAtLeast(cx, "setDefaultDynamicBackendConfiguration", 1)) {
Expand Down
3 changes: 2 additions & 1 deletion runtime/fastly/builtins/backend.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#ifndef FASTLY_BACKEND_H
#define FASTLY_BACKEND_H

#include "../host-api/host_api_fastly.h"
#include "builtin.h"
#include "extension-api.h"

namespace fastly::backend {

class Backend : public builtins::FinalizableBuiltinImpl<Backend> {
private:
public:
static constexpr const char *class_name = "Backend";
static const int ctor_length = 1;
Expand All @@ -23,6 +23,7 @@ class Backend : public builtins::FinalizableBuiltinImpl<Backend> {

static JSString *name(JSContext *cx, JSObject *self);
static JSObject *create(JSContext *cx, JS::HandleObject request);
static bool restore_global_state(JSContext *cx);

// static methods
static bool exists(JSContext *cx, unsigned argc, JS::Value *vp);
Expand Down
8 changes: 8 additions & 0 deletions runtime/fastly/builtins/fastly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,14 @@ const JSPropertySpec Fastly::properties[] = {
#endif
JS_PS_END};

bool Fastly::restore_builtin_state(JSContext *cx) {
Fastly::baseURL.reset();
Fastly::defaultBackend.reset();
Fastly::baseURL.init(cx);
Fastly::defaultBackend.init(cx);
return true;
}

bool install(api::Engine *engine) {
ENGINE = engine;

Expand Down
1 change: 1 addition & 0 deletions runtime/fastly/builtins/fastly.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class Fastly : public builtins::BuiltinNoConstructor<Fastly> {
static bool allowDynamicBackends_set(JSContext *cx, unsigned argc, JS::Value *vp);
static bool inspect(JSContext *cx, unsigned argc, JS::Value *vp);
static bool setReusableSandboxOptions(JSContext *cx, unsigned argc, JS::Value *vp);
static bool restore_builtin_state(JSContext *cx);
};

JS::Result<std::tuple<JS::UniqueChars, size_t>> convertBodyInit(JSContext *cx,
Expand Down
26 changes: 26 additions & 0 deletions runtime/fastly/handler.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../StarlingMonkey/builtins/web/performance.h"
#include "./builtins/backend.h"
#include "./builtins/fastly.h"
#include "./builtins/fetch-event.h"
#include "./host-api/fastly.h"
Expand All @@ -17,12 +18,32 @@ namespace fastly::runtime {

api::Engine *ENGINE;

int restore_builtin_state() {
Comment thread
zkat marked this conversation as resolved.
JSContext *cx(ENGINE->cx());
if (!::fastly::backend::Backend::restore_global_state(cx)) {
if (ENGINE->debug_logging_enabled()) {
fprintf(stderr,
"Warning: Failed to restore Backend state processing next request. Exiting.\n");
}
return false;
}
if (!fastly::Fastly::restore_builtin_state(cx)) {
if (ENGINE->debug_logging_enabled()) {
fprintf(stderr,
"Warning: Failed to restore Backend state processing next request. Exiting.\n");
}
return false;
}
return true;
}

// Install corresponds to Wizer time, so we configure the engine here
bool install(api::Engine *engine) {
#if defined(JS_GC_ZEAL) && defined(FASTLY_GC_FREQUENCY)
JS::SetGCZeal(engine->cx(), 2, FASTLY_GC_FREQUENCY);
#endif
ENGINE = engine;

return true;
}

Expand Down Expand Up @@ -100,6 +121,11 @@ bool handle_incoming(host_api::Request req) {
printf("Done. Total request processing time: %fms. Total compute time: %fms\n", diff / 1000,
total_compute / 1000);
}

if (!restore_builtin_state()) {
return false;
}

return true;
}

Expand Down
Loading