Skip to content

Commit d990325

Browse files
committed
Add a timeout to cache lookups
This should help ensure that we don't wait *too* long for the cache to respond (for example on an excessively slow network) and time out the server unnecessarily.
1 parent ca2f8fc commit d990325

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/compiler/compiler.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ use std::time::{
5353
Instant,
5454
};
5555
use tempdir::TempDir;
56+
use tokio_core::reactor::{Handle, Timeout};
5657

5758
use errors::*;
5859

@@ -174,6 +175,8 @@ pub enum MissType {
174175
Normal,
175176
/// Cache lookup was overridden, recompilation was forced.
176177
ForcedRecache,
178+
/// Cache took too long to respond.
179+
TimedOut,
177180
}
178181

179182
/// Information about a successful cache write.
@@ -298,7 +301,8 @@ impl Compiler {
298301
parsed_args: &ParsedArguments,
299302
cwd: &str,
300303
cache_control: CacheControl,
301-
pool: &CpuPool)
304+
pool: &CpuPool,
305+
handle: &Handle)
302306
-> SFuture<(CompileResult, process::Output)>
303307
where T: CommandCreatorSync
304308
{
@@ -319,6 +323,7 @@ impl Compiler {
319323
let storage = storage.clone();
320324
let pool = pool.clone();
321325
let creator = creator.clone();
326+
let handle = handle.clone();
322327

323328
Box::new(result.and_then(move |preprocessor_result| -> SFuture<_> {
324329
// If the preprocessor failed, just return that result.
@@ -355,6 +360,20 @@ impl Compiler {
355360
storage.get(&key)
356361
};
357362

363+
// Wait at most a minute for the cache to respond before we forge
364+
// ahead ourselves with a compilation.
365+
let timeout = Duration::new(60, 0);
366+
let timeout = Timeout::new(timeout, &handle).into_future().flatten();
367+
368+
let cache_status = cache_status.map(Some);
369+
let timeout = timeout.map(|_| None).chain_err(|| "timeout error");
370+
let cache_status = cache_status.select(timeout).then(|r| {
371+
match r {
372+
Ok((a, _other)) => Ok(a),
373+
Err((e, _other)) => Err(e),
374+
}
375+
});
376+
358377
Box::new(cache_status.and_then(move |result| {
359378
let duration = start.elapsed();
360379
let pwd = Path::new(&cwd);
@@ -363,7 +382,7 @@ impl Compiler {
363382
.collect::<HashMap<_, _>>();
364383

365384
let miss_type = match result {
366-
Cache::Hit(mut entry) => {
385+
Some(Cache::Hit(mut entry)) => {
367386
debug!("[{}]: Cache hit!", parsed_args.output_file());
368387
let mut stdout = io::Cursor::new(vec!());
369388
let mut stderr = io::Cursor::new(vec!());
@@ -386,14 +405,18 @@ impl Compiler {
386405
(result, output)
387406
})) as SFuture<_>
388407
}
389-
Cache::Miss => {
408+
Some(Cache::Miss) => {
390409
debug!("[{}]: Cache miss!", parsed_args.output_file());
391410
MissType::Normal
392411
}
393-
Cache::Recache => {
412+
Some(Cache::Recache) => {
394413
debug!("[{}]: Cache recache!", parsed_args.output_file());
395414
MissType::ForcedRecache
396415
}
416+
None => {
417+
debug!("[{}]: Cache timed out!", parsed_args.output_file());
418+
MissType::TimedOut
419+
}
397420
};
398421
me.compile(&creator,
399422
preprocessor_result,

src/server.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,8 @@ impl<C> SccacheService<C>
549549
&parsed_arguments,
550550
&cwd,
551551
cache_control,
552-
&self.pool);
552+
&self.pool,
553+
&self.handle);
553554
let me = self.clone();
554555
let task = result.then(move |result| {
555556
let mut res = ServerResponse::new();
@@ -575,6 +576,9 @@ impl<C> SccacheService<C>
575576
stats.cache_misses += 1;
576577
stats.forced_recaches += 1;
577578
}
579+
MissType::TimedOut => {
580+
stats.cache_misses += 1;
581+
}
578582
}
579583
stats.cache_read_miss_duration += duration;
580584
cache_write = Some(future);

0 commit comments

Comments
 (0)