Set timeout in REPL

This commit is contained in:
Shunsuke Shibayama 2022-08-17 12:02:56 +09:00
parent 34a25290aa
commit 1e3819b416
3 changed files with 17 additions and 4 deletions

View file

@ -130,6 +130,7 @@ pub struct ErgConfig {
pub opt_level: u8, pub opt_level: u8,
pub dump_as_pyc: bool, pub dump_as_pyc: bool,
pub python_ver: Option<u32>, pub python_ver: Option<u32>,
pub py_server_timeout: u64,
pub input: Input, pub input: Input,
pub module: &'static str, pub module: &'static str,
/// verbosity level for system messages. /// verbosity level for system messages.
@ -142,7 +143,7 @@ pub struct ErgConfig {
impl Default for ErgConfig { impl Default for ErgConfig {
#[inline] #[inline]
fn default() -> Self { fn default() -> Self {
Self::new("exec", 1, false, None, Input::REPL, "<module>", 2) Self::new("exec", 1, false, None, 10, Input::REPL, "<module>", 2)
} }
} }
@ -152,6 +153,7 @@ impl ErgConfig {
opt_level: u8, opt_level: u8,
dump_as_pyc: bool, dump_as_pyc: bool,
python_ver: Option<u32>, python_ver: Option<u32>,
py_server_timeout: u64,
input: Input, input: Input,
module: &'static str, module: &'static str,
verbose: u8, verbose: u8,
@ -161,6 +163,7 @@ impl ErgConfig {
opt_level, opt_level,
dump_as_pyc, dump_as_pyc,
python_ver, python_ver,
py_server_timeout,
input, input,
module, module,
verbose, verbose,
@ -203,6 +206,9 @@ impl ErgConfig {
"-p" | "--py-ver" | "--python-version" => { "-p" | "--py-ver" | "--python-version" => {
cfg.python_ver = Some(args.next().unwrap().parse::<u32>().unwrap()); cfg.python_ver = Some(args.next().unwrap().parse::<u32>().unwrap());
} }
"--py-server-timeout" => {
cfg.py_server_timeout = args.next().unwrap().parse::<u64>().unwrap();
}
"--verbose" => { "--verbose" => {
cfg.verbose = args.next().unwrap().parse::<u8>().unwrap(); cfg.verbose = args.next().unwrap().parse::<u8>().unwrap();
} }

View file

@ -127,7 +127,7 @@ mod tests {
#[test] #[test]
fn test_parser1() -> Result<(), ParserRunnerErrors> { fn test_parser1() -> Result<(), ParserRunnerErrors> {
let input = Input::File(FILE1.into()); let input = Input::File(FILE1.into());
let cfg = ErgConfig::new("exec", 1, false, None, input.clone(), "<module>", 2); let cfg = ErgConfig::new("exec", 1, false, None, 100, input.clone(), "<module>", 2);
let lexer = Lexer::new(input.clone()); let lexer = Lexer::new(input.clone());
let mut parser = ParserRunner::new(cfg); let mut parser = ParserRunner::new(cfg);
match parser.parse( match parser.parse(

View file

@ -35,7 +35,10 @@ impl Runnable for DummyVM {
let addr = format!("{repl_server_ip}:{repl_server_port}"); let addr = format!("{repl_server_ip}:{repl_server_port}");
loop { loop {
match TcpStream::connect(&addr) { match TcpStream::connect(&addr) {
Ok(stream) => break Some(stream), Ok(stream) => {
stream.set_read_timeout(Some(Duration::from_secs(cfg.py_server_timeout))).unwrap();
break Some(stream)
},
Err(_) => { Err(_) => {
println!("Retrying to connect to the REPL server..."); println!("Retrying to connect to the REPL server...");
sleep(Duration::from_millis(500)); sleep(Duration::from_millis(500));
@ -104,11 +107,15 @@ impl Runnable for DummyVM {
s.to_string() s.to_string()
} }
Result::Err(e) => { Result::Err(e) => {
self.finish();
panic!("{}", format!("Read error: {e}")); panic!("{}", format!("Read error: {e}"));
} }
} }
} }
Result::Err(e) => panic!("{}", format!("Sending error: {e}")), Result::Err(e) => {
self.finish();
panic!("{}", format!("Sending error: {e}"))
},
}; };
if res.ends_with("None") { if res.ends_with("None") {
res.truncate(res.len() - 5); res.truncate(res.len() - 5);