-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1235 from gpradeepkrishna/feature/add-ability-to-…
…configure-failure-rate-threshold-for-circuit-breakers add ability to configure failure rate threshold in circuit breakers
- Loading branch information
Showing
7 changed files
with
120 additions
and
1 deletion.
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
51 changes: 51 additions & 0 deletions
51
...g-boot-autoconfigure/src/main/java/org/zalando/riptide/autoconfigure/RatioInTimeSpan.java
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,51 @@ | ||
package org.zalando.riptide.autoconfigure; | ||
|
||
import lombok.Getter; | ||
|
||
import java.time.Duration; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
@Getter | ||
final class RatioInTimeSpan { | ||
|
||
private static final Pattern PATTERN = Pattern.compile("(?<ratio>.*)? in (?<timespan>.*)?"); | ||
|
||
private final Ratio ratio; | ||
private final TimeSpan timeSpan; | ||
|
||
// used by SnakeYAML | ||
@SuppressWarnings("unused") | ||
public RatioInTimeSpan(String value) { | ||
this(RatioInTimeSpan.valueOf(value)); | ||
} | ||
|
||
private RatioInTimeSpan(Ratio ratio, TimeSpan timeSpan) { | ||
this.ratio = ratio; | ||
this.timeSpan = timeSpan; | ||
} | ||
|
||
private RatioInTimeSpan(RatioInTimeSpan ratioInTimeSpan) { | ||
this(ratioInTimeSpan.ratio, ratioInTimeSpan.timeSpan); | ||
} | ||
|
||
void applyTo(RatioInTimeSpanConsumer consumer) { | ||
consumer.accept(ratio.getAmount(), ratio.getTotal(), timeSpan.toDuration()); | ||
} | ||
|
||
public static RatioInTimeSpan valueOf(String value) { | ||
final Matcher matcher = PATTERN.matcher(value); | ||
|
||
if (!matcher.matches()) { | ||
throw new IllegalArgumentException("'" + value + "' is not a valid ratio in timespan"); | ||
} | ||
|
||
final Ratio ratio = Ratio.valueOf(matcher.group("ratio")); | ||
final TimeSpan timeSpan = TimeSpan.valueOf(matcher.group("timespan")); | ||
return new RatioInTimeSpan(ratio, timeSpan); | ||
} | ||
|
||
interface RatioInTimeSpanConsumer { | ||
void accept(int amount, int total, Duration duration); | ||
} | ||
} |
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
55 changes: 55 additions & 0 deletions
55
...ot-autoconfigure/src/test/java/org/zalando/riptide/autoconfigure/RatioInTimeSpanTest.java
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,55 @@ | ||
package org.zalando.riptide.autoconfigure; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.Duration; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.hasEntry; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
final class RatioInTimeSpanTest { | ||
|
||
@Test | ||
void shouldParseRatioInTimeSpan() { | ||
final RatioInTimeSpan ratioInTimeSpan = RatioInTimeSpan.valueOf("3 out of 5 in 5 seconds"); | ||
|
||
assertThat(ratioInTimeSpan.getRatio().getAmount(), is(3)); | ||
assertThat(ratioInTimeSpan.getRatio().getTotal(), is(5)); | ||
assertThat(ratioInTimeSpan.getTimeSpan().toDuration(), is(Duration.ofSeconds(5))); | ||
} | ||
|
||
@Test | ||
void shouldParseUsingConstructor() { | ||
final RatioInTimeSpan ratioInTimeSpan = new RatioInTimeSpan("4 / 10 in 15 minutes"); | ||
|
||
assertThat(ratioInTimeSpan.getRatio().getAmount(), is(4)); | ||
assertThat(ratioInTimeSpan.getRatio().getTotal(), is(10)); | ||
assertThat(ratioInTimeSpan.getTimeSpan().toDuration(), is(Duration.ofMinutes(15))); | ||
} | ||
|
||
@Test | ||
void shouldApplyTo() { | ||
final RatioInTimeSpan ratioInTimeSpan = RatioInTimeSpan.valueOf("4 in 15 minutes"); | ||
final Map<String, Object> consumerParameters = new HashMap<>(); | ||
|
||
ratioInTimeSpan.applyTo((amount, total, duration) -> { | ||
consumerParameters.put("amount", amount); | ||
consumerParameters.put("total", total); | ||
consumerParameters.put("duration", duration); | ||
}); | ||
|
||
assertThat(consumerParameters, hasEntry("amount", 4)); | ||
assertThat(consumerParameters, hasEntry("total", 4)); | ||
assertThat(consumerParameters, hasEntry("duration", Duration.ofMinutes(15))); | ||
} | ||
|
||
@Test | ||
void shouldFailOnUnsupportedFormat() { | ||
assertThrows(IllegalArgumentException.class, () -> RatioInTimeSpan.valueOf("a lot out of many")); | ||
} | ||
|
||
} |
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