Permalink
Browse files

always set "expired" under "synchronized" to avoid race condition.

  • Loading branch information...
Wanli Yang Wanli Yang
Wanli Yang authored and Wanli Yang committed Mar 21, 2011
1 parent 0ca0602 commit 48c08bb5396d51aa0b7bafef27fff4f1f08405c2
Showing with 4 additions and 7 deletions.
  1. +4 −7 finagle-core/src/main/scala/com/twitter/finagle/service/ExpiringService.scala
@@ -25,14 +25,15 @@ class ExpiringService[Req, Rep](
private[this] var idleTimeTask = maxIdleTime map { idleTime => timer.schedule(idleTime.fromNow) { maybeIdleExpire() } }
private[this] var lifeTimeTask = maxLifeTime map { lifeTime => timer.schedule(lifeTime.fromNow) { maybeLifeTimeExpire() } }
- private[this] def maybeExpire() = {
+ private[this] def maybeExpire(force_expire: Boolean) = {
val justExpired = synchronized {
// We check requestCount == 0 here to avoid the race between
// cancellation & running of the timer.
if (!expired && requestCount == 0) {
expired = true
true
} else {
+ if (force_expire) expired = true
false
}
}
@@ -42,15 +43,11 @@ class ExpiringService[Req, Rep](
}
private[this] def maybeIdleExpire() {
- if (maybeExpire()) cancelLifeTimer()
+ if (maybeExpire(false)) cancelLifeTimer()
}
private[this] def maybeLifeTimeExpire() {
- if (maybeExpire()) {
- cancelIdleTimer()
- } else {
- expired = true
- }
+ if (maybeExpire(true)) cancelIdleTimer()
}
// May be overriden to provide your own expiration action.

0 comments on commit 48c08bb

Please sign in to comment.