Skip to content

Commit 048131c

Browse files
authored
chore: Rework the reactor interface (#610)
1 parent 5707f20 commit 048131c

File tree

9 files changed

+49
-32
lines changed

9 files changed

+49
-32
lines changed

runtime/js-compute-runtime/fastly-world/fastly_world.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3051,14 +3051,14 @@ bool fastly_compute_at_edge_uap_parse(fastly_world_string_t *user_agent, fastly_
30513051
}
30523052
}
30533053

3054-
__attribute__((__export_name__("compute-at-edge#serve")))
3055-
int32_t __wasm_export_compute_at_edge_serve(int32_t arg, int32_t arg0) {
3056-
compute_at_edge_request_t arg1 = (compute_at_edge_request_t) {
3054+
__attribute__((__export_name__("fastly:compute-at-edge/reactor#serve")))
3055+
int32_t __wasm_export_exports_fastly_compute_at_edge_reactor_serve(int32_t arg, int32_t arg0) {
3056+
fastly_compute_at_edge_reactor_request_t arg1 = (fastly_compute_at_edge_http_types_request_t) {
30573057
(uint32_t) (arg),
30583058
(uint32_t) (arg0),
30593059
};
30603060
fastly_world_result_void_void_t ret;
3061-
ret.is_err = !compute_at_edge_serve(&arg1);
3061+
ret.is_err = !exports_fastly_compute_at_edge_reactor_serve(&arg1);
30623062
int32_t result;
30633063
if ((ret).is_err) {
30643064
result = 1;

runtime/js-compute-runtime/fastly-world/fastly_world.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -442,14 +442,7 @@ typedef struct {
442442
fastly_compute_at_edge_cache_handle_t f1;
443443
} fastly_world_tuple2_fastly_compute_at_edge_cache_body_handle_fastly_compute_at_edge_cache_handle_t;
444444

445-
typedef uint32_t compute_at_edge_request_handle_t;
446-
447-
typedef uint32_t compute_at_edge_body_handle_t;
448-
449-
typedef struct {
450-
compute_at_edge_request_handle_t f0;
451-
compute_at_edge_body_handle_t f1;
452-
} compute_at_edge_request_t;
445+
typedef fastly_compute_at_edge_http_types_request_t fastly_compute_at_edge_reactor_request_t;
453446

454447
// Imported Functions from `fastly:compute-at-edge/async-io`
455448
// Blocks until one of the given objects is ready for I/O, or the optional timeout expires.
@@ -610,8 +603,8 @@ bool fastly_compute_at_edge_secret_store_plaintext(fastly_compute_at_edge_secret
610603
// Imported Functions from `fastly:compute-at-edge/uap`
611604
bool fastly_compute_at_edge_uap_parse(fastly_world_string_t *user_agent, fastly_compute_at_edge_uap_user_agent_t *ret, fastly_compute_at_edge_uap_error_t *err);
612605

613-
// Exported Functions from `compute-at-edge`
614-
bool compute_at_edge_serve(compute_at_edge_request_t *req);
606+
// Exported Functions from `fastly:compute-at-edge/reactor`
607+
bool exports_fastly_compute_at_edge_reactor_serve(fastly_compute_at_edge_reactor_request_t *req);
615608

616609
#ifdef __cplusplus
617610
}
Binary file not shown.

runtime/js-compute-runtime/fastly.wit

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,16 @@ interface cache {
667667
get-body: func(handle: handle, options: get-body-options) -> result<body-handle, error>
668668
}
669669

670+
interface reactor {
671+
use http-types.{request}
672+
673+
/// Serve the given request
674+
///
675+
/// response handle not currently returned, because in the case of a streamed response
676+
/// send downstream must be fully streamed due to the run to completion semantics.
677+
serve: func(req: request) -> result
678+
}
679+
670680
world fastly-world {
671681

672682
import async-io
@@ -682,15 +692,5 @@ world fastly-world {
682692
import secret-store
683693
import uap
684694

685-
export compute-at-edge: interface {
686-
type request-handle = u32
687-
type body-handle = u32
688-
type request = tuple<request-handle, body-handle>
689-
690-
/// Serve the given request
691-
///
692-
/// response handle not currently returned, because in the case of a streamed response
693-
/// send downstream must be fully streamed due to the run to completion semantics.
694-
serve: func(req: request) -> result
695-
}
695+
export reactor
696696
}

runtime/js-compute-runtime/host_interface/host_api.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ struct CacheOverrideTag final {
330330
void set_pci();
331331
};
332332

333+
struct Request;
334+
333335
class HttpReq final : public HttpBase {
334336
public:
335337
using Handle = uint32_t;
@@ -348,6 +350,9 @@ class HttpReq final : public HttpBase {
348350
static Result<Void> register_dynamic_backend(std::string_view name, std::string_view target,
349351
const BackendConfig &config);
350352

353+
/// Fetch the downstream request/body pair
354+
static Result<Request> downstream_get();
355+
351356
/// Get the downstream ip address.
352357
static Result<HostBytes> downstream_client_ip_addr();
353358

@@ -442,6 +447,15 @@ struct Response {
442447
Response(HttpResp resp, HttpBody body) : resp{resp}, body{body} {}
443448
};
444449

450+
/// The pair of a request and its body.
451+
struct Request {
452+
HttpReq req;
453+
HttpBody body;
454+
455+
Request() = default;
456+
Request(HttpReq req, HttpBody body) : req{req}, body{body} {}
457+
};
458+
445459
class GeoIp final {
446460
~GeoIp() = delete;
447461

runtime/js-compute-runtime/impl/main.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#include "host_interface/fastly.h"
2+
#include "host_interface/host_api.h"
3+
#include "js-compute-builtins.h"
24

35
int main(int argc, const char *argv[]) {
4-
fastly_compute_at_edge_http_types_request_t req;
5-
if (fastly::req_body_downstream_get(&req.f0, &req.f1) != 0) {
6+
host_api::Request req;
7+
8+
if (fastly::req_body_downstream_get(&req.req.handle, &req.body.handle) != 0) {
69
abort();
710
return 1;
811
}
912

10-
compute_at_edge_serve(static_cast<compute_at_edge_request_t *>(static_cast<void *>(&req)));
13+
reactor_main(req);
1114

1215
// Note: we deliberately skip shutdown, because it takes quite a while,
1316
// and serves no purpose for us.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
#include "host_interface/fastly.h"
2+
#include "host_interface/host_api.h"
3+
#include "js-compute-builtins.h"
24

35
int main() { return 0; }
6+
7+
bool exports_fastly_compute_at_edge_reactor_serve(fastly_compute_at_edge_reactor_request_t *req) {
8+
host_api::Request request{host_api::HttpReq{req->f0}, host_api::HttpBody{req->f1}};
9+
return reactor_main(request);
10+
}

runtime/js-compute-runtime/js-compute-builtins.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,6 @@ void dump_promise_rejection(JSContext *cx, JS::HandleValue reason, JS::HandleObj
113113
bool print_stack(JSContext *cx, FILE *fp);
114114
bool print_stack(JSContext *cx, JS::HandleObject stack, FILE *fp);
115115

116+
bool reactor_main(host_api::Request req);
117+
116118
#endif // fastly_sys_h

runtime/js-compute-runtime/js-compute-runtime.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "core/allocator.h"
2424
#include "core/encode.h"
2525
#include "core/event_loop.h"
26-
#include "host_interface/fastly.h"
2726
#include "host_interface/host_api.h"
2827
#include "js-compute-builtins.h"
2928
#include "third_party/wizer.h"
@@ -480,7 +479,7 @@ static void wait_for_backends(JSContext *cx, double *total_compute) {
480479
printf("Done, waited for %fms\n", diff / 1000);
481480
}
482481

483-
bool compute_at_edge_serve(compute_at_edge_request_t *req) {
482+
bool reactor_main(host_api::Request req) {
484483
assert(hasWizeningFinished());
485484

486485
builtins::Performance::timeOrigin.emplace(std::chrono::high_resolution_clock::now());
@@ -501,8 +500,7 @@ bool compute_at_edge_serve(compute_at_edge_request_t *req) {
501500
js::ResetMathRandomSeed(cx);
502501

503502
HandleObject fetch_event = builtins::FetchEvent::instance();
504-
builtins::FetchEvent::init_request(cx, fetch_event, host_api::HttpReq{req->f0},
505-
host_api::HttpBody{req->f1});
503+
builtins::FetchEvent::init_request(cx, fetch_event, req.req, req.body);
506504

507505
dispatch_fetch_event(cx, fetch_event, &total_compute);
508506

0 commit comments

Comments
 (0)