mirror of
https://github.com/moparisthebest/JdbcMapper
synced 2025-02-16 07:00:16 -05:00
Add withJitter(maxJitterMs) to QueryRunner.DelayStrategy
This commit is contained in:
parent
f24d4ed03e
commit
345a4120c8
@ -164,6 +164,12 @@ public class QueryRunner<T extends JdbcMapper> {
|
|||||||
|
|
||||||
public static interface DelayStrategy {
|
public static interface DelayStrategy {
|
||||||
long getDelay(int attempt);
|
long getDelay(int attempt);
|
||||||
|
|
||||||
|
//IFJAVA8_START
|
||||||
|
default DelayStrategy withJitter(final int maxJitterMs) {
|
||||||
|
return QueryRunner.withJitter(this, maxJitterMs);
|
||||||
|
}
|
||||||
|
//IFJAVA8_END
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DelayStrategy exponentialBackoff() {
|
public static DelayStrategy exponentialBackoff() {
|
||||||
@ -231,6 +237,24 @@ public class QueryRunner<T extends JdbcMapper> {
|
|||||||
IFJAVA6_END*/
|
IFJAVA6_END*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static DelayStrategy withJitter(final DelayStrategy toWrap, final int maxJitterMs) {
|
||||||
|
return
|
||||||
|
/*IFJAVA6_START
|
||||||
|
new DelayStrategy() {
|
||||||
|
@Override
|
||||||
|
public long getDelay(final int attempt) {
|
||||||
|
return
|
||||||
|
IFJAVA6_END*/
|
||||||
|
//IFJAVA8_START
|
||||||
|
(attempt) ->
|
||||||
|
//IFJAVA8_END
|
||||||
|
toWrap.getDelay(attempt) + ThreadLocalRandom.current().nextInt(maxJitterMs);
|
||||||
|
/*IFJAVA6_START
|
||||||
|
}
|
||||||
|
};
|
||||||
|
IFJAVA6_END*/
|
||||||
|
}
|
||||||
|
|
||||||
/*IFJAVA6_START
|
/*IFJAVA6_START
|
||||||
// terrible, I know, use java8
|
// terrible, I know, use java8
|
||||||
private static class ThreadLocalRandom {
|
private static class ThreadLocalRandom {
|
||||||
|
@ -19,7 +19,7 @@ public class QueryRunnerTest {
|
|||||||
public Connection create() throws SQLException {
|
public Connection create() throws SQLException {
|
||||||
return QueryMapperTest.getConnection();
|
return QueryMapperTest.getConnection();
|
||||||
}
|
}
|
||||||
}), QueryRunner.fixedDelay(5));
|
}), QueryRunner.withJitter(QueryRunner.fixedDelay(5), 5));
|
||||||
|
|
||||||
private void testPerson(final Person expected, final String query) throws Throwable {
|
private void testPerson(final Person expected, final String query) throws Throwable {
|
||||||
final Person actual =
|
final Person actual =
|
||||||
|
Loading…
Reference in New Issue
Block a user