Skip to content

Commit 10dec41

Browse files
committed
api: introduce RootContext::on_create_child_context() callback to allow RootContext propagate configuration into HttpContext/StreamContext
1 parent dea75fc commit 10dec41

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/dispatcher.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,45 @@ impl Dispatcher {
9595
}
9696
}
9797

98+
/// Lets the root context to create its child context.
99+
fn create_child_context(&self, context_id: u32, root_context_id: u32) -> bool {
100+
if !self.roots.borrow().contains_key(&root_context_id) {
101+
panic!("invalid root_context_id")
102+
}
103+
if let Some(child_context) = self
104+
.roots
105+
.borrow_mut()
106+
.get_mut(&root_context_id)
107+
.and_then(|root| root.on_create_child_context(context_id))
108+
{
109+
match child_context {
110+
ChildContext::HttpContext(http_context) => {
111+
if self
112+
.http_streams
113+
.borrow_mut()
114+
.insert(context_id, http_context)
115+
.is_some()
116+
{
117+
panic!("duplicate context_id")
118+
}
119+
}
120+
ChildContext::StreamContext(stream_context) => {
121+
if self
122+
.streams
123+
.borrow_mut()
124+
.insert(context_id, stream_context)
125+
.is_some()
126+
{
127+
panic!("duplicate context_id")
128+
}
129+
}
130+
}
131+
true
132+
} else {
133+
false
134+
}
135+
}
136+
98137
fn create_stream_context(&self, context_id: u32, root_context_id: u32) {
99138
if !self.roots.borrow().contains_key(&root_context_id) {
100139
panic!("invalid root_context_id")
@@ -145,6 +184,8 @@ impl Dispatcher {
145184
fn on_create_context(&self, context_id: u32, root_context_id: u32) {
146185
if root_context_id == 0 {
147186
self.create_root_context(context_id)
187+
} else if self.create_child_context(context_id, root_context_id) {
188+
// root context created a child context by himself
148189
} else if self.new_http_stream.get().is_some() {
149190
self.create_http_context(context_id, root_context_id);
150191
} else if self.new_stream.get().is_some() {

src/traits.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ pub trait Context {
9999
}
100100
}
101101

102+
/// Represents a child context of the root context.
103+
pub enum ChildContext {
104+
StreamContext(Box<dyn StreamContext>),
105+
HttpContext(Box<dyn HttpContext>),
106+
}
107+
102108
pub trait RootContext: Context {
103109
fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool {
104110
true
@@ -121,6 +127,12 @@ pub trait RootContext: Context {
121127
fn on_queue_ready(&mut self, _queue_id: u32) {}
122128

123129
fn on_log(&mut self) {}
130+
131+
fn on_create_child_context(&mut self, _context_id: u32) -> Option<ChildContext> {
132+
// for the sake of compatibility with `set_http_context` and `set_stream_context` API,
133+
// root context is not required to create its child context.
134+
None
135+
}
124136
}
125137

126138
pub trait StreamContext: Context {

0 commit comments

Comments
 (0)