##// END OF EJS Templates
copies: compute the exact set of revision to walk...
copies: compute the exact set of revision to walk This change make the code clearer by removing the revision queue. It comes without very noticeable performance impact. However the simpler code will be easier to update in later changesets. revision: large amount; added files: large amount; rename small amount; c3b14617fbd7 9ba6ab77fd29 before: ! wall 1.430082 comb 1.430000 user 1.390000 sys 0.040000 (median of 10) after: ! wall 1.405192 comb 1.410000 user 1.390000 sys 0.020000 (median of 10) revision: large amount; added files: small amount; rename small amount; c3b14617fbd7 f650a9b140d2 before: ! wall 1.971366 comb 1.970000 user 1.950000 sys 0.020000 (median of 10) after: ! wall 1.892541 comb 1.890000 user 1.870000 sys 0.020000 (median of 10) revision: large amount; added files: large amount; rename large amount; 08ea3258278e d9fa043f30c0 before: ! wall 0.252594 comb 0.250000 user 0.240000 sys 0.010000 (median of 38) after: ! wall 0.240075 comb 0.240000 user 0.240000 sys 0.000000 (median of 40) revision: small amount; added files: large amount; rename large amount; df6f7a526b60 a83dc6a2d56f before: ! wall 0.013100 comb 0.010000 user 0.010000 sys 0.000000 (median of 226) after: ! wall 0.013247 comb 0.010000 user 0.010000 sys 0.000000 (median of 223) revision: small amount; added files: large amount; rename small amount; 4aa4e1f8e19a 169138063d63 before: ! wall 0.001633 comb 0.000000 user 0.000000 sys 0.000000 (median of 1000) after: ! wall 0.001670 comb 0.000000 user 0.000000 sys 0.000000 (median of 1000) revision: small amount; added files: small amount; rename small amount; 4bc173b045a6 964879152e2e before: ! wall 0.000078 comb 0.000000 user 0.000000 sys 0.000000 (median of 11984) after: ! wall 0.000119 comb 0.000000 user 0.000000 sys 0.000000 (median of 7982) revision: medium amount; added files: large amount; rename medium amount; c95f1ced15f2 2c68e87c3efe before: ! wall 0.207093 comb 0.210000 user 0.210000 sys 0.000000 (median of 47) after: ! wall 0.201551 comb 0.200000 user 0.200000 sys 0.000000 (median of 48) revision: medium amount; added files: medium amount; rename small amount; d343da0c55a8 d7746d32bf9d before: ! wall 0.038462 comb 0.040000 user 0.040000 sys 0.000000 (median of 100) after: ! wall 0.036578 comb 0.030000 user 0.030000 sys 0.000000 (median of 100) Differential Revision: https://phab.mercurial-scm.org/D7076

File last commit:

r40011:571d8eb3 default
r43593:83bb1e89 default
Show More
runcommand.rs
163 lines | 5.8 KiB | application/rls-services+xml | RustLexer
Yuya Nishihara
rust-chg: add state machine to handle "runcommand" request with cHg extension...
r40011 // Copyright 2018 Yuya Nishihara <yuya@tcha.org>
//
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2 or any later version.
//! Functions to run Mercurial command in cHg-aware command server.
use bytes::Bytes;
use futures::future::IntoFuture;
use futures::{Async, Future, Poll};
use std::io;
use std::mem;
use std::os::unix::io::AsRawFd;
use tokio_hglib::{Client, Connection};
use tokio_hglib::codec::ChannelMessage;
use tokio_hglib::protocol::MessageLoop;
use super::attachio::AttachIo;
use super::message::{self, CommandType};
use super::uihandler::SystemHandler;
enum AsyncS<R, S> {
Ready(R),
NotReady(S),
PollAgain(S),
}
enum CommandState<C, H>
where C: Connection,
H: SystemHandler,
{
Running(MessageLoop<C>, H),
SpawningPager(Client<C>, <H::SpawnPagerResult as IntoFuture>::Future),
AttachingPager(AttachIo<C, io::Stdin, H::PagerStdin, H::PagerStdin>, H),
WaitingSystem(Client<C>, <H::RunSystemResult as IntoFuture>::Future),
Finished,
}
type CommandPoll<C, H> = io::Result<(AsyncS<(Client<C>, H, i32), CommandState<C, H>>)>;
/// Future resolves to `(exit_code, client)`.
#[must_use = "futures do nothing unless polled"]
pub struct ChgRunCommand<C, H>
where C: Connection,
H: SystemHandler,
{
state: CommandState<C, H>,
}
impl<C, H> ChgRunCommand<C, H>
where C: Connection + AsRawFd,
H: SystemHandler,
{
pub fn with_client(client: Client<C>, handler: H, packed_args: Bytes)
-> ChgRunCommand<C, H> {
let msg_loop = MessageLoop::start_with_args(client, b"runcommand", packed_args);
ChgRunCommand {
state: CommandState::Running(msg_loop, handler),
}
}
}
impl<C, H> Future for ChgRunCommand<C, H>
where C: Connection + AsRawFd,
H: SystemHandler,
{
type Item = (Client<C>, H, i32);
type Error = io::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
loop {
let state = mem::replace(&mut self.state, CommandState::Finished);
match state.poll()? {
AsyncS::Ready((client, handler, code)) => {
return Ok(Async::Ready((client, handler, code)));
}
AsyncS::NotReady(newstate) => {
self.state = newstate;
return Ok(Async::NotReady);
}
AsyncS::PollAgain(newstate) => {
self.state = newstate;
}
}
}
}
}
impl<C, H> CommandState<C, H>
where C: Connection + AsRawFd,
H: SystemHandler,
{
fn poll(self) -> CommandPoll<C, H> {
match self {
CommandState::Running(mut msg_loop, handler) => {
if let Async::Ready((client, msg)) = msg_loop.poll()? {
process_message(client, handler, msg)
} else {
Ok(AsyncS::NotReady(CommandState::Running(msg_loop, handler)))
}
}
CommandState::SpawningPager(client, mut fut) => {
if let Async::Ready((handler, pin)) = fut.poll()? {
let fut = AttachIo::with_client(client, io::stdin(), pin, None);
Ok(AsyncS::PollAgain(CommandState::AttachingPager(fut, handler)))
} else {
Ok(AsyncS::NotReady(CommandState::SpawningPager(client, fut)))
}
}
CommandState::AttachingPager(mut fut, handler) => {
if let Async::Ready(client) = fut.poll()? {
let msg_loop = MessageLoop::start(client, b""); // terminator
Ok(AsyncS::PollAgain(CommandState::Running(msg_loop, handler)))
} else {
Ok(AsyncS::NotReady(CommandState::AttachingPager(fut, handler)))
}
}
CommandState::WaitingSystem(client, mut fut) => {
if let Async::Ready((handler, code)) = fut.poll()? {
let data = message::pack_result_code(code);
let msg_loop = MessageLoop::resume_with_data(client, data);
Ok(AsyncS::PollAgain(CommandState::Running(msg_loop, handler)))
} else {
Ok(AsyncS::NotReady(CommandState::WaitingSystem(client, fut)))
}
}
CommandState::Finished => panic!("poll ChgRunCommand after it's done")
}
}
}
fn process_message<C, H>(client: Client<C>, handler: H, msg: ChannelMessage) -> CommandPoll<C, H>
where C: Connection,
H: SystemHandler,
{
match msg {
ChannelMessage::Data(b'r', data) => {
let code = message::parse_result_code(data)?;
Ok(AsyncS::Ready((client, handler, code)))
}
ChannelMessage::Data(..) => {
// just ignores data sent to optional channel
let msg_loop = MessageLoop::resume(client);
Ok(AsyncS::PollAgain(CommandState::Running(msg_loop, handler)))
}
ChannelMessage::InputRequest(..) | ChannelMessage::LineRequest(..) => {
Err(io::Error::new(io::ErrorKind::InvalidData, "unsupported request"))
}
ChannelMessage::SystemRequest(data) => {
let (cmd_type, cmd_spec) = message::parse_command_spec(data)?;
match cmd_type {
CommandType::Pager => {
let fut = handler.spawn_pager(cmd_spec).into_future();
Ok(AsyncS::PollAgain(CommandState::SpawningPager(client, fut)))
}
CommandType::System => {
let fut = handler.run_system(cmd_spec).into_future();
Ok(AsyncS::PollAgain(CommandState::WaitingSystem(client, fut)))
}
}
}
}
}