Comments (2)
Yeah, just using a "run" function close to the struct was my first solution as well. But since you pointed out how the supervision doesn't work with a reader process it really makes little sense to have a trait for a reader process. It would probably be nicer to have some built-in way to define the "run" or "loop" logic, but it's not a necessity I guess. I will continue writing it the way you suggested here and see how that works in the long run.
from lunatic-rs.
I don't think we can implement this as part of the AbstractProcess
though. The main purpose of the AbstractProcess
is to dispatch messages based on the type. This means to allow handling incoming messages with ProcessMessage
and ProcessRequest
.
If you change the run
function implementation, this breaks it. Now defining a ProcessMessage
handler and sending a message to this process will actually never trigger the handler, because the process is running in an infinite loop and never processing messages. It may communicate the wrong idea to developers, that they somehow can change the state from inside the handler
function.
We could introduce a LoopingProcess
trait, but I don't think this trait would be of much use to us. It would only allow us to keep the logic "closer" to the struct, but you can do this with something like:
struct ClientProcess {
// fields
}
impl ClientProcess {
pub fn start() {
Process::spawn(Self{ ... }, |this, _| Self::run(this))
}
fn run(state: Self) {
loop {
match state.packet_reader.read() {
Ok(message) => {
state.metrics_recorder.track_new_packet();
println!("Received packet {:?}", message);
state.writer.respond("Some response");
}
Err(err) => panic!("A decoding error ocurred: {:?}", err),
};
}
}
}
Notice also that attaching a reader like this to a supervision tree generally doesn't work. You can't restart a failed reader that already consumed some of the TcpStream
. It will not have enough context to continue working.
What we could do is abstract this start()
implementation behind a LoopingProcess
trait so that you only need to implement a fn loop(state: &Self)
method that will be called over and over again. This could save some boilerplate, but it's not much code and staying explicit about what is exactly happening here is cool too.
from lunatic-rs.
Related Issues (20)
- `send` functions should take a reference of message `&M` HOT 2
- RUSTSEC-2020-0168: mach is unmaintained
- RUSTSEC-2021-0145: Potential unaligned read
- Add a dynamic WorkerPool primitive
- `lunatic_sqlite_api` breaks rust-analyzer (vscode) in a cargo workspace HOT 1
- RUSTSEC-2023-0052: webpki: CPU denial of service in certificate path building
- Question: how to compile examples/monitor.rs into monitor.wasm? HOT 1
- abstract_process Macro send_after Design HOT 2
- Support generics and `where` clauses in `#[abstract_process]` macro HOT 2
- Support `Mailbox` serializer type param in `spawn!` macro
- Mailbox should be the first argument in `spawn` and `spawn_link` HOT 5
- Abstract process panics when using different Serializer in `RequestHandler` HOT 5
- Encode / Decode local buffer before sending to VM HOT 9
- If dependencies use different versions of the `lunatic` compilation fails
- Spawning processes should allow for borrowed captured data HOT 2
- Allow specifying configuration & tag when spawning from WasmModule HOT 3
- ProcessConfig::new can fail, but will not report this HOT 1
- `@task`s should use a closure to allow for return keyword and `?` operator
- Unsoundness with `Fn` trait HOT 3
- Async Request Handlers 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 lunatic-rs.