Skip to content

Commit

Permalink
Correct a high CPU issue for empty file source globs (#214)
Browse files Browse the repository at this point in the history
This commit corrects an issue where an empty file source -- one
whose glob matches no files -- will cause the read loops to spin
without end. We predicated our delays on the assumption of matching
some files.

This resolve #197.

Signed-off-by: Brian L. Troutwine <blt@postmates.com>
  • Loading branch information
blt authored Feb 21, 2017
1 parent 5c01200 commit afa92a0
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/source/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ impl FileWatcher {
}

pub fn read_line(&mut self, mut buffer: &mut String) -> io::Result<usize> {
let max_attempts = 5;
let max_attempts = 6;
loop {
let mut attempts = 0;
// read lines
loop {
match self.reader.read_line(&mut buffer) {
Ok(sz) => {
if sz == 0 {
time::delay(1);
time::delay(attempts);
attempts += 1;
if attempts > max_attempts {
break;
Expand Down Expand Up @@ -139,7 +139,12 @@ impl Source for FileServer {
let start = Instant::now();
let mut attempts = 0;
loop {
time::delay(attempts);
if fp_map.is_empty() {
time::delay(9);
break;
} else {
time::delay(attempts);
}
for file in fp_map.values_mut() {
loop {
let mut lines_read = 0;
Expand All @@ -164,6 +169,7 @@ impl Source for FileServer {
io::ErrorKind::TimedOut => {}
_ => trace!("read-line error: {}", e),
}
attempts += 1;
break;
}
}
Expand All @@ -172,9 +178,6 @@ impl Source for FileServer {
for l in lines.drain(..) {
send("file", &mut self.chans, metric::Event::new_log(l));
}
attempts = 0;
} else {
attempts += 1;
}
}
if start.elapsed() >= glob_delay {
Expand Down

0 comments on commit afa92a0

Please sign in to comment.