Skip to content

Commit af786b6

Browse files
committed
fix: grpc services lifecycle usage
1 parent 5c0e67b commit af786b6

File tree

5 files changed

+76
-33
lines changed

5 files changed

+76
-33
lines changed

examples/services/grpc/src/main.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ use helloworld::{HelloReply, HelloRequest};
1212

1313
#[derive(Clone)]
1414
pub struct MyGreeter {
15-
ctx: Arc<mikros::FutureMutex<Box<Context>>>
15+
ctx: Box<Arc<mikros::FutureMutex<Context>>>
1616
}
1717

1818
impl MyGreeter {
19-
pub fn new(ctx: Arc<mikros::FutureMutex<Box<Context>>>) -> Self {
20-
Self { ctx }
19+
pub fn new(ctx: Arc<mikros::FutureMutex<Context>>) -> Self {
20+
Self {
21+
ctx: Box::new(ctx)
22+
}
2123
}
2224
}
2325

@@ -60,12 +62,13 @@ impl lifecycle::Lifecycle for Context {
6062

6163
#[tokio::main]
6264
async fn main() -> Result<(), Box<dyn std::error::Error>> {
63-
let ctx = Arc::new(mikros::FutureMutex::new(Box::new(Context{ value: 0 })));
65+
let ctx = Arc::new(mikros::FutureMutex::new(Context{ value: 0 }));
6466
let greeter = Arc::new(MyGreeter::new(ctx.clone()));
6567
let greeter_service = GreeterServer::from_arc(greeter);
6668

6769
let svc = ServiceBuilder::default()
68-
.grpc(greeter_service, &ctx)
70+
// .grpc(greeter_service)
71+
.grpc_with_lifecycle(greeter_service, ctx.clone())
6972
.build();
7073

7174
match svc {

src/plugin/service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ where
3232

3333
impl Clone for Box<dyn Service> {
3434
fn clone(&self) -> Self {
35-
self.clone_box()
35+
ServiceClone::clone_box(self.as_ref())
3636
}
3737
}

src/service/builder.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,23 @@ impl ServiceBuilder {
2929
}
3030
}
3131

32-
/// Sets the current service as a native type.
32+
/// Initializes the native service type with the required structure
33+
/// implementing its API.
3334
pub fn native<S: NativeService + 'static>(mut self, svc: Arc<Mutex<S>>) -> Self {
3435
self.servers.insert(definition::ServiceKind::Native.to_string(), Box::new(Native::new(svc)));
3536
self
3637
}
3738

38-
/// Sets the current service as a script type.
39+
/// Initializes the script service type with the required structure
40+
/// implementing its API.
3941
pub fn script<S: ScriptService + 'static>(mut self, svc: Arc<Mutex<S>>) -> Self {
4042
self.servers.insert(definition::ServiceKind::Script.to_string(), Box::new(Script::new(svc)));
4143
self
4244
}
4345

44-
pub fn grpc<S, B>(mut self, server: S, lifecycle: &Arc<Mutex<Box<B>>>) -> Self
46+
/// Initializes the grpc service type with the required structure
47+
/// implementing its API.
48+
pub fn grpc<S>(mut self, server: S) -> Self
4549
where
4650
S: tonic::codegen::Service<Request<BoxBody>, Response = Response<BoxBody>, Error = Infallible>
4751
+ NamedService
@@ -50,12 +54,25 @@ impl ServiceBuilder {
5054
+ Sync
5155
+ 'static,
5256
S::Future: Send + 'static,
53-
B: Lifecycle
57+
{
58+
self.servers.insert(definition::ServiceKind::Grpc.to_string(), Box::new(Grpc::new(server)));
59+
self
60+
}
61+
62+
/// Initializes the grpc service type with the required structure
63+
/// implementing its API and another with implementing the Lifecycle
64+
/// API.
65+
pub fn grpc_with_lifecycle<S, B: Lifecycle + 'static>(mut self, server: S, lifecycle: Arc<Mutex<B>>) -> Self
66+
where
67+
S: tonic::codegen::Service<Request<BoxBody>, Response = Response<BoxBody>, Error = Infallible>
68+
+ NamedService
5469
+ Clone
5570
+ Send
71+
+ Sync
5672
+ 'static,
73+
S::Future: Send + 'static,
5774
{
58-
self.servers.insert(definition::ServiceKind::Grpc.to_string(), Box::new(Grpc::new(server, lifecycle)));
75+
self.servers.insert(definition::ServiceKind::Grpc.to_string(), Box::new(Grpc::new_with_lifecycle(server, lifecycle)));
5976
self
6077
}
6178

src/service/grpc/mod.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ use crate::grpc;
1818
use crate::service::lifecycle::Lifecycle;
1919

2020
#[derive(Clone)]
21-
pub(crate) struct Grpc<S, B> {
21+
pub(crate) struct Grpc<S> {
2222
port: i32,
2323
server: S,
24-
lifecycle: Arc<Mutex<Box<B>>>,
24+
lifecycle: Option<Box<Arc<Mutex<dyn Lifecycle>>>>,
2525
}
2626

27-
impl<S, B> Grpc<S, B>
27+
impl<S> Grpc<S>
2828
where
2929
S: tonic::codegen::Service<Request<BoxBody>, Response = Response<BoxBody>, Error = Infallible>
3030
+ NamedService
@@ -33,22 +33,26 @@ where
3333
+ Sync
3434
+ 'static,
3535
S::Future: Send + 'static,
36-
B: Lifecycle
37-
+ Clone
38-
+ Send
39-
+ 'static,
4036
{
41-
pub(crate) fn new(server: S, lifecycle: &Arc<Mutex<Box<B>>>) -> Self {
37+
pub(crate) fn new_with_lifecycle<B: Lifecycle + 'static>(server: S, lifecycle: Arc<Mutex<B>>) -> Self {
4238
Self {
4339
port: 0,
4440
server,
45-
lifecycle: lifecycle.clone(),
41+
lifecycle: Some(Box::new(lifecycle)),
42+
}
43+
}
44+
45+
pub(crate) fn new(server: S) -> Self {
46+
Self {
47+
port: 0,
48+
server,
49+
lifecycle: None,
4650
}
4751
}
4852
}
4953

5054
#[async_trait::async_trait]
51-
impl<S, B> Lifecycle for Grpc<S, B>
55+
impl<S> Lifecycle for Grpc<S>
5256
where
5357
S: tonic::codegen::Service<Request<BoxBody>, Response = Response<BoxBody>, Error = Infallible>
5458
+ NamedService
@@ -57,22 +61,26 @@ where
5761
+ Sync
5862
+ 'static,
5963
S::Future: 'static + Send,
60-
B: Lifecycle
61-
+ Clone
62-
+ Send
63-
+ 'static,
6464
{
6565
async fn on_start(&mut self) -> merrors::Result<()> {
66-
self.lifecycle.lock().await.on_start().await
66+
if let Some(lifecycle) = &self.lifecycle {
67+
return lifecycle.lock().await.on_start().await
68+
}
69+
70+
Ok(())
6771
}
6872

6973
async fn on_finish(&self) -> merrors::Result<()> {
70-
self.lifecycle.lock().await.on_finish().await
74+
if let Some(lifecycle) = &self.lifecycle {
75+
return lifecycle.lock().await.on_finish().await
76+
}
77+
78+
Ok(())
7179
}
7280
}
7381

7482
#[async_trait::async_trait]
75-
impl<S, B> plugin::service::Service for Grpc<S, B>
83+
impl<S> plugin::service::Service for Grpc<S>
7684
where
7785
S: tonic::codegen::Service<Request<BoxBody>, Response = Response<BoxBody>, Error = Infallible>
7886
+ NamedService
@@ -81,10 +89,6 @@ where
8189
+ Sync
8290
+ 'static,
8391
S::Future: Send + 'static,
84-
B: Lifecycle
85-
+ Clone
86-
+ Send
87-
+ 'static,
8892
{
8993
fn kind(&self) -> definition::ServiceKind {
9094
definition::ServiceKind::Grpc

src/service/lifecycle.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::errors as merrors;
22

33
#[async_trait::async_trait]
4-
pub trait Lifecycle: Send + Sync {
4+
pub trait Lifecycle: LifecycleClone + Send + Sync {
55
async fn on_start(&mut self) -> merrors::Result<()> {
66
Ok(())
77
}
@@ -10,3 +10,22 @@ pub trait Lifecycle: Send + Sync {
1010
Ok(())
1111
}
1212
}
13+
14+
pub trait LifecycleClone {
15+
fn clone_box(&self) -> Box<dyn Lifecycle>;
16+
}
17+
18+
impl<T> LifecycleClone for T
19+
where
20+
T: 'static + Lifecycle + Clone,
21+
{
22+
fn clone_box(&self) -> Box<dyn Lifecycle> {
23+
Box::new(self.clone())
24+
}
25+
}
26+
27+
impl Clone for Box<dyn Lifecycle> {
28+
fn clone(&self) -> Self {
29+
LifecycleClone::clone_box(self.as_ref())
30+
}
31+
}

0 commit comments

Comments
 (0)