-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
73 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.twitter.querulous | ||
|
||
import com.twitter.xrayspecs.{Time, Duration} | ||
import com.twitter.xrayspecs.TimeConversions._ | ||
import java.sql.{SQLException, SQLIntegrityConstraintViolationException} | ||
|
||
|
||
trait AutoDisabler { | ||
protected val disableErrorCount: Int | ||
protected val disableDuration: Duration | ||
|
||
private var disabledUntil: Time = Time.never | ||
private var consecutiveErrors = 0 | ||
|
||
protected def throwIfDisabled() { | ||
synchronized { | ||
if (Time.now < disabledUntil) { | ||
throw new SQLException("Server is temporarily disabled") | ||
} | ||
} | ||
} | ||
|
||
protected def noteOperationOutcome(success: Boolean) { | ||
synchronized { | ||
if (success) { | ||
consecutiveErrors = 0 | ||
} else { | ||
consecutiveErrors += 1 | ||
if (consecutiveErrors >= disableErrorCount) { | ||
disabledUntil = disableDuration.fromNow | ||
} | ||
} | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/scala/com/twitter/querulous/database/AutoDisablingDatabase.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.twitter.querulous.database | ||
|
||
import com.twitter.xrayspecs.Duration | ||
import com.twitter.xrayspecs.TimeConversions._ | ||
import java.sql.{Connection, SQLException, SQLIntegrityConstraintViolationException} | ||
|
||
|
||
class AutoDisablingDatabaseFactory(databaseFactory: DatabaseFactory, disableErrorCount: Int, disableDuration: Duration) extends DatabaseFactory { | ||
def apply(dbhosts: List[String], dbname: String, username: String, password: String) = { | ||
new AutoDisablingDatabase(databaseFactory(dbhosts, dbname, username, password), disableErrorCount, disableDuration) | ||
} | ||
|
||
def apply(dbhosts: List[String], username: String, password: String) = { | ||
new AutoDisablingDatabase(databaseFactory(dbhosts, username, password), disableErrorCount, disableDuration) | ||
} | ||
} | ||
|
||
class AutoDisablingDatabase(database: Database, protected val disableErrorCount: Int, protected val disableDuration: Duration) extends Database with AutoDisabler { | ||
def open() = { | ||
throwIfDisabled() | ||
try { | ||
val rv = database.open() | ||
noteOperationOutcome(true) | ||
rv | ||
} catch { | ||
case e: SQLException => | ||
noteOperationOutcome(false) | ||
throw e | ||
case e: Exception => | ||
throw e | ||
} | ||
} | ||
|
||
def close(connection: Connection) { database.close(connection) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters