Skip to content

Commit b06a97d

Browse files
committed
Quick and dirty patch to compile client-only transport on wasm32.
1 parent ed7a686 commit b06a97d

File tree

11 files changed

+74
-32
lines changed

11 files changed

+74
-32
lines changed

tonic/Cargo.toml

+14-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ keywords = ["rpc", "grpc", "async", "futures", "protobuf"]
2626
default = ["transport", "codegen", "prost"]
2727
codegen = ["async-trait"]
2828
transport = [
29+
"hyper/default",
30+
"hyper/stream",
31+
"tokio",
32+
"tower",
33+
"tower-balance",
34+
"tower-load",
35+
"tracing-futures",
36+
]
37+
client = [
2938
"hyper",
3039
"tokio",
3140
"tower",
@@ -64,8 +73,8 @@ prost-derive = { version = "0.6", optional = true }
6473
async-trait = { version = "0.1.13", optional = true }
6574

6675
# transport
67-
hyper = { version = "0.13.4", features = ["stream"], optional = true }
68-
tokio = { version = "0.2.13", features = ["tcp"], optional = true }
76+
hyper = { version = "0.13.4", default-features = false, optional = true }
77+
tokio = { version = "0.2.13", optional = true }
6978
tower = { version = "0.3", optional = true}
7079
tower-make = { version = "0.3", features = ["connect"] }
7180
tower-balance = { version = "0.3", optional = true }
@@ -76,6 +85,9 @@ tracing-futures = { version = "0.2", optional = true }
7685
tokio-rustls = { version = "0.14", optional = true }
7786
rustls-native-certs = { version = "0.4", optional = true }
7887

88+
[target.'cfg(target_arch = "wasm32")'.dependencies]
89+
wasm-bindgen-futures = "0.4.18"
90+
7991
[dev-dependencies]
8092
tokio = { version = "0.2", features = ["rt-core", "macros"] }
8193
static_assertions = "1.0"

tonic/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub mod codec;
8282
pub mod metadata;
8383
pub mod server;
8484

85-
#[cfg(feature = "transport")]
85+
#[cfg(any(feature = "transport", feature = "client"))]
8686
#[cfg_attr(docsrs, doc(cfg(feature = "transport")))]
8787
pub mod transport;
8888

tonic/src/request.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::metadata::MetadataMap;
2-
#[cfg(feature = "transport")]
2+
#[cfg(any(feature = "transport", feature = "client"))]
33
use crate::transport::Certificate;
44
use futures_core::Stream;
55
use http::Extensions;
66
use std::net::SocketAddr;
7-
#[cfg(feature = "transport")]
7+
#[cfg(any(feature = "transport", feature = "client"))]
88
use std::sync::Arc;
99

1010
/// A gRPC request and metadata from an RPC call.
@@ -18,7 +18,7 @@ pub struct Request<T> {
1818
#[derive(Clone)]
1919
pub(crate) struct ConnectionInfo {
2020
pub(crate) remote_addr: Option<SocketAddr>,
21-
#[cfg(feature = "transport")]
21+
#[cfg(any(feature = "transport", feature = "client"))]
2222
pub(crate) peer_certs: Option<Arc<Vec<Certificate>>>,
2323
}
2424

tonic/src/transport/channel/endpoint.rs

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ impl Endpoint {
3939
// FIXME: determine if we want to expose this or not. This is really
4040
// just used in codegen for a shortcut.
4141
#[doc(hidden)]
42+
#[cfg(feature = "transport")]
4243
pub fn new<D>(dst: D) -> Result<Self, Error>
4344
where
4445
D: TryInto<Self>,
@@ -203,6 +204,7 @@ impl Endpoint {
203204
}
204205

205206
/// Create a channel from this config.
207+
#[cfg(feature = "transport")]
206208
pub async fn connect(&self) -> Result<Channel, Error> {
207209
let mut http = hyper::client::connect::HttpConnector::new();
208210
http.enforce_http(false);
@@ -223,6 +225,7 @@ impl Endpoint {
223225
///
224226
/// The channel returned by this method does not attempt to connect to the endpoint until first
225227
/// use.
228+
#[cfg(feature = "transport")]
226229
pub fn connect_lazy(&self) -> Result<Channel, Error> {
227230
let mut http = hyper::client::connect::HttpConnector::new();
228231
http.enforce_http(false);

tonic/src/transport/channel/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ impl Channel {
108108
///
109109
/// This creates a [`Channel`] that will load balance accross all the
110110
/// provided endpoints.
111+
#[cfg(feature = "transport")]
111112
pub fn balance_list(list: impl Iterator<Item = Endpoint>) -> Self {
112113
let (channel, mut tx) = Self::balance_channel(DEFAULT_BUFFER_SIZE);
113114
list.for_each(|endpoint| {
@@ -121,6 +122,7 @@ impl Channel {
121122
/// Balance a list of [`Endpoint`]'s.
122123
///
123124
/// This creates a [`Channel`] that will listen to a stream of change events and will add or remove provided endpoints.
125+
#[cfg(feature = "transport")]
124126
pub fn balance_channel<K>(capacity: usize) -> (Self, Sender<Change<K, Endpoint>>)
125127
where
126128
K: Hash + Eq + Send + Clone + 'static,

tonic/src/transport/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,34 @@
8787
//! [rustls]: https://docs.rs/rustls/0.16.0/rustls/
8888
8989
pub mod channel;
90+
#[cfg(feature = "transport")]
9091
pub mod server;
9192

93+
/// Trait that connected IO resources implement.
94+
///
95+
/// The goal for this trait is to allow users to implement
96+
/// custom IO types that can still provide the same connection
97+
/// metadata.
98+
pub trait Connected {
99+
/// Return the remote address this IO resource is connected too.
100+
fn remote_addr(&self) -> Option<std::net::SocketAddr> {
101+
None
102+
}
103+
104+
/// Return the set of connected peer TLS certificates.
105+
fn peer_certs(&self) -> Option<Vec<Certificate>> {
106+
None
107+
}
108+
}
109+
92110
mod error;
93111
mod service;
94112
mod tls;
95113

96114
#[doc(inline)]
97115
pub use self::channel::{Channel, Endpoint};
98116
pub use self::error::Error;
117+
#[cfg(feature = "transport")]
99118
#[doc(inline)]
100119
pub use self::server::{NamedService, Server};
101120
pub use self::tls::{Certificate, Identity};
@@ -105,5 +124,6 @@ pub use hyper::{Body, Uri};
105124
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
106125
pub use self::channel::ClientTlsConfig;
107126
#[cfg(feature = "tls")]
127+
#[cfg(feature = "transport")]
108128
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
109129
pub use self::server::ServerTlsConfig;

tonic/src/transport/server/conn.rs

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
1+
use super::super::Connected;
12
#[cfg(feature = "tls")]
23
use super::TlsStream;
3-
use crate::transport::Certificate;
44
use hyper::server::conn::AddrStream;
55
use std::net::SocketAddr;
66
use tokio::net::TcpStream;
77
#[cfg(feature = "tls")]
88
use tokio_rustls::rustls::Session;
99

10-
/// Trait that connected IO resources implement.
11-
///
12-
/// The goal for this trait is to allow users to implement
13-
/// custom IO types that can still provide the same connection
14-
/// metadata.
15-
pub trait Connected {
16-
/// Return the remote address this IO resource is connected too.
17-
fn remote_addr(&self) -> Option<SocketAddr> {
18-
None
19-
}
20-
21-
/// Return the set of connected peer TLS certificates.
22-
fn peer_certs(&self) -> Option<Vec<Certificate>> {
23-
None
24-
}
25-
}
26-
2710
impl Connected for AddrStream {
2811
fn remote_addr(&self) -> Option<SocketAddr> {
2912
Some(self.remote_addr())

tonic/src/transport/server/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod incoming;
66
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
77
mod tls;
88

9-
pub use conn::Connected;
9+
pub use super::Connected;
1010
#[cfg(feature = "tls")]
1111
pub use tls::ServerTlsConfig;
1212

tonic/src/transport/service/connection.rs

+27-6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ pub(crate) struct Connection {
2828
inner: BoxService<Request, Response, crate::Error>,
2929
}
3030

31+
#[cfg(target_arch = "wasm32")]
32+
mod wasm {
33+
use std::future::Future;
34+
use std::pin::Pin;
35+
36+
type BoxSendFuture = Pin<Box<dyn Future<Output = ()> + Send>>;
37+
38+
pub struct Executor;
39+
40+
impl hyper::rt::Executor<BoxSendFuture> for Executor {
41+
fn execute(&self, fut: BoxSendFuture) {
42+
wasm_bindgen_futures::spawn_local(fut)
43+
}
44+
}
45+
}
46+
3147
impl Connection {
3248
pub(crate) fn new<C>(
3349
connector: C,
@@ -44,16 +60,21 @@ impl Connection {
4460
.http2_initial_stream_window_size(endpoint.init_stream_window_size)
4561
.http2_initial_connection_window_size(endpoint.init_connection_window_size)
4662
.http2_only(true)
47-
.http2_keep_alive_interval(endpoint.http2_keep_alive_interval)
63+
// .http2_keep_alive_interval(endpoint.http2_keep_alive_interval)
4864
.clone();
4965

50-
if let Some(val) = endpoint.http2_keep_alive_timeout {
51-
settings.http2_keep_alive_timeout(val);
66+
#[cfg(target_arch = "wasm32")]
67+
{
68+
settings.executor(wasm::Executor);
5269
}
5370

54-
if let Some(val) = endpoint.http2_keep_alive_while_idle {
55-
settings.http2_keep_alive_while_idle(val);
56-
}
71+
// if let Some(val) = endpoint.http2_keep_alive_timeout {
72+
// settings.http2_keep_alive_timeout(val);
73+
// }
74+
75+
// if let Some(val) = endpoint.http2_keep_alive_while_idle {
76+
// settings.http2_keep_alive_while_idle(val);
77+
// }
5778

5879
let settings = settings.clone();
5980

tonic/src/transport/service/discover.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ impl<K: Hash + Eq + Clone> DynamicServiceStream<K> {
2929
}
3030
}
3131

32+
#[cfg(feature = "transport")]
3233
impl<K: Hash + Eq + Clone> Discover for DynamicServiceStream<K> {
3334
type Key = K;
3435
type Service = Connection;

tonic/src/transport/service/io.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::transport::{server::Connected, Certificate};
1+
use crate::transport::{Certificate, Connected};
22
use hyper::client::connect::{Connected as HyperConnected, Connection};
33
use std::io;
44
use std::net::SocketAddr;

0 commit comments

Comments
 (0)