-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Network recovery strategy (handlers) - linear, exponential (#23)
* Network recovery strategy (handlers) - linear, exponential * Readme update
- Loading branch information
Showing
8 changed files
with
129 additions
and
8 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
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
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
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
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
26 changes: 26 additions & 0 deletions
26
core/src/main/scala/com/avast/clients/rabbitmq/RecoveryDelayHandlers.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,26 @@ | ||
package com.avast.clients.rabbitmq | ||
import java.time.Duration | ||
|
||
import com.rabbitmq.client.RecoveryDelayHandler | ||
|
||
private[rabbitmq] object RecoveryDelayHandlers { | ||
case class Linear(delay: Duration, period: Duration) extends RecoveryDelayHandler { | ||
override def getDelay(recoveryAttempts: Int): Long = { | ||
if (recoveryAttempts == 0) delay.toMillis else period.toMillis | ||
} | ||
} | ||
|
||
case class Exponential(delay: Duration, period: Duration, factor: Double, maxLength: Duration) extends RecoveryDelayHandler { | ||
private val maxMillis = maxLength.toMillis | ||
|
||
override def getDelay(recoveryAttempts: Int): Long = { | ||
if (recoveryAttempts == 0) delay.toMillis | ||
else { | ||
math.min( | ||
maxMillis, | ||
(period.toMillis * math.pow(factor, recoveryAttempts - 1)).toLong | ||
) | ||
} | ||
} | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
core/src/test/scala/com/avast/clients/rabbitmq/RecoveryDelayHandlersTest.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,30 @@ | ||
package com.avast.clients.rabbitmq | ||
import java.time.Duration | ||
|
||
import scala.util.Random | ||
|
||
class RecoveryDelayHandlersTest extends TestBase { | ||
test("linear") { | ||
val rdh = RecoveryDelayHandlers.Linear(Duration.ofMillis(10), Duration.ofMillis(42)) | ||
|
||
assertResult(10)(rdh.getDelay(0)) | ||
|
||
for (_ <- 1 to 200) { | ||
assertResult(42)(rdh.getDelay(Random.nextInt() + 1)) | ||
} | ||
} | ||
|
||
test("exponential") { | ||
val rdh = RecoveryDelayHandlers.Exponential(Duration.ofMillis(1), Duration.ofMillis(5), 2.0, Duration.ofMillis(42)) | ||
|
||
assertResult(1)(rdh.getDelay(0)) | ||
|
||
assertResult(5)(rdh.getDelay(1)) | ||
assertResult(10)(rdh.getDelay(2)) | ||
assertResult(20)(rdh.getDelay(3)) | ||
assertResult(40)(rdh.getDelay(4)) | ||
assertResult(42)(rdh.getDelay(5)) | ||
assertResult(42)(rdh.getDelay(6)) | ||
assertResult(42)(rdh.getDelay(7)) | ||
} | ||
} |