-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do I use a remote reactor? #80
Comments
I think you'll want to change the return value of If you do that, does it work out? |
You mean by replacing let socket = TcpStream::connect_stream(stream, &addr, handle);
tx.complete(socket); with TcpStream::connect_stream(stream, &addr, handle).and_then(|socket| {
tx.complete(socket);
Ok(())
}); Like this? extern crate futures;
extern crate tokio_core;
extern crate tokio_tls;
use std::net::ToSocketAddrs;
use std::net::TcpStream as StdTcpStream;
use futures::Future;
use tokio_core::reactor::{Core, Remote};
use tokio_core::net::TcpStream;
use tokio_tls::ClientContext;
fn main() {
let remote = Core::new().unwrap().remote();
let (tx, rx) = futures::oneshot();
let stream = StdTcpStream::connect("www.rust-lang.org:443").unwrap();
remote.spawn(|handle| {
let addr = stream.peer_addr().unwrap();
TcpStream::connect_stream(stream, &addr, handle).and_then(|socket| {
tx.complete(socket);
Ok(())
});
Ok(())
});
let tls_handshake = rx.and_then(|socket| {
let cx = ClientContext::new().unwrap();
cx.handshake("www.rust-lang.org", socket)
});
let request = tls_handshake.and_then(|socket| {
tokio_core::io::write_all(socket, "\
GET / HTTP/1.0\r\n\
Host: www.rust-lang.org\r\n\
\r\n\
".as_bytes())
});
let response = request.and_then(|(socket, _)| {
tokio_core::io::read_to_end(socket, Vec::new())
});
let (_, res) = response.wait().unwrap();
println!("{}", String::from_utf8_lossy(&res));
} This gives me:-
|
Oh the function passed to |
@alexcrichton I'm not sure I totally understand you. I've tried what I thought you meant to no avail. Anyway I think something interesting is going on here. In the following code, I'm not using extern crate futures;
extern crate tokio_core;
extern crate tokio_tls;
use std::net::ToSocketAddrs;
use futures::Future;
use tokio_core::reactor::{Core, Remote};
use tokio_core::net::TcpStream;
use tokio_tls::ClientContext;
fn main() {
let remote = Core::new().unwrap().remote();
let addr = "www.rust-lang.org:443".to_socket_addrs().unwrap().next().unwrap();
let socket = TcpStream::connect(&addr, &remote.current_reactor().unwrap());
let tls_handshake = socket.and_then(|socket| {
let cx = ClientContext::new().unwrap();
cx.handshake("www.rust-lang.org", socket)
});
let request = tls_handshake.and_then(|socket| {
tokio_core::io::write_all(socket, "\
GET / HTTP/1.0\r\n\
Host: www.rust-lang.org\r\n\
\r\n\
".as_bytes())
});
let response = request.and_then(|(socket, _)| {
tokio_core::io::read_to_end(socket, Vec::new())
});
let (_, data) = response.wait().unwrap();
println!("{}", String::from_utf8_lossy(&data));
} Note that |
@rushmorem in your previous example yes the types need to match up, but you can fix that by changing this: let tls_handshake = rx.and_then(|socket| { to let tls_handshake = rx.map_err(|_| panic!("canceled")).and_then(|socket| { The second example doesn't compile for me right now due to the |
I see. In that case I think I'm trying to go about this the wrong way. I'm working on a database driver. I'm using https://github.com/sfackler/r2d2 as my connection pool. I'm trying to bring I could introduce Any suggestions? |
Ideally the database driver would be async I/O powered to reduce the overhead, but in lieu of that I think a reasonable starting point for an architecture might look like:
You'd then probably implement that with a thread pool where one thread is handing out connections and other threads are executing the actual queries with those connections. (perhaps) That may not be the most ideal, but is perhaps a place to get started? |
Yes you are right. This approach is what I was referring to when I said I could introduce futures in the exec method. Granted it's not ideal but it's a good first step so I will go with that for now. Thanks a lot for all your help and patience. |
No problem! Feel free to keep asking questions if you've got them :) |
I'm working on a library in which I need to use
tokio_core::reactor::Remote
instead oftokio_core::reactor::Core
. Unfortunately, all the examples I can find are usingtokio_core::reactor::Core
. @carllerche was kind enough to point me in the right direction in #75, so I now have a good idea how this works.I tried to reproduce the tutorial using
Remote
instead ofCore
. Here is the code I came up with so far:-However, this errors out. The first error being:-
I have tried calling
as_ref()
(among other things) to get out of the box on thesocket
on that line 29 which has the first error. How do I get this to work?The text was updated successfully, but these errors were encountered: