Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Mongo | The MongoDB database
Mockito | [Mockito](https://site.mockito.org/), the most popular mocking framework for Java unit tests
OCA | Oracle Certified Associate Java SE 8
OCP | Oracle Certified Professional Java SE 8
Reliability | Tips to make production more reliable.
Rest | RESTful API using [Jersey][jersey].
Typesafe Config | [Typesafe Config](https://github.com/lightbend/config), configuration library for JVM languages.
XML | XML serialization, XPath, XSD.
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<module>guava</module>
<module>jetty</module>
<module>protobuf</module>
<module>reliability</module>
</modules>

<dependencyManagement>
Expand Down
32 changes: 32 additions & 0 deletions reliability/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>java-examples-parent</artifactId>
<groupId>io.mincong</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>java-examples-reliability</artifactId>
<name>Java Examples - Reliability</name>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
43 changes: 43 additions & 0 deletions reliability/src/main/java/io/mincong/reliability/Throttler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.mincong.reliability;

import java.util.ArrayList;
import java.util.List;

public class Throttler {

private final int messageLimit;

public Throttler(int messageLimit) {
this.messageLimit = messageLimit;
}

public ThrottleResult throttle(List<String> messages) {
var passed = new ArrayList<String>();
var throttled = new ArrayList<String>();
var quota = messageLimit;

for (var message : messages) {
if (quota > 0) {
passed.add(message);
quota--;
} else {
throttled.add(message);
}
}
/*
* You can also add metrics or logs on the output to improve the
* observability of the system.
*/
return new ThrottleResult(passed, throttled);
}

public static class ThrottleResult {
public final List<String> passed;
public final List<String> throttled;

public ThrottleResult(List<String> passed, List<String> throttled) {
this.passed = passed;
this.throttled = throttled;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.mincong.reliability;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;
import org.junit.jupiter.api.Test;

class ThrottlerTest {

@Test
void throttle_lessMessagesThanLimit() {
var throttler = new Throttler(3);

var result = throttler.throttle(List.of("1", "2"));

assertThat(result.passed).containsExactly("1", "2");
assertThat(result.throttled).isEmpty();
}

@Test
void throttle_moreMessagesThanLimit() {
var throttler = new Throttler(3);

var result = throttler.throttle(List.of("1", "2", "3", "4"));

assertThat(result.passed).containsExactly("1", "2", "3");
assertThat(result.throttled).containsExactly("4");
}
}