Skip to content

Instantly share code, notes, and snippets.

@marioidival
Last active July 30, 2018 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marioidival/b081abb4b413edced81e97bc5bc0cae6 to your computer and use it in GitHub Desktop.
Save marioidival/b081abb4b413edced81e97bc5bc0cae6 to your computer and use it in GitHub Desktop.
resp: 245
resp: 246
resp: 247
resp: 248
resp: 249
resp: 250
resp: 251
resp: 252
resp: 253
resp: 254
resp: 255
resp: 256
resp: 257
error map: Mailbox has closed
error map: Mailbox has closed
error map: Mailbox has closed
error map: Mailbox has closed
error map: Mailbox has closed
error map: Mailbox has closed
error map: Mailbox has closed
error map: Mailbox has closed
error map: Mailbox has closed
error map: Mailbox has closed
error map: Mailbox has closed
error map: Mailbox has closed
error map: Mailbox has closed
thread 'main' panicked at 'Use Self::Context::notify() instead of direct use of address', /home/kakarotto/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-0.7.2/src/mailbox.rs:91:17
extern crate actix;
extern crate futures;
extern crate tokio;
use actix::prelude::*;
use futures::Future;
struct Count(i64);
impl Message for Count {
type Result = i64;
}
struct Counter {
counter: i64,
}
impl Actor for Counter {
type Context = Context<Self>;
}
impl Handler<Count> for Counter {
type Result = i64;
fn handle(&mut self, msg: Count, _ctx: &mut Context<Self>) -> Self::Result {
println!("current counter {}", self.counter);
self.counter += msg.0;
self.counter
}
}
fn main() {
System::run(|| {
let addr = Arbiter::start(|ctx: &mut Context<_>| {
ctx.set_mailbox_capacity(0);
Counter { counter: 1 }
});
for _ in 1..1000 {
let res = addr.send(Count(1));
tokio::spawn(res.map(|res| {
println!("resp: {}", res);
System::current().stop();
}).map_err(|e| {
eprintln!("error map: {}", e);
()
}));
}
});
}
extern crate actix;
use actix::prelude::*;
struct Count(i64);
impl Message for Count {
type Result = i64;
}
struct Counter {
counter: i64,
}
impl Actor for Counter {
type Context = Context<Self>;
fn stopped(&mut self, _ctx: &mut Context<Self>) {
System::current().stop()
}
}
impl Handler<Count> for Counter {
type Result = i64;
fn handle(&mut self, msg: Count, _ctx: &mut Context<Self>) -> Self::Result {
println!("current counter {}", self.counter);
self.counter += msg.0;
self.counter
}
}
fn main() {
System::run(|| {
Arbiter::start(|ctx: &mut Context<_>| {
ctx.set_mailbox_capacity(0);
for _ in 1..100000 {
ctx.notify(Count(1));
}
Counter { counter: 1 }
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment