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

Am i missing some thing or is there is a bug ? #1701

Closed
goriunov opened this Issue Nov 6, 2018 · 4 comments

Comments

Projects
None yet
2 participants
@goriunov

goriunov commented Nov 6, 2018

I have simple Hyper server with r2d2 db pool running on arch linux, if i run benchmark wrk with below code i will get around 34k per 10s, but if i comment one like println!("GOt in here"); in PutPost future i am geting around 22k per 10s, Am i missing something ?

Code:

fn insert_data_in_db(post: &NewPost, db: &PgConnection) -> Post {
  diesel::insert_into(posts::table)
    .values(post)
    .get_result(db)
    .expect("Error saving new post")
}

#[derive(Clone)]
struct PutPost {
  pool: Pool,
  new_post: NewPost,
  completed: i8,
}

impl Future for PutPost {
  type Item = ();
  type Error = ();

  fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
    if self.completed < 1 {
      let self_clone = self.clone();
      let mut task_arc: Arc<Mutex<task::Task>> = Arc::new(Mutex::new(task::current()));

      tokio::spawn(poll_fn(move || {
        let self_clone = self_clone.clone();
        let task_arc = task_arc.clone();
        tokio_threadpool::blocking(move || {
          let db_connection = get_conn(&self_clone.pool);
          insert_data_in_db(&self_clone.new_post, &*db_connection.unwrap());
          let task = &*task_arc.lock().unwrap();
          println!("GOt in here");
          task.notify();
        }).map_err(|_| {
          panic!("The threadpool shut down");
        })
      }));

      self.completed = self.completed + 1;
      Ok(Async::NotReady)
    } else {
      // println!("Worked well",);
      Ok(Async::Ready(()))
    }
  }
}

fn http_handler(req: Request<Body>, db_pool: &Pool) -> BoxFut {
        let mut response = Response::new(Body::empty());

  // println!("Has been connected");
  match (req.method(), req.uri().path()) {
    (&Method::GET, "/") => {
      let put_post = PutPost {
        pool: db_pool.clone(),
        new_post: NewPost {
          title: String::from("Hello world"),
          body: String::from("Hope it works"),
        },
        completed: 0,
      };

      let new_post_insert = put_post
        .and_then(|_| {
          *response.body_mut() = Body::from("We should have put everything properly");
          Ok(response)
        }).or_else(|_| {
          Ok(
            Response::builder()
              .status(StatusCode::NOT_FOUND)
              .body(NOT_FOUND.into())
              .unwrap(),
          )
        });
      return Box::new(new_post_insert);
    }
    _ => {
      *response.status_mut() = StatusCode::NOT_FOUND;
    }
  }

  return Box::new(future::ok(response));
}

fn main() {
  let addr = ([127, 0, 0, 1], 3000).into();
  let db_poll = init_pool();


  let new_service = move || {
    let db_poll = db_poll.clone();
    service_fn(move |req| http_handler(req, &db_poll))
  };

  let server = Server::bind(&addr)
    .serve(new_service)
    .map_err(|e| eprintln!("server error: {}", e));

  println!("Listening on http://{}", addr);
  hyper::rt::run(server);
}
@goriunov

This comment has been minimized.

@goriunov

This comment has been minimized.

goriunov commented Nov 6, 2018

Repo with simplified code: https://github.com/goriunov/rust-test

@goriunov goriunov changed the title Am i missing some thing or is there a bug ? Am i missing some thing or is there is a bug ? Nov 8, 2018

@seanmonstar

This comment has been minimized.

Member

seanmonstar commented Nov 12, 2018

There is no bug, but as was mentioned in the Reddit thread, it could be synchronizing on the stdout lock helps with something else, like the task::current().notify() you have that may cause extra wakeups.

@goriunov

This comment has been minimized.

goriunov commented Nov 13, 2018

@seanmonstar Thank you for your suggestion. I have removed that code long time ago and it still has the same problem could you please check this code. I am not sure what can be wrong (can not solve that problem for a while and no one really explains properly what is going on)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment