Comments (6)
Because you did not prolong anything. This time you have a short-living reference to Rc
. You forgot to make a copy of Rc
.
This is usually done so:
let obj = Rc::new(...);
let my_function = {
let obj_copy = obj.clone();
move |foo| { /* do something with obj_copy */ }
};
Please read thr Rust lifetimes tutorial and understand the difference between cloning a Rc<T>
and copying a &Rc<T>
.
from tokio-tungstenite.
You're doing the same mistake again and again. This is not related to Tungstenite. Did you read the tutorial? https://tokio.rs/docs/getting-started/futures/
Also, your reference to the HashMap
is not mutable, so you can't add to it at all. Please consider reading this: https://doc.rust-lang.org/book/first-edition/ownership.html
from tokio-tungstenite.
@Relrin why do you suspect lifetimes to be a problem in this place. According to the errors you posted in your question, rust compiler does not like the type which you passed in for_each
, namely it tells you that it expects a function which in turn returns something that can be turned into a future IntoFuture
.
This is the for_each
function which you called: https://docs.rs/futures/0.1.17/futures/stream/trait.Stream.html#method.for_each
As you can see from the documentation for this function, it tells exactly the same the Rust compiler wrote in the error.
from tokio-tungstenite.
@agalakhov @application-developer-DA
I'm presuming that is the issue with lifetimes and borrowing first of all because when trying just to get a copy of WebSocket headers and using later somewhere in future, like this (at actual moment it only copying, without using somewhere later):
pub fn run(&self, address: SocketAddr) {
let mut core = Core::new().unwrap();
let handle = core.handle();
let socket = TcpListener::bind(&address, &handle).unwrap();
println!("Listening on: {}", address);
let server = socket.incoming().for_each(|(stream, addr)| {
let engine_inner = self.engine.clone();
let connections_inner = self.connections.clone();
let auth_middleware_inner = self.auth_middleware.clone();
let handle_inner = handle.clone();
let mut headers: Headers = HashMap::new();
let copy_headers_callback = |request: &Request| {
for &(ref name, ref value) in request.headers.iter() {
headers.insert(name.to_string(), value.clone());
}
Ok(None)
};
accept_hdr_async(stream, copy_headers_callback)
// Process the messages
.and_then(move |ws_stream| {
You will receive an error that was generated by the compiler:
error[E0597]: `headers` does not live long enough
--> src/proxy.rs:60:21
|
58 | let copy_headers_callback = |request: &Request| {
| ------------------- capture occurs here
59 | for &(ref name, ref value) in request.headers.iter() {
60 | headers.insert(name.to_string(), value.clone());
| ^^^^^^^ does not live long enough
...
111 | });
| - borrowed value only lives until here
...
115 | }
| - borrowed value needs to live until here
from tokio-tungstenite.
Because it does not live long enough. Your function is asynchronous, that is, you can't tell WHEN the callback will be called. The headers
variable does not live so long. It only lives until the end of the closure, but the closure returns a future, not a value! Use Rc
.
from tokio-tungstenite.
I'd tried to prolong the life an object via using Rc<RefCell>
wrapped for a HashMap
, but still getting the same errors. It looks weird, but, I'm interested in allocating a memory for a headers in run-time only in the certain moment, when he's connecting and copy the headers into this "structure".
So, the compiler said "your headers should somewhere outside" of the scope of let server = ...
expression. But in this case then I should make the Rc<RefCell<Headers>>
as a part of a proxy, which is can be used for storing a data per each user. For an instance it could be a field of Proxy struct with Rc<RefCell<HashMap<Uuid, Headers>>>
, however from the some point of view it looks like a dirty hack, isn't it? And, yeah, we still got here an issue with life of an object, but in this case it could be Uuid
typo.
The current implementation looks like this:
pub type Headers = HashMap<String, Box<[u8]>>;
pub struct Proxy {
engine: Rc<RefCell<Engine>>,
connections: Rc<RefCell<HashMap<SocketAddr, mpsc::UnboundedSender<Message>>>>,
auth_middleware: Rc<RefCell<Box<Middleware>>>,
}
impl Proxy {
pub fn new(router: Box<Router>, cli: &CliOptions) -> Proxy {
let auth_middleware: Box<Middleware> = Box::new(EmptyMiddleware::new(cli));
Proxy {
engine: Rc::new(RefCell::new(Engine::new(router))),
connections: Rc::new(RefCell::new(HashMap::new())),
auth_middleware: Rc::new(RefCell::new(auth_middleware)),
}
}
pub fn run(&self, address: SocketAddr) {
let mut core = Core::new().unwrap();
let handle = core.handle();
let socket = TcpListener::bind(&address, &handle).unwrap();
println!("Listening on: {}", address);
let server = socket.incoming().for_each(|(stream, addr)| {
let engine_inner = self.engine.clone();
let connections_inner = self.connections.clone();
let auth_middleware_inner = self.auth_middleware.clone();
let handle_inner = handle.clone();
let mut headers: Rc<RefCell<Headers>> = Rc::new(RefCell::new(HashMap::new()));
let copy_headers_callback = |request: &Request| {
let headers_inner = headers.clone();
for &(ref name, ref value) in request.headers.iter() {
headers_inner.borrow_mut().insert(name.to_string(), value.clone());
}
Ok(None)
};
accept_hdr_async(stream, copy_headers_callback)
// Process the messages
.and_then(move |ws_stream| {
// Create a channel for the stream, which other sockets will use to
// send us messages. It could be used for broadcasting your data to
// another users in the future.
let (tx, rx) = mpsc::unbounded();
connections_inner.borrow_mut().insert(addr, tx);
// Split the WebSocket stream so that it will be possible to work
// with the reading and writing halves separately.
let (sink, stream) = ws_stream.split();
// TODO: fire it before starting the processing messages
let _auth_future = auth_middleware_inner.borrow().process_request(&headers.borrow(), &handle_inner);
// Read and process each message
let connections = connections_inner.clone();
let ws_reader = stream.for_each(move |message: Message| {
engine_inner.borrow().handle(&message, &addr, &connections);
Ok(())
});
// Write back prepared responses
let ws_writer = rx.fold(sink, |mut sink, msg| {
sink.start_send(msg).unwrap();
Ok(sink)
});
// Wait for either half to be done to tear down the other
let connection = ws_reader.map(|_| ()).map_err(|_| ())
.select(ws_writer.map(|_| ()).map_err(|_| ()));
// Close the connection after using
handle_inner.spawn(connection.then(move |_| {
connections_inner.borrow_mut().remove(&addr);
println!("Connection {} closed.", addr);
Ok(())
}));
Ok(())
})
// An error occurred during the WebSocket handshake
.or_else(|err| {
println!("{}", err.description());
Ok(())
})
});
// Run the server
core.run(server).unwrap();
}
}
A generated error is still the same:
error[E0597]: `headers` does not live long enough
--> src/proxy.rs:59:37
|
58 | let copy_headers_callback = |request: &Request| {
| ------------------- capture occurs here
59 | let headers_inner = headers.clone();
| ^^^^^^^ does not live long enough
...
112 | });
| - borrowed value only lives until here
...
116 | }
| - borrowed value needs to live until here
from tokio-tungstenite.
Related Issues (20)
- Passing body in `connect`? HOT 2
- Can we provided a way to keep the original header name case. HOT 1
- 403 Forbidden HOT 1
- "WebSocket protocol error: httparse error: invalid token" when connect from ngrok tcp tunnel. HOT 5
- Is it possible to bind client to specific interface? HOT 1
- 没有连接断开时,直接检测客户端的状态的api,以实现重新连接,这在其它语言中都是常见的
- Performance boost implementing `poll_write_vectored` and rustls 0.23 HOT 2
- Is MaybeTlsStream supposed to work for servers too? HOT 2
- 0.20 and 0.21 break something HOT 25
- Vectorized send HOT 2
- new rustls 0.23.x is incompatible with the 0.22.x used by tokio-tungstenite HOT 2
- Adding authorization header leads in infinite block of connect_async HOT 2
- How to correctly handle large messages HOT 1
- How can I send pong frames as heartbeats from a client? HOT 4
- Specify default-features = false for rustls in Cargo.toml
- Websocket missing some ping messages. HOT 2
- tokio-tungstenite not using version 0.22 of tungstenite HOT 1
- no process-level CryptoProvider available -- call CryptoProvider::install_default() before this point HOT 5
- No option to enable `url` flag on `tungstenite` HOT 1
- How to reply to the client and send a message if an error occures on `accept_async` funtion. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tokio-tungstenite.