Skip to content

Commit decc39a

Browse files
authored
Create new module: Reliability (#154)
* Create new module: Reliability * Add Throttler
1 parent 356c0dc commit decc39a

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Mongo | The MongoDB database
3434
Mockito | [Mockito](https://site.mockito.org/), the most popular mocking framework for Java unit tests
3535
OCA | Oracle Certified Associate Java SE 8
3636
OCP | Oracle Certified Professional Java SE 8
37+
Reliability | Tips to make production more reliable.
3738
Rest | RESTful API using [Jersey][jersey].
3839
Typesafe Config | [Typesafe Config](https://github.com/lightbend/config), configuration library for JVM languages.
3940
XML | XML serialization, XPath, XSD.

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<module>guava</module>
5959
<module>jetty</module>
6060
<module>protobuf</module>
61+
<module>reliability</module>
6162
</modules>
6263

6364
<dependencyManagement>

reliability/pom.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-examples-parent</artifactId>
7+
<groupId>io.mincong</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>java-examples-reliability</artifactId>
13+
<name>Java Examples - Reliability</name>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.junit.jupiter</groupId>
18+
<artifactId>junit-jupiter-api</artifactId>
19+
<scope>test</scope>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.junit.jupiter</groupId>
23+
<artifactId>junit-jupiter-engine</artifactId>
24+
<scope>test</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.assertj</groupId>
28+
<artifactId>assertj-core</artifactId>
29+
<scope>test</scope>
30+
</dependency>
31+
</dependencies>
32+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.mincong.reliability;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Throttler {
7+
8+
private final int messageLimit;
9+
10+
public Throttler(int messageLimit) {
11+
this.messageLimit = messageLimit;
12+
}
13+
14+
public ThrottleResult throttle(List<String> messages) {
15+
var passed = new ArrayList<String>();
16+
var throttled = new ArrayList<String>();
17+
var quota = messageLimit;
18+
19+
for (var message : messages) {
20+
if (quota > 0) {
21+
passed.add(message);
22+
quota--;
23+
} else {
24+
throttled.add(message);
25+
}
26+
}
27+
/*
28+
* You can also add metrics or logs on the output to improve the
29+
* observability of the system.
30+
*/
31+
return new ThrottleResult(passed, throttled);
32+
}
33+
34+
public static class ThrottleResult {
35+
public final List<String> passed;
36+
public final List<String> throttled;
37+
38+
public ThrottleResult(List<String> passed, List<String> throttled) {
39+
this.passed = passed;
40+
this.throttled = throttled;
41+
}
42+
}
43+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.mincong.reliability;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.List;
6+
import org.junit.jupiter.api.Test;
7+
8+
class ThrottlerTest {
9+
10+
@Test
11+
void throttle_lessMessagesThanLimit() {
12+
var throttler = new Throttler(3);
13+
14+
var result = throttler.throttle(List.of("1", "2"));
15+
16+
assertThat(result.passed).containsExactly("1", "2");
17+
assertThat(result.throttled).isEmpty();
18+
}
19+
20+
@Test
21+
void throttle_moreMessagesThanLimit() {
22+
var throttler = new Throttler(3);
23+
24+
var result = throttler.throttle(List.of("1", "2", "3", "4"));
25+
26+
assertThat(result.passed).containsExactly("1", "2", "3");
27+
assertThat(result.throttled).containsExactly("4");
28+
}
29+
}

0 commit comments

Comments
 (0)