Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit bf1b92f

Browse files
committed
Added throttler
1 parent 412c979 commit bf1b92f

File tree

4 files changed

+143
-52
lines changed

4 files changed

+143
-52
lines changed
Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,11 @@
11
package com.kttdevelopment.simplehttpserver.handler;
22

3-
import com.kttdevelopment.simplehttpserver.HttpSession;
4-
import com.kttdevelopment.simplehttpserver.SimpleHttpServer;
53
import com.sun.net.httpserver.HttpExchange;
64

7-
import java.util.concurrent.atomic.AtomicInteger;
8-
import java.util.function.Predicate;
5+
abstract class ConnectionThrottler {
96

10-
public class ConnectionThrottler {
7+
abstract boolean addConnection(final HttpExchange exchange);
118

12-
private final Predicate<HttpExchange> contributeToLimit;
13-
private final AtomicInteger connections = new AtomicInteger(0);
14-
15-
private int maxConnections = 0;
16-
17-
public ConnectionThrottler(){
18-
contributeToLimit = (exchange) -> true;
19-
}
20-
21-
public ConnectionThrottler(final Predicate<HttpExchange> counts){
22-
this.contributeToLimit = counts;
23-
}
24-
25-
26-
public synchronized final boolean addConnection(final HttpExchange exchange){
27-
if(!contributeToLimit.test(exchange)){
28-
return true;
29-
}else if(connections.get() + 1 <= maxConnections){
30-
connections.incrementAndGet();
31-
return true;
32-
}
33-
return false;
34-
}
35-
36-
public synchronized final void deleteConnection(final HttpExchange exchange){
37-
if(contributeToLimit.test(exchange))
38-
connections.decrementAndGet();
39-
}
40-
41-
//
42-
43-
44-
@Override
45-
public String toString(){
46-
final StringBuilder OUT = new StringBuilder();
47-
48-
OUT.append("ConnectionThrottler") .append('{');
49-
OUT.append("connections") .append('=') .append(connections) .append(", ");
50-
OUT.append("maxConnections") .append('=') .append(maxConnections);
51-
OUT.append('}');
52-
53-
return OUT.toString();
54-
}
9+
abstract void deleteConnection(final HttpExchange exchange);
5510

5611
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.kttdevelopment.simplehttpserver.handler;
2+
3+
import com.sun.net.httpserver.HttpExchange;
4+
5+
import java.util.concurrent.atomic.AtomicInteger;
6+
import java.util.function.Predicate;
7+
8+
public class ServerThrottler extends ConnectionThrottler {
9+
10+
private final Predicate<HttpExchange> usesLimit;
11+
private final AtomicInteger connections = new AtomicInteger(0);
12+
13+
private int maxConnections = 0;
14+
15+
public ServerThrottler(){
16+
usesLimit = exchange -> true;
17+
}
18+
19+
public ServerThrottler(final int maxConnections){
20+
usesLimit = exchange -> true;
21+
this.maxConnections = maxConnections;
22+
}
23+
24+
public ServerThrottler(final Predicate<HttpExchange> counts){
25+
this.usesLimit = counts;
26+
}
27+
28+
public ServerThrottler(final Predicate<HttpExchange> contributeToLimit, final int maxConnections){
29+
this.usesLimit = contributeToLimit;
30+
this.maxConnections = maxConnections;
31+
}
32+
33+
final boolean addConnection(final HttpExchange exchange){
34+
if(!usesLimit.test(exchange)){
35+
return true;
36+
}else{
37+
synchronized(this){
38+
if(connections.get() + 1 <= maxConnections){
39+
connections.incrementAndGet();
40+
return true;
41+
}
42+
}
43+
}
44+
return false;
45+
}
46+
47+
final void deleteConnection(final HttpExchange exchange){
48+
if(usesLimit.test(exchange))
49+
synchronized(this){
50+
connections.decrementAndGet();
51+
}
52+
}
53+
54+
//
55+
56+
@Override
57+
public String toString(){
58+
final StringBuilder OUT = new StringBuilder();
59+
60+
OUT.append("ConnectionThrottler") .append('{');
61+
OUT.append("connections") .append('=') .append(connections) .append(", ");
62+
OUT.append("maxConnections") .append('=') .append(maxConnections);
63+
OUT.append('}');
64+
65+
return OUT.toString();
66+
}
67+
68+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.kttdevelopment.simplehttpserver.handler;
2+
3+
import com.kttdevelopment.simplehttpserver.HttpSession;
4+
import com.kttdevelopment.simplehttpserver.SimpleHttpServer;
5+
import com.sun.net.httpserver.HttpExchange;
6+
7+
import java.util.*;
8+
import java.util.concurrent.atomic.AtomicInteger;
9+
import java.util.function.Predicate;
10+
11+
public class SessionThrottler extends ConnectionThrottler {
12+
13+
private final SimpleHttpServer server;
14+
15+
private final Predicate<HttpSession> usesLimit;
16+
private final Map<HttpSession, AtomicInteger> sessions = Collections.synchronizedMap(new HashMap<>());
17+
18+
private int maxConnections = 0;
19+
20+
public SessionThrottler(final SimpleHttpServer server){
21+
this.server = server;
22+
usesLimit = (exchange) -> true;
23+
}
24+
25+
public SessionThrottler(final SimpleHttpServer server, final int maxConnections){
26+
this.server = server;
27+
usesLimit = (exchange) -> true;
28+
this.maxConnections = maxConnections;
29+
}
30+
31+
public SessionThrottler(final SimpleHttpServer server, final Predicate<HttpSession> usesLimit){
32+
this.server = server;
33+
this.usesLimit = usesLimit;
34+
}
35+
36+
public SessionThrottler(final SimpleHttpServer server, final Predicate<HttpSession> usesLimit, final int maxConnections){
37+
this.server = server;
38+
this.usesLimit = usesLimit;
39+
this.maxConnections = maxConnections;
40+
}
41+
42+
@Override
43+
final boolean addConnection(final HttpExchange exchange){
44+
final HttpSession session = server.getHttpSession(exchange);
45+
if(!usesLimit.test(session)){
46+
return true;
47+
}else{
48+
synchronized(this){
49+
final AtomicInteger conn = sessions.get(session);
50+
if(conn.get() + 1 <= maxConnections){
51+
conn.incrementAndGet();
52+
return true;
53+
}
54+
}
55+
}
56+
return false;
57+
}
58+
59+
@Override
60+
final void deleteConnection(final HttpExchange exchange){
61+
final HttpSession session = server.getHttpSession(exchange);
62+
if(usesLimit.test(session))
63+
synchronized(this){
64+
sessions.get(session).decrementAndGet();
65+
}
66+
}
67+
68+
}

src/main/java/com/kttdevelopment/simplehttpserver/handler/ThrottledHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77

88
public class ThrottledHandler implements HttpHandler {
99

10-
private final HttpHandler handler;
11-
private final ConnectionThrottler throttler;
10+
private final HttpHandler handler;
11+
private final ServerThrottler throttler;
1212

13-
public ThrottledHandler(final HttpHandler handler, final ConnectionThrottler throttler){
13+
public ThrottledHandler(final HttpHandler handler, final ServerThrottler throttler){
1414
this.handler = handler;
1515
this.throttler = throttler;
1616
}
1717

1818
@Override
19-
public synchronized final void handle(final HttpExchange exchange) throws IOException{
19+
public final void handle(final HttpExchange exchange) throws IOException{
2020
if(throttler.addConnection(exchange)){
2121
handler.handle(exchange);
2222
throttler.deleteConnection(exchange);

0 commit comments

Comments
 (0)