use of suite.os.SocketUtil.Io in project suite by stupidsing.
the class LoadBalancer method run.
public void run() throws IOException {
BooMutable running = BooMutable.true_();
Thread probe = new Thread(() -> {
while (running.isTrue()) try {
List<String> alives1 = new ArrayList<>();
for (String server : servers) try (Socket socket = new Socket(server, port)) {
alives1.add(server);
} catch (SocketException ex) {
}
alives = alives1;
Thread.sleep(500l);
} catch (Exception ex) {
LogUtil.error(ex);
}
});
Io io = (is, os) -> {
int count = counter.getAndIncrement();
List<String> alives0 = alives;
String server = alives0.get(count % alives0.size());
try (Socket socket = new Socket(server, port)) {
InputStream sis = socket.getInputStream();
OutputStream sos = socket.getOutputStream();
List<Thread> threads = List.of(Copy.streamByThread(is, sos), Copy.streamByThread(sis, os));
Thread_.startJoin(threads);
}
};
try {
probe.start();
new SocketUtil().listenIo(port, io);
} finally {
running.setFalse();
}
}
Aggregations