Skip to content

Commit b345cf5

Browse files
committed
cache canary results
Signed-off-by: Kuat Yessenov <[email protected]>
1 parent 491915a commit b345cf5

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

include/proxy-wasm/wasm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ class WasmHandleBase : public std::enable_shared_from_this<WasmHandleBase> {
337337

338338
protected:
339339
std::shared_ptr<WasmBase> wasm_base_;
340+
std::unordered_map<std::string, bool> plugin_canary_cache_;
340341
};
341342

342343
std::string makeVmKey(std::string_view vm_id, std::string_view configuration,

src/wasm.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,11 @@ bool WasmHandleBase::canary(const std::shared_ptr<PluginBase> &plugin,
472472
if (this->wasm() == nullptr) {
473473
return false;
474474
}
475+
auto it = plugin_canary_cache_.find(plugin->key());
476+
if (it != plugin_canary_cache_.end()) {
477+
return it->second;
478+
}
479+
std::cout << "CANARY TEST " << plugin->key() << std::endl;
475480
auto configuration_canary_handle = clone_factory(shared_from_this());
476481
if (!configuration_canary_handle) {
477482
this->wasm()->fail(FailState::UnableToCloneVm, "Failed to clone Base Wasm");
@@ -490,9 +495,11 @@ bool WasmHandleBase::canary(const std::shared_ptr<PluginBase> &plugin,
490495
if (!configuration_canary_handle->wasm()->configure(root_context, plugin)) {
491496
configuration_canary_handle->wasm()->fail(FailState::ConfigureFailed,
492497
"Failed to configure base Wasm plugin");
498+
plugin_canary_cache_[plugin->key()] = false;
493499
return false;
494500
}
495501
configuration_canary_handle->kill();
502+
plugin_canary_cache_[plugin->key()] = true;
496503
return true;
497504
}
498505

test/wasm_test.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,30 @@ TEST_P(TestVm, AlwaysApplyCanary) {
233233
}
234234
}
235235
}
236+
237+
{
238+
// Validate that canarying is cached
239+
canary_count = 0;
240+
TestContext::resetGlobalLog();
241+
const auto &root_id = root_ids[0];
242+
const auto &vm_id = vm_ids[0];
243+
const auto &vm_config = vm_configs[0];
244+
const auto &plugin_key = plugin_keys[0];
245+
const auto &plugin_config = plugin_configs[0];
246+
WasmHandleFactory wasm_handle_factory_comp =
247+
[this, vm_id, vm_config](std::string_view vm_key) -> std::shared_ptr<WasmHandleBase> {
248+
auto base_wasm = std::make_shared<TestWasm>(
249+
newVm(), std::unordered_map<std::string, std::string>(), vm_id, vm_config, vm_key);
250+
return std::make_shared<WasmHandleBase>(base_wasm);
251+
};
252+
const auto plugin_comp = std::make_shared<PluginBase>(plugin_name, root_id, vm_id, engine_,
253+
plugin_config, fail_open, plugin_key);
254+
const auto vm_key = makeVmKey(vm_id, vm_config, "common_code");
255+
// Create a base Wasm by createWasm.
256+
auto wasm_handle_comp = createWasm(vm_key, source, plugin_comp, wasm_handle_factory_comp,
257+
wasm_handle_clone_factory_for_canary, false);
258+
EXPECT_EQ(canary_count, 0);
259+
}
236260
}
237261

238262
} // namespace proxy_wasm

0 commit comments

Comments
 (0)