|
1 | 1 | package com.redhat.hacbs.domainproxy.server; |
2 | 2 |
|
3 | 3 | import static com.redhat.hacbs.domainproxy.common.CommonIOUtil.LOCALHOST; |
4 | | -import static com.redhat.hacbs.domainproxy.common.CommonIOUtil.createChannelToChannelBiDirectionalHandler; |
| 4 | +import static com.redhat.hacbs.domainproxy.common.CommonIOUtil.TIMEOUT_MS; |
| 5 | +import static com.redhat.hacbs.domainproxy.common.CommonIOUtil.channelToChannelBiDirectionalHandler; |
| 6 | +import static java.lang.Thread.currentThread; |
5 | 7 |
|
6 | 8 | import java.io.IOException; |
7 | 9 | import java.net.InetSocketAddress; |
|
16 | 18 | import java.util.Iterator; |
17 | 19 | import java.util.concurrent.ExecutorService; |
18 | 20 | import java.util.concurrent.Executors; |
19 | | -import java.util.concurrent.atomic.AtomicBoolean; |
20 | 21 |
|
21 | 22 | import jakarta.annotation.PostConstruct; |
22 | 23 | import jakarta.annotation.PreDestroy; |
@@ -45,54 +46,52 @@ public class DomainProxyServer { |
45 | 46 | @ConfigProperty(name = "byte-buffer-size") |
46 | 47 | int byteBufferSize; |
47 | 48 |
|
48 | | - private final AtomicBoolean running = new AtomicBoolean(true); |
49 | | - private final ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor(); |
| 49 | + private ExecutorService executor; |
50 | 50 |
|
51 | 51 | @PostConstruct |
52 | 52 | public void start() { |
53 | 53 | Log.infof("Byte buffer size %d", byteBufferSize); // TODO Remove |
54 | | - new Thread(() -> { |
55 | | - Runtime.getRuntime().addShutdownHook(new Thread(() -> { |
56 | | - try { |
57 | | - Files.delete(Path.of(domainSocket)); |
58 | | - } catch (final IOException e) { |
59 | | - Log.errorf(e, "Error deleting domain socket"); |
60 | | - } |
61 | | - })); |
62 | | - try (final ServerSocketChannel serverChannel = ServerSocketChannel.open(StandardProtocolFamily.UNIX); |
63 | | - final Selector selector = Selector.open()) { |
64 | | - serverChannel.bind(UnixDomainSocketAddress.of(domainSocket)); |
65 | | - serverChannel.configureBlocking(false); |
66 | | - serverChannel.register(selector, SelectionKey.OP_ACCEPT); |
67 | | - while (running.get()) { |
68 | | - if (selector.selectNow() > 0) { |
69 | | - final Iterator<SelectionKey> keys = selector.selectedKeys().iterator(); |
70 | | - while (keys.hasNext()) { |
71 | | - final SelectionKey key = keys.next(); |
72 | | - keys.remove(); |
73 | | - if (key.isAcceptable()) { |
74 | | - if (key.channel() instanceof final ServerSocketChannel keyChannel) { |
75 | | - final SocketChannel domainSocketChannel = keyChannel.accept(); |
76 | | - final SocketChannel httpServerChannel = SocketChannel |
77 | | - .open(new InetSocketAddress(LOCALHOST, httpServerPort)); |
78 | | - executor.submit( |
79 | | - createChannelToChannelBiDirectionalHandler(byteBufferSize, httpServerChannel, |
80 | | - domainSocketChannel)); |
81 | | - } |
82 | | - } |
| 54 | + executor = Executors.newVirtualThreadPerTaskExecutor(); |
| 55 | + executor.submit(this::startServer); |
| 56 | + } |
| 57 | + |
| 58 | + private void startServer() { |
| 59 | + try (final ServerSocketChannel serverChannel = ServerSocketChannel.open(StandardProtocolFamily.UNIX); |
| 60 | + final Selector selector = Selector.open()) { |
| 61 | + currentThread().setName("connectionHandler"); |
| 62 | + serverChannel.bind(UnixDomainSocketAddress.of(domainSocket)); |
| 63 | + serverChannel.configureBlocking(false); |
| 64 | + serverChannel.register(selector, SelectionKey.OP_ACCEPT); |
| 65 | + while (!currentThread().isInterrupted()) { |
| 66 | + if (selector.select(TIMEOUT_MS) > 0) { |
| 67 | + final Iterator<SelectionKey> keys = selector.selectedKeys().iterator(); |
| 68 | + while (keys.hasNext()) { |
| 69 | + final SelectionKey key = keys.next(); |
| 70 | + keys.remove(); |
| 71 | + if (key.isAcceptable()) { |
| 72 | + final ServerSocketChannel keyChannel = (ServerSocketChannel) key.channel(); |
| 73 | + final SocketChannel domainSocketChannel = keyChannel.accept(); |
| 74 | + final SocketChannel httpServerChannel = SocketChannel |
| 75 | + .open(new InetSocketAddress(LOCALHOST, httpServerPort)); |
| 76 | + executor.submit(channelToChannelBiDirectionalHandler(byteBufferSize, httpServerChannel, |
| 77 | + domainSocketChannel)); |
83 | 78 | } |
84 | 79 | } |
85 | 80 | } |
86 | | - } catch (final IOException e) { |
87 | | - Log.errorf(e, "Error initialising domain proxy server"); |
88 | 81 | } |
89 | | - Quarkus.asyncExit(); |
90 | | - }).start(); |
| 82 | + } catch (final IOException e) { |
| 83 | + Log.errorf(e, "Error initialising domain proxy server"); |
| 84 | + } |
| 85 | + Quarkus.asyncExit(); |
91 | 86 | } |
92 | 87 |
|
93 | 88 | @PreDestroy |
94 | 89 | public void stop() { |
95 | | - running.set(false); |
96 | | - executor.shutdown(); |
| 90 | + executor.shutdownNow(); |
| 91 | + try { |
| 92 | + Files.deleteIfExists(Path.of(domainSocket)); |
| 93 | + } catch (final IOException e) { |
| 94 | + Log.errorf(e, "Error deleting domain socket"); |
| 95 | + } |
97 | 96 | } |
98 | 97 | } |
0 commit comments