Skip to content

Commit 28ba351

Browse files
committed
Move context creation to fn of RootContext instead of callback
Signed-off-by: Daniel Grimm <dgrimm@redhat.com>
1 parent 4cb2db7 commit 28ba351

File tree

7 files changed

+86
-49
lines changed

7 files changed

+86
-49
lines changed

examples/http_auth_random.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,23 @@ use std::time::Duration;
2020
#[no_mangle]
2121
pub fn _start() {
2222
proxy_wasm::set_log_level(LogLevel::Trace);
23-
proxy_wasm::set_http_context(|_, _| -> Box<dyn HttpContext> { Box::new(HttpAuthRandom) });
23+
proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> { Box::new(HttpAuthRandomRoot) });
2424
}
2525

2626
struct HttpAuthRandom;
27+
struct HttpAuthRandomRoot;
28+
29+
impl RootContext for HttpAuthRandomRoot {
30+
fn get_type(&self) -> ContextType {
31+
ContextType::HttpContext
32+
}
33+
34+
fn create_http_context(&self, _root_context_id: u32, _context_id: u32) -> Box<dyn HttpContext> {
35+
Box::new(HttpAuthRandom)
36+
}
37+
}
38+
39+
impl Context for HttpAuthRandomRoot {}
2740

2841
impl HttpContext for HttpAuthRandom {
2942
fn on_http_request_headers(&mut self, _: usize) -> Action {

examples/http_body.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,24 @@ use proxy_wasm::types::*;
1818
#[no_mangle]
1919
pub fn _start() {
2020
proxy_wasm::set_log_level(LogLevel::Trace);
21-
proxy_wasm::set_http_context(|_, _| -> Box<dyn HttpContext> { Box::new(HttpBody) });
21+
proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> { Box::new(HttpBodyRoot) });
2222
}
2323

2424
struct HttpBody;
25+
struct HttpBodyRoot;
26+
27+
impl RootContext for HttpBodyRoot {
28+
fn get_type(&self) -> ContextType {
29+
ContextType::HttpContext
30+
}
31+
32+
fn create_http_context(&self, _context_id: u32, _root_context_id: u32) -> Box<dyn HttpContext> {
33+
Box::new(HttpBody)
34+
}
35+
}
36+
37+
impl Context for HttpBodyRoot {}
38+
2539

2640
impl Context for HttpBody {}
2741

examples/http_headers.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,25 @@ use proxy_wasm::types::*;
1919
#[no_mangle]
2020
pub fn _start() {
2121
proxy_wasm::set_log_level(LogLevel::Trace);
22-
proxy_wasm::set_http_context(|context_id, _| -> Box<dyn HttpContext> {
23-
Box::new(HttpHeaders { context_id })
22+
proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> {
23+
Box::new(HttpHeadersRoot)
2424
});
2525
}
2626

27+
struct HttpHeadersRoot;
28+
29+
impl Context for HttpHeadersRoot {}
30+
31+
impl RootContext for HttpHeadersRoot {
32+
fn get_type(&self) -> ContextType {
33+
ContextType::HttpContext
34+
}
35+
36+
fn create_http_context(&self, _context_id: u32, _root_context_id: u32) -> Box<dyn HttpContext> {
37+
Box::new(HttpHeaders{context_id: _context_id})
38+
}
39+
}
40+
2741
struct HttpHeaders {
2842
context_id: u32,
2943
}

src/dispatcher.rs

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@ pub(crate) fn set_root_context(callback: NewRootContext) {
2626
DISPATCHER.with(|dispatcher| dispatcher.set_root_context(callback));
2727
}
2828

29-
pub(crate) fn set_stream_context(callback: NewStreamContext) {
30-
DISPATCHER.with(|dispatcher| dispatcher.set_stream_context(callback));
31-
}
32-
33-
pub(crate) fn set_http_context(callback: NewHttpContext) {
34-
DISPATCHER.with(|dispatcher| dispatcher.set_http_context(callback));
35-
}
36-
3729
pub(crate) fn register_callout(token_id: u32) {
3830
DISPATCHER.with(|dispatcher| dispatcher.register_callout(token_id));
3931
}
@@ -46,9 +38,7 @@ impl RootContext for NoopRoot {}
4638
struct Dispatcher {
4739
new_root: Cell<Option<NewRootContext>>,
4840
roots: RefCell<HashMap<u32, Box<dyn RootContext>>>,
49-
new_stream: Cell<Option<NewStreamContext>>,
5041
streams: RefCell<HashMap<u32, Box<dyn StreamContext>>>,
51-
new_http_stream: Cell<Option<NewHttpContext>>,
5242
http_streams: RefCell<HashMap<u32, Box<dyn HttpContext>>>,
5343
active_id: Cell<u32>,
5444
callouts: RefCell<HashMap<u32, u32>>,
@@ -59,9 +49,7 @@ impl Dispatcher {
5949
Dispatcher {
6050
new_root: Cell::new(None),
6151
roots: RefCell::new(HashMap::new()),
62-
new_stream: Cell::new(None),
6352
streams: RefCell::new(HashMap::new()),
64-
new_http_stream: Cell::new(None),
6553
http_streams: RefCell::new(HashMap::new()),
6654
active_id: Cell::new(0),
6755
callouts: RefCell::new(HashMap::new()),
@@ -72,14 +60,6 @@ impl Dispatcher {
7260
self.new_root.set(Some(callback));
7361
}
7462

75-
fn set_stream_context(&self, callback: NewStreamContext) {
76-
self.new_stream.set(Some(callback));
77-
}
78-
79-
fn set_http_context(&self, callback: NewHttpContext) {
80-
self.new_http_stream.set(Some(callback));
81-
}
82-
8363
fn create_root_context(&self, context_id: u32) {
8464
let new_context = match self.new_root.get() {
8565
Some(f) => f(context_id),
@@ -96,12 +76,9 @@ impl Dispatcher {
9676
}
9777

9878
fn create_stream_context(&self, context_id: u32, root_context_id: u32) {
99-
if !self.roots.borrow().contains_key(&root_context_id) {
100-
panic!("invalid root_context_id")
101-
}
102-
let new_context = match self.new_stream.get() {
103-
Some(f) => f(context_id, root_context_id),
104-
None => panic!("missing constructor"),
79+
let new_context = match self.roots.borrow().get(&root_context_id) {
80+
Some(root_context) => root_context.create_stream_context(context_id, root_context_id),
81+
None => panic!("invalid root_context_id"),
10582
};
10683
if self
10784
.streams
@@ -114,12 +91,9 @@ impl Dispatcher {
11491
}
11592

11693
fn create_http_context(&self, context_id: u32, root_context_id: u32) {
117-
if !self.roots.borrow().contains_key(&root_context_id) {
118-
panic!("invalid root_context_id")
119-
}
120-
let new_context = match self.new_http_stream.get() {
121-
Some(f) => f(context_id, root_context_id),
122-
None => panic!("missing constructor"),
94+
let new_context = match self.roots.borrow().get(&root_context_id) {
95+
Some(root_context) => root_context.create_http_context(context_id, root_context_id),
96+
None => panic!("invalid root_context_id"),
12397
};
12498
if self
12599
.http_streams
@@ -144,11 +118,13 @@ impl Dispatcher {
144118

145119
fn on_create_context(&self, context_id: u32, root_context_id: u32) {
146120
if root_context_id == 0 {
147-
self.create_root_context(context_id)
148-
} else if self.new_http_stream.get().is_some() {
149-
self.create_http_context(context_id, root_context_id);
150-
} else if self.new_stream.get().is_some() {
151-
self.create_stream_context(context_id, root_context_id);
121+
self.create_root_context(context_id);
122+
} else if let Some(root_context) = self.roots.borrow().get(&root_context_id) {
123+
match root_context.get_type() {
124+
ContextType::HttpContext => self.create_http_context(context_id, root_context_id),
125+
ContextType::StreamContext => self.create_stream_context(context_id, root_context_id),
126+
ContextType::RootContext => panic!("missing constructors"),
127+
}
152128
} else {
153129
panic!("missing constructors")
154130
}

src/lib.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,5 @@ pub fn set_root_context(callback: types::NewRootContext) {
2828
dispatcher::set_root_context(callback);
2929
}
3030

31-
pub fn set_stream_context(callback: types::NewStreamContext) {
32-
dispatcher::set_stream_context(callback);
33-
}
34-
35-
pub fn set_http_context(callback: types::NewHttpContext) {
36-
dispatcher::set_http_context(callback);
37-
}
38-
3931
#[no_mangle]
4032
pub extern "C" fn proxy_abi_version_0_1_0() {}

src/traits.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ pub trait RootContext: Context {
121121
fn on_queue_ready(&mut self, _queue_id: u32) {}
122122

123123
fn on_log(&mut self) {}
124+
125+
fn create_http_context(&self, _context_id: u32, _root_context_id: u32) -> Box<dyn HttpContext> {
126+
Box::new(EmptyHttpContext)
127+
}
128+
129+
fn create_stream_context(&self, _context_id: u32, _root_context_id: u32) -> Box<dyn StreamContext> {
130+
Box::new(EmptyStreamContext)
131+
}
132+
133+
fn get_type(&self) -> ContextType {
134+
ContextType::RootContext
135+
}
124136
}
125137

126138
pub trait StreamContext: Context {
@@ -159,6 +171,16 @@ pub trait StreamContext: Context {
159171
fn on_log(&mut self) {}
160172
}
161173

174+
struct EmptyHttpContext;
175+
176+
impl HttpContext for EmptyHttpContext {}
177+
impl Context for EmptyHttpContext {}
178+
179+
struct EmptyStreamContext;
180+
181+
impl StreamContext for EmptyStreamContext {}
182+
impl Context for EmptyStreamContext {}
183+
162184
pub trait HttpContext: Context {
163185
fn on_http_request_headers(&mut self, _num_headers: usize) -> Action {
164186
Action::Continue

src/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,9 @@ pub enum MetricType {
8585
}
8686

8787
pub type Bytes = Vec<u8>;
88+
89+
pub enum ContextType {
90+
RootContext = 0,
91+
HttpContext = 1,
92+
StreamContext = 2,
93+
}

0 commit comments

Comments
 (0)