Skip to content

Commit

Permalink
Merge pull request envoyproxy#49 from bianpengyuan/wasm-ticker
Browse files Browse the repository at this point in the history
Make timer work for wasm module
  • Loading branch information
mandarjog committed Apr 10, 2019
2 parents 0e61789 + 9e16764 commit 14a36c2
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 6 deletions.
2 changes: 2 additions & 0 deletions api/wasm/cpp/proxy_wasm_intrinsics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ extern "C" EMSCRIPTEN_KEEPALIVE void proxy_onCreate(uint32_t context_id) {
ensureContext(context_id)->onCreate();
}

extern "C" EMSCRIPTEN_KEEPALIVE void proxy_onTick() { ensureContext(0)->onTick(); }

extern "C" EMSCRIPTEN_KEEPALIVE FilterHeadersStatus proxy_onRequestHeaders(uint32_t context_id) {
auto c = getContext(context_id);
if (!c)
Expand Down
1 change: 1 addition & 0 deletions api/wasm/cpp/proxy_wasm_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ class Context {
virtual void onDone() {}
virtual void onLog() {}
virtual void onDelete() {}
virtual void onTick() {}

virtual void onHttpCallResponse(uint32_t token, std::unique_ptr<WasmData> header_pairs,
std::unique_ptr<WasmData> body,
Expand Down
1 change: 1 addition & 0 deletions api/wasm/cpp/proxy_wasm_intrinsics.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mergeInto(LibraryManager.library, {
proxy_log: function () {},
proxy_setTickPeriodMilliseconds: function () {},
proxy_getRequestMetadata: function () {},
proxy_setRequestMetadata: function () {},
proxy_getRequestMetadataPairs: function () {},
Expand Down
8 changes: 4 additions & 4 deletions source/extensions/common/wasm/wasm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1054,9 +1054,9 @@ void Wasm::getFunctions() {
}
}

Wasm::Wasm(const Wasm& wasm)
Wasm::Wasm(const Wasm& wasm, Event::Dispatcher& dispatcher)
: std::enable_shared_from_this<Wasm>(wasm), cluster_manager_(wasm.cluster_manager_),
dispatcher_(wasm.dispatcher_) {
dispatcher_(dispatcher) {
wasm_vm_ = wasm.wasmVm()->clone();
general_context_ = createContext();
getFunctions();
Expand Down Expand Up @@ -1107,7 +1107,7 @@ void Wasm::start() { general_context_->onStart(); }
void Wasm::setTickPeriod(std::chrono::milliseconds tick_period) {
bool was_running = timer_ && tick_period_.count() > 0;
tick_period_ = tick_period;
if (tick_ && tick_period_.count() > 0 && !was_running) {
if (tick_period_.count() > 0 && !was_running) {
timer_ = dispatcher_.createTimer([weak = std::weak_ptr<Wasm>(shared_from_this())]() {
auto shared = weak.lock();
if (shared)
Expand Down Expand Up @@ -1301,7 +1301,7 @@ std::shared_ptr<Wasm> createThreadLocalWasm(Wasm& base_wasm, absl::string_view c
Event::Dispatcher& dispatcher) {
std::shared_ptr<Wasm> wasm;
if (base_wasm.wasmVm()->clonable()) {
wasm = std::make_shared<Wasm>(base_wasm);
wasm = std::make_shared<Wasm>(base_wasm, dispatcher);
} else {
wasm = std::make_shared<Wasm>(base_wasm.wasmVm()->vm(), base_wasm.id(),
base_wasm.initial_configuration(), base_wasm.clusterManager(),
Expand Down
3 changes: 1 addition & 2 deletions source/extensions/common/wasm/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class Wasm : public Envoy::Server::Wasm,
public:
Wasm(absl::string_view vm, absl::string_view id, absl::string_view initial_configuration,
Upstream::ClusterManager& cluster_manager, Event::Dispatcher& dispatcher);
Wasm(const Wasm& other);
Wasm(const Wasm& other, Event::Dispatcher& dispatcher);
~Wasm() {}

bool initialize(const std::string& code, absl::string_view name, bool allow_precompiled);
Expand Down Expand Up @@ -355,7 +355,6 @@ class Wasm : public Envoy::Server::Wasm,
uint32_t next_context_id_ = 0;
std::unique_ptr<WasmVm> wasm_vm_;
std::shared_ptr<Context> general_context_; // Context unrelated to any specific stream.
std::function<void(Common::Wasm::Context*)> tick_;
std::chrono::milliseconds tick_period_;
Event::TimerPtr timer_;

Expand Down

0 comments on commit 14a36c2

Please sign in to comment.