我想编写一个 SOCKS 服务器,它根据客户端请求的目的地选择几个互联网网关之一。一般流程是
- 执行 SOCKS5 协商并从客户端获取地址信息
- 请求内部服务器选择 Internet 网关和目标 IP
- 连接并进行通信
对于这个内部服务器,会生成一个等待 mpsc 队列的 Tokio 任务。接收到的消息应包含 SOCKS5 地址信息和 oneshot 通道的 tx 端以返回结果。
另一个 Tokio 任务只是定期查询内部服务器:
extern crate futures;
extern crate tokio_core;
extern crate tokio_timer;
use std::time;
use std::time::{Duration, Instant};
use std::fmt::Debug;
use tokio_core::reactor::{Core, Interval};
use tokio_timer::wheel;
use futures::{Future, Sink, Stream};
use futures::sync::{mpsc, oneshot};
type MsgRequest<A, E> = oneshot::Sender<Result<A, E>>;
type FutRequest<A, E> = mpsc::Sender<MsgRequest<A, E>>;
#[derive(Debug)]
struct Responder<A, E> {
fut_tx: FutRequest<A, E>,
}
impl<A: 'static, E: 'static> Responder<A, E>
where
E: Debug,
{
fn query(&self) -> Result<A, E> {
println!("enter query");
let (res_tx, res_rx) = oneshot::channel::<Result<A, E>>();
println!("send query");
let fut_tx = self.fut_tx.clone();
let res = fut_tx
.send(res_tx)
.then(|tx| {
if let Ok(_tx) = tx {
println!("Sink flushed");
}
res_rx
})
.and_then(|x| Ok(x))
.wait()
.unwrap();
res
}
}
impl<A: 'static, E: 'static> Clone for Responder<A, E> {
fn clone(&self) -> Self {
Responder {
fut_tx: self.fut_tx.clone(),
}
}
}
fn resolve(tx: oneshot::Sender<Result<u8, String>>) -> Result<(), ()> {
println!("resolve");
let delay = time::Duration::from_secs(10);
wheel()
.build()
.sleep(delay)
.then(|_| tx.send(Ok(0)))
.wait()
.unwrap();
println!("resolve answered");
Ok(())
}
fn main() {
let mut lp = Core::new().unwrap();
let handle = lp.handle();
let (fut_tx, fut_rx) = mpsc::channel::<MsgRequest<u8, String>>(100);
let resolver = fut_rx.for_each(|msg| resolve(msg));
handle.spawn(resolver);
let responder = Responder { fut_tx };
let server = Interval::new_at(Instant::now(), Duration::new(2, 0), &handle)
.unwrap()
.for_each(move |_| {
println!("Call query for_each");
let rx = responder.clone();
let _res = rx.query();
Ok(())
})
.map_err(|_| ());
handle.spawn(server);
loop {
lp.turn(None);
}
}
使用 Cargo.toml 依赖项:
[dependencies]
futures = "0.1"
tokio-core = "0.1"
tokio-timer = "0.1"
此代码死锁。输出是:
Call query for_each
enter query
send query
Sink flushed
预期输出为:
Call query for_each
enter query
send query
Sink flushed
resolve
resolve answered
Call query for_each
enter query
send query
Sink flushed
resolve
resolve answered
....
这表明该tx
结束的请求已成功发送到内部服务器,但内部服务器没有处理它。据我了解,mpsc 和 oneshot 可用于在任务之间传输,而不仅仅是线程,因此包含线程不应像它那样死锁。
这里有什么问题?