Skip to content

Commit 5f1597a

Browse files
authored
Allow to set multiple ServiceConfig::on_worker_start() callbacks (#760)
1 parent 8e42112 commit 5f1597a

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ ntex-net = "3.6.3"
6060
ntex-http = "1.0.0"
6161
ntex-router = "1.0.0"
6262
ntex-rt = "3.7.0"
63-
ntex-server = "3.6.0"
63+
ntex-server = "3.7.0"
6464
ntex-service = "4.4.0"
6565
ntex-tls = "3.2.1"
6666
ntex-macros = "3.1.0"

ntex-server/CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes
22

3+
## [3.7.0] - 2026-02-12
4+
5+
* Allow to set multiple ServiceConfig::on_worker_start() callbacks
6+
37
## [3.6.0] - 2026-01-28
48

59
* Use system name for workers

ntex-server/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ntex-server"
3-
version = "3.6.0"
3+
version = "3.7.0"
44
authors = ["ntex contributors <team@ntex.rs>"]
55
description = "Server for ntex framework"
66
keywords = ["network", "framework", "async", "futures"]

ntex-server/src/net/config.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ struct Socket {
4949

5050
pub(super) struct ServiceConfigInner {
5151
token: Token,
52-
apply: Option<Box<dyn OnWorkerStart>>,
52+
on_start_set: bool,
53+
on_start: Vec<Box<dyn OnWorkerStart>>,
5354
sockets: Vec<Socket>,
5455
backlog: i32,
5556
}
@@ -70,10 +71,11 @@ impl ServiceConfig {
7071
token,
7172
backlog,
7273
sockets: Vec::new(),
73-
apply: Some(OnWorkerStartWrapper::create(|_| {
74+
on_start_set: false,
75+
on_start: vec![OnWorkerStartWrapper::create(|_| {
7476
not_configured();
7577
Ready::Ok::<_, &str>(())
76-
})),
78+
})],
7779
})))
7880
}
7981

@@ -149,7 +151,12 @@ impl ServiceConfig {
149151
F: AsyncFn(ServiceRuntime) -> Result<(), E> + Send + Clone + 'static,
150152
E: fmt::Display + 'static,
151153
{
152-
self.0.borrow_mut().apply = Some(OnWorkerStartWrapper::create(f));
154+
let mut inner = self.0.borrow_mut();
155+
if !inner.on_start_set {
156+
inner.on_start.clear();
157+
inner.on_start_set = true;
158+
}
159+
inner.on_start.push(OnWorkerStartWrapper::create(f));
153160
self
154161
}
155162

@@ -187,33 +194,39 @@ impl ServiceConfig {
187194
sockets,
188195
Box::new(ConfiguredService {
189196
names,
190-
rt: inner.apply.take().unwrap(),
197+
on_start: mem::take(&mut inner.on_start),
191198
}),
192199
)
193200
}
194201
}
195202

196203
struct ConfiguredService {
197-
rt: Box<dyn OnWorkerStart>,
198204
names: HashMap<String, Entry>,
205+
on_start: Vec<Box<dyn OnWorkerStart>>,
199206
}
200207

201208
impl FactoryService for ConfiguredService {
202209
fn clone_factory(&self) -> FactoryServiceType {
203210
Box::new(Self {
204-
rt: self.rt.clone(),
205211
names: self.names.clone(),
212+
on_start: self.on_start.iter().map(|cb| (*cb).clone()).collect(),
206213
})
207214
}
208215

209216
fn create(&self) -> BoxFuture<'static, Result<Vec<NetService>, ()>> {
210217
// configure services
211218
let rt = ServiceRuntime::new(self.names.clone());
212-
let cfg_fut = self.rt.run(ServiceRuntime(rt.0.clone()));
219+
let on_start: Vec<_> = self
220+
.on_start
221+
.iter()
222+
.map(|cb| cb.run(ServiceRuntime(rt.0.clone())))
223+
.collect();
213224

214225
// construct services
215226
Box::pin(async move {
216-
cfg_fut.await?;
227+
for fut in on_start {
228+
fut.await?;
229+
}
217230
rt.validate();
218231

219232
let names = mem::take(&mut rt.0.borrow_mut().names);

0 commit comments

Comments
 (0)