|
32 | 32 | import java.util.concurrent.Executor;
|
33 | 33 | import java.util.concurrent.ExecutorService;
|
34 | 34 | import java.util.concurrent.Executors;
|
| 35 | +import java.util.concurrent.RejectedExecutionException; |
35 | 36 | import java.util.concurrent.atomic.AtomicBoolean;
|
36 | 37 | import java.util.function.BooleanSupplier;
|
37 | 38 |
|
@@ -75,16 +76,20 @@ public void onOpen(final WebSocket webSocket) {
|
75 | 76 | if (e instanceof InterruptedException) {
|
76 | 77 | Thread.currentThread().interrupt();
|
77 | 78 | }
|
78 |
| - logger.debug("Error while writing client data"); |
79 |
| - if (alive.get()) { |
80 |
| - clientThrowables.add(e); |
81 |
| - closeBothWays(webSocket, 1001, "Client error"); |
82 |
| - } |
| 79 | + clientError(webSocket, "writing client data", e); |
83 | 80 | }
|
84 | 81 | });
|
85 | 82 | }
|
86 | 83 | }
|
87 | 84 |
|
| 85 | + private void clientError(final WebSocket webSocket, String operation, Exception e) { |
| 86 | + if (alive.get()) { |
| 87 | + logger.debug("Error while " + operation, e); |
| 88 | + clientThrowables.add(e); |
| 89 | + closeBothWays(webSocket, 1001, "Client error"); |
| 90 | + } |
| 91 | + } |
| 92 | + |
88 | 93 | @Override
|
89 | 94 | public void onMessage(WebSocket webSocket, String text) {
|
90 | 95 | logger.debug("{}: onMessage(String)", LOG_PREFIX);
|
@@ -125,27 +130,27 @@ public void onMessage(WebSocket webSocket, ByteBuffer buffer) {
|
125 | 130 | } else {
|
126 | 131 | // Data
|
127 | 132 | if (out != null) {
|
128 |
| - serialExecutor.execute(() -> { |
129 |
| - try { |
130 |
| - while (buffer.hasRemaining()) { |
131 |
| - int written = out.write(buffer); // channel byte already skipped |
132 |
| - if (written == 0) { |
133 |
| - // out is non-blocking, prevent a busy loop |
134 |
| - Thread.sleep(50); |
| 133 | + try { |
| 134 | + serialExecutor.execute(() -> { |
| 135 | + try { |
| 136 | + while (buffer.hasRemaining()) { |
| 137 | + int written = out.write(buffer); // channel byte already skipped |
| 138 | + if (written == 0) { |
| 139 | + // out is non-blocking, prevent a busy loop |
| 140 | + Thread.sleep(50); |
| 141 | + } |
135 | 142 | }
|
| 143 | + webSocket.request(); |
| 144 | + } catch (IOException | InterruptedException e) { |
| 145 | + if (e instanceof InterruptedException) { |
| 146 | + Thread.currentThread().interrupt(); |
| 147 | + } |
| 148 | + clientError(webSocket, "forwarding data to the client", e); |
136 | 149 | }
|
137 |
| - webSocket.request(); |
138 |
| - } catch (IOException | InterruptedException e) { |
139 |
| - if (e instanceof InterruptedException) { |
140 |
| - Thread.currentThread().interrupt(); |
141 |
| - } |
142 |
| - if (alive.get()) { |
143 |
| - clientThrowables.add(e); |
144 |
| - logger.debug("Error while forwarding data to the client", e); |
145 |
| - closeBothWays(webSocket, 1002, PROTOCOL_ERROR); |
146 |
| - } |
147 |
| - } |
148 |
| - }); |
| 150 | + }); |
| 151 | + } catch (RejectedExecutionException e) { |
| 152 | + // just ignore |
| 153 | + } |
149 | 154 | }
|
150 | 155 | }
|
151 | 156 | }
|
|
0 commit comments