proxy-wasm / proxy-wasm-cpp-sdk

WebAssembly for Proxies (C++ SDK)
Apache License 2.0
139 stars 67 forks source link

How to resume filters after returning StopIteration in onRequestHeaders()? #156

Closed yizhengx closed 1 year ago

yizhengx commented 1 year ago

Hi, I'm writing a wasm filter which needs to stop the http filters for a while then resume it in rootConext Ontick() function. To pause the http filters, I return FilterHeadersStatus::StopIteration in Context::onRequestHeaders(). The codes look like this

FilterHeadersStatus ExampleContext::onRequestHeaders(uint32_t, bool) {
    // store the context id to the queue maintained in rootContext
    rootContext()->pushRequestToQueue(id_);
    return FilterHeadersStatus::StopIteration;
};
void ExampleRootContext::onTick(){
   if (some conditions){
       pop context id from queue;
       resume context;
   }
};

Now I'm confused how to resume the context. I've tried setEffectiveContext(context_id) then call continueConext() which didn't work.

PiotrSikora commented 1 year ago

Do you have trace logs? Could you provide actual code? This should work, and Istio Proxy's StackDriver extension does something similar, see: StackdriverRootContext::onTick().

cc @mpwarres @kyessenov

yizhengx commented 1 year ago

@PiotrSikora Thank you for your reply. Now it works. Previously I wrap the continueRequest() function as a method in ExampleContext class since I thought continueRequest() may need to be called by Context object.

void ExampleRootContext::onTick(){
   if (some conditions){
       pop context id from queue;
       getContext(context_id)->setEffectiveContext();
       getContext(context_id)->continueRequest();
   }
};

Now I just remove it and directly call continueRequest() in OnTick().

void ExampleRootContext::onTick(){
   if (some conditions){
       pop context id from queue;
       getContext(context_id)->setEffectiveContext();
       continueRequest();
   }
};