Skip to content

Commit 2491fea

Browse files
committed
🧵 Service::handle now blocking
Otherwise the `Server::run` becomes `!Send`, making it impossible for users to run it in spawned tasks in multi-threadded runtimes (e.g `tokio::task::spawn`). Implementers can still handle methods asynchronously by returning a stream. Although that means they can not keep a reference to the server around in their async handling logic and are limited to only be able to send owned types. An alternative here would be to accept `Server::run` being `!Send` but givne the widespread use of multi-threaded runtimes and (especially) `tokio::task::spawn`, I have a strong feeling that it would make the API very unattractive for the majority of the potential users.
1 parent 075b704 commit 2491fea

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

zlink/src/server/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ where
158158
writer: &mut WriteConnection<<Listener::Socket as Socket>::WriteHalf>,
159159
) -> crate::Result<Option<Service::ReplyStream>> {
160160
let mut stream = None;
161-
match self.service.handle(call).await {
161+
match self.service.handle(call) {
162162
Reply::Single(reply) => writer.send_reply(reply, Some(false)).await?,
163163
Reply::Error(err) => writer.send_error(err).await?,
164164
Reply::Multi(s) => stream = Some(s),

zlink/src/server/service.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Serice-related API.
22
3-
use core::{fmt::Debug, future::Future};
3+
use core::fmt::Debug;
44

55
use futures_util::Stream;
66
use serde::{Deserialize, Serialize};
@@ -40,12 +40,14 @@ where
4040
Self: 'ser;
4141

4242
/// Handle a method call.
43+
///
44+
/// While this methos is no async, it can return a reply stream that can be used to
45+
/// ascynchronously send out replies. The main use case for allowing to return a stream is
46+
/// multiple replies but it can be used for single reply as well.
4347
fn handle<'ser>(
4448
&'ser mut self,
4549
method: Call<Self::MethodCall<'_>>,
46-
) -> impl Future<
47-
Output = Reply<Option<Self::ReplyParams<'ser>>, Self::ReplyStream, Self::ReplyError<'ser>>,
48-
>;
50+
) -> Reply<Option<Self::ReplyParams<'ser>>, Self::ReplyStream, Self::ReplyError<'ser>>;
4951
}
5052

5153
/// A service method call reply.

0 commit comments

Comments
 (0)