Skip to content

Commit f54a861

Browse files
authored
Move control of the in-VM context lifecycle to the host specific code. (proxy-wasm#17)
* Move control of the in-VM context lifecycle to the host specific code. Signed-off-by: John Plevyak <[email protected]>
1 parent b55f5ba commit f54a861

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

WORKSPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
55

66
git_repository(
77
name = "proxy_wasm_cpp_sdk",
8-
commit = "b273b07ae0cfa9cc76dfe38236b163e9e3a2ab49",
8+
commit = "f750d1f5da6a2f20cc55da75dd9772b7ba1650ca",
99
remote = "https://github.com/proxy-wasm/proxy-wasm-cpp-sdk",
1010
)
1111

include/proxy-wasm/null_plugin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ template <typename T> using Optional = optional<T>;
4242
struct NullPluginRegistry {
4343
uint32_t (*proxy_validate_configuration_)(uint32_t root_context_id,
4444
uint32_t plugin_configuration_size) = nullptr;
45-
uint32_t (*proxy_on_context_create_)(uint32_t context_id, uint32_t parent_context_id) = nullptr;
45+
void (*proxy_on_context_create_)(uint32_t context_id, uint32_t parent_context_id) = nullptr;
4646
uint32_t (*proxy_on_vm_start_)(uint32_t root_context_id,
4747
uint32_t vm_configuration_size) = nullptr;
4848
uint32_t (*proxy_on_configure_)(uint32_t root_context_id,

src/context.cc

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ bool ContextBase::onStart(std::shared_ptr<PluginBase> plugin) {
251251
if (wasm_->on_context_create_) {
252252
plugin_ = plugin;
253253
wasm_->on_context_create_(this, id_, 0);
254+
in_vm_context_created_ = true;
254255
plugin_.reset();
255256
}
256257
if (wasm_->on_vm_start_) {
@@ -277,10 +278,14 @@ bool ContextBase::onConfigure(std::shared_ptr<PluginBase> plugin) {
277278
}
278279

279280
void ContextBase::onCreate(uint32_t parent_context_id) {
280-
if (wasm_->on_context_create_) {
281+
if (!in_vm_context_created_ && wasm_->on_context_create_) {
281282
DeferAfterCallActions actions(this);
282283
wasm_->on_context_create_(this, id_, parent_context_id);
284+
in_vm_context_created_ = true;
283285
}
286+
// NB: If no on_context_create function is registered the in-VM SDK is responsible for
287+
// managing any required in-VM state.
288+
in_vm_context_created_ = true;
284289
}
285290

286291
// Shared Data
@@ -337,11 +342,10 @@ void ContextBase::onTick(uint32_t) {
337342
}
338343

339344
FilterStatus ContextBase::onNetworkNewConnection() {
340-
DeferAfterCallActions actions(this);
341-
onCreate(root_context_id_);
342345
if (!wasm_->on_new_connection_) {
343346
return FilterStatus::Continue;
344347
}
348+
DeferAfterCallActions actions(this);
345349
if (wasm_->on_new_connection_(this, id_).u64_ == 0) {
346350
return FilterStatus::Continue;
347351
}
@@ -388,12 +392,10 @@ void ContextBase::onUpstreamConnectionClose(CloseType close_type) {
388392
template <typename P> static uint32_t headerSize(const P &p) { return p ? p->size() : 0; }
389393

390394
FilterHeadersStatus ContextBase::onRequestHeaders(uint32_t headers, bool end_of_stream) {
391-
DeferAfterCallActions actions(this);
392-
onCreate(root_context_id_);
393-
in_vm_context_created_ = true;
394395
if (!wasm_->on_request_headers_) {
395396
return FilterHeadersStatus::Continue;
396397
}
398+
DeferAfterCallActions actions(this);
397399
auto result =
398400
wasm_->on_request_headers_(this, id_, headers, static_cast<uint32_t>(end_of_stream)).u64_;
399401
if (result > static_cast<uint64_t>(FilterHeadersStatus::StopAllIterationAndWatermark))
@@ -438,18 +440,10 @@ FilterMetadataStatus ContextBase::onRequestMetadata(uint32_t elements) {
438440
}
439441

440442
FilterHeadersStatus ContextBase::onResponseHeaders(uint32_t headers, bool end_of_stream) {
441-
DeferAfterCallActions actions(this);
442-
if (!in_vm_context_created_) {
443-
// If the request is invalid then onRequestHeaders() will not be called and neither will
444-
// onCreate() then sendLocalReply be called which will call this function. In this case we
445-
// need to call onCreate() so that the Context inside the VM is created before the
446-
// onResponseHeaders() call.
447-
onCreate(root_context_id_);
448-
in_vm_context_created_ = true;
449-
}
450443
if (!wasm_->on_response_headers_) {
451444
return FilterHeadersStatus::Continue;
452445
}
446+
DeferAfterCallActions actions(this);
453447
auto result =
454448
wasm_->on_response_headers_(this, id_, headers, static_cast<uint32_t>(end_of_stream)).u64_;
455449
if (result > static_cast<uint64_t>(FilterHeadersStatus::StopAllIterationAndWatermark))
@@ -542,23 +536,23 @@ void ContextBase::onGrpcClose(uint32_t token, uint32_t status_code) {
542536
}
543537

544538
bool ContextBase::onDone() {
545-
DeferAfterCallActions actions(this);
546539
if (wasm_->on_done_) {
540+
DeferAfterCallActions actions(this);
547541
return wasm_->on_done_(this, id_).u64_ != 0;
548542
}
549543
return true;
550544
}
551545

552546
void ContextBase::onLog() {
553-
DeferAfterCallActions actions(this);
554547
if (wasm_->on_log_) {
548+
DeferAfterCallActions actions(this);
555549
wasm_->on_log_(this, id_);
556550
}
557551
}
558552

559553
void ContextBase::onDelete() {
560-
DeferAfterCallActions actions(this);
561-
if (wasm_->on_delete_) {
554+
if (in_vm_context_created_ && wasm_->on_delete_) {
555+
DeferAfterCallActions actions(this);
562556
wasm_->on_delete_(this, id_);
563557
}
564558
}

0 commit comments

Comments
 (0)