proxy-wasm-rust-sdk icon indicating copy to clipboard operation
proxy-wasm-rust-sdk copied to clipboard

how can I use plugin configuration in httpContext

Open LugiaChang opened this issue 1 year ago • 3 comments

My plugin configuration is quite large, and I don't want to clone the plugin configuration every time When I create an httpContext. Can I use Rc::clone() inside Wasm? (Is it may cause memory leak? Im sorry not good at rust) . Or is there any other way for me to get the plugin configuration saved in RootContext from httpContext while avoiding cloning all the data?

struct HttpHeadersRoot {
    wasm_configuration: Rc<WasmConfiguration>,
}

impl RootContext for HttpHeadersRoot {
    fn get_type(&self) -> Option<ContextType> {
        Some(ContextType::HttpContext)
    }

   fn on_configure(&mut self, _: usize) -> bool {
        if let Some(config_bytes) = self.get_plugin_configuration() {
            if str::from_utf8(&config_bytes).is_err() {
                return false;
            }
            let t: WasmConfiguration = serde_json::from_str(str::from_utf8(&config_bytes).unwrap()).unwrap();
            self.wasm_configuration = Rc::new(t);
            return true
        } else {
            return false;
        }
    }

    fn create_http_context(&self, _: u32) -> Option<Box<dyn HttpContext>> {
        Some(Box::new(HttpHeaders {
            wasm_configuration: Rc::clone(&self.wasm_configuration),
        }))
    }
}

LugiaChang avatar Aug 31 '23 02:08 LugiaChang

I used an Arc @LugiaChang

antonengelhardt avatar Aug 31 '23 09:08 antonengelhardt

I used an Arc @LugiaChang

Is Wasm single thread? I'm not quite sure.

LugiaChang avatar Aug 31 '23 09:08 LugiaChang

单线程的,每次请求结束之后会通过 drop 把数据释放掉,所以不存在内存泄露

pi-pi-miao avatar Aug 24 '24 06:08 pi-pi-miao