Skip to content

Commit

Permalink
max-connection-limit implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
jveverka committed Dec 9, 2021
1 parent 4986e13 commit aef87a6
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 11 deletions.
5 changes: 4 additions & 1 deletion examples/tcp-proxy/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ plugins {
id 'application'
}

sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17

group = 'one.microproject.tcp.server'
version = '1.0.0'
mainClassName = 'one.microproject.tcp.server.Main'
Expand All @@ -14,4 +17,4 @@ dependencies {
implementation 'com.fasterxml.jackson.core:jackson-core:2.13.0'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.0'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.13.0'
}
}
12 changes: 12 additions & 0 deletions examples/tcp-proxy/scripts/tcp-proxy-start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

export JAVA_HOME=/opt/tcp-proxy/jdk1.8.0_251
export PATH=$JAAVA_HOME/bin:$PATH

LOG_FILE=/opt/tcp-proxy/tcp-proxy.log
CONFIG=/opt/tcp-proxy/tcp-proxy-config.json

mv $LOG_FILE $LOG_FILE.old

/opt/tcp-proxy/tcp-proxy-1.0.0/bin/tcp-proxy $CONFIG > $LOG_FILE 2>&1

3 changes: 3 additions & 0 deletions examples/tcp-proxy/scripts/tcp-proxy-stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

killall java
20 changes: 20 additions & 0 deletions examples/tcp-proxy/scripts/tcp-proxy.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[Unit]
Description=TCP Proxy Service
After=network.target
After=systemd-user-sessions.service
After=network-online.target

[Service]
User=pi
Type=simple
WorkingDirectory=/opt/tcp-proxy
ExecStart=/opt/tcp-proxy/tcp-proxy-start.sh
ExecStop=/opt/tcp-proxy/tcp-proxy-stop.sh
TimeoutSec=30
Restart=on-failure
RestartSec=30
StartLimitInterval=350
StartLimitBurst=10

[Install]
WantedBy=multi-user.target
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void main(String[] args) throws IOException {
Configuration configuration = mapper.readValue(new File(args[0]), Configuration.class);
LOG.info("Starting TCP Proxy id={} name={} proxies={}", configuration.id(), configuration.name(), configuration.proxies().size());
configuration.proxies().forEach(c -> {
LOG.info("Proxy Config: {}:{} -> {}:{}", c.serverHost(), c.serverPort(), c.targetHost(), c.targetPort());
LOG.info("Proxy Config: {}:{} -> {}:{} maxConnections={}", c.serverHost(), c.serverPort(), c.targetHost(), c.targetPort(), c.maxConnections());
TCPProxyImpl tcpProxy = new TCPProxyImpl(c);
try {
tcpProxy.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public interface ConnectionRegistry {

void register(ActiveConnection activeConnection);

int getActiveConnections();

void unregister(String id);

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package one.microproject.tcp.server.impl;

public record ProxyConfiguration(String serverHost, Integer serverPort, String targetHost, Integer targetPort) {
public record ProxyConfiguration(String serverHost, Integer serverPort, String targetHost, Integer targetPort, Integer maxConnections) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class TCPProxyImpl implements TcpProxy, ConnectionRegistry {
private final Integer serverPort;
private final String targetHost;
private final Integer targetPort;
private final Integer maxConnections;
private final Map<String, ActiveConnection> activeConnections;

private TcpMain tcpMain;
Expand All @@ -31,17 +32,21 @@ public TCPProxyImpl(ProxyConfiguration configuration) {
this.serverPort = configuration.serverPort();
this.targetHost = configuration.targetHost();
this.targetPort = configuration.targetPort();
this.maxConnections = configuration.maxConnections();
this.activeConnections = new ConcurrentHashMap<>();
}

@Override
public void start() throws IOException {
LOG.info("Starting TCP proxy ...");
this.processors = Executors.newFixedThreadPool(8);
ServerSocket serverSocket = new ServerSocket(serverPort, 10, InetAddress.getByName(serverHost));
this.tcpMain = new TcpMain(this, serverSocket, processors, targetHost, targetPort);
processors.submit(tcpMain);
LOG.info("TCP proxy started.");
int threadPoolSize = 1 + (maxConnections*2);
LOG.info("Starting internal threadpool size={}", threadPoolSize);
this.processors = Executors.newFixedThreadPool(threadPoolSize);
try (ServerSocket serverSocket = new ServerSocket(serverPort, maxConnections, InetAddress.getByName(serverHost))) {
this.tcpMain = new TcpMain(this, serverSocket, processors, targetHost, targetPort, maxConnections);
processors.submit(tcpMain);
LOG.info("TCP proxy started.");
}
}

@Override
Expand All @@ -66,6 +71,11 @@ public synchronized void register(ActiveConnection activeConnection) {
LOG.info("Active connections: {}", activeConnections.size());
}

@Override
public synchronized int getActiveConnections() {
return activeConnections.size();
}

@Override
public synchronized void unregister(String id) {
activeConnections.remove(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ public class TcpMain implements Runnable, AutoCloseable {
private final ExecutorService processors;
private final String targetHost;
private final Integer targetPort;
private final Integer maxConnections;

private boolean active;

public TcpMain(ConnectionRegistry connectionRegistry, ServerSocket serverSocket,
ExecutorService processors, String targetHost, Integer targetPort) {
ExecutorService processors, String targetHost, Integer targetPort, Integer maxConnections) {
this.connectionRegistry = connectionRegistry;
this.processors = processors;
this.serverSocket = serverSocket;
this.targetHost = targetHost;
this.targetPort = targetPort;
this.active = true;
this.maxConnections = maxConnections;
}

@Override
Expand All @@ -37,6 +39,12 @@ public void run() {
while (active) {
LOG.info("Waiting for incoming TCP connections ...");
Socket clientSocket = serverSocket.accept();
LOG.info("TCP connection accepted !");
if (connectionRegistry.getActiveConnections() >= maxConnections) {
LOG.info("Max connections {} per server exceeded, closing connection {}:{} !", maxConnections, clientSocket.getRemoteSocketAddress(), clientSocket.getPort());
clientSocket.close();
continue;
}
String id = UUID.randomUUID().toString();
LOG.info("Connection id={} from {}:{} to {}:{} in progress ...", id, clientSocket.getRemoteSocketAddress(), clientSocket.getPort(), targetHost, targetPort);
try (Socket socket = new Socket(targetHost, targetPort)) {
Expand All @@ -50,8 +58,13 @@ public void run() {
}
}
} catch (IOException e) {
LOG.info("TcpMain IOException");
LOG.error("TcpMain IOException: ", e);
this.active = false;
try {
close();
} catch (Exception ex) {
LOG.error("TcpMain Exception: ", e);
}
}
}

Expand All @@ -60,6 +73,7 @@ public void close() throws Exception {
LOG.info("Closing TCP Server ...");
this.active = false;
this.serverSocket.close();
this.processors.shutdown();
}

}
3 changes: 2 additions & 1 deletion examples/tcp-proxy/tcp-proxy-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"serverHost": "0.0.0.0",
"serverPort": 4567,
"targetHost": "192.168.44.101",
"targetPort": 22
"targetPort": 22,
"maxConnections": 2
}
]
}

0 comments on commit aef87a6

Please sign in to comment.