Skip to content

Commit 0e4f39c

Browse files
authored
Run loops check the interrupted flag (#1215)
* Run loops check the interrupted flag * Run loops check the interrupted flag
1 parent 4cf29d4 commit 0e4f39c

File tree

7 files changed

+16
-28
lines changed

7 files changed

+16
-28
lines changed

src/main/java/io/nats/client/impl/MessageManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public void shutdown() {
149149

150150
@Override
151151
public void run() {
152-
if (alive.get()) {
152+
if (alive.get() && !Thread.interrupted()) {
153153
long sinceLast = System.currentTimeMillis() - lastMsgReceived.get();
154154
if (alive.get() && sinceLast > alarmPeriodSetting) {
155155
handleHeartbeatError();

src/main/java/io/nats/client/impl/NatsConnection.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,20 +2178,17 @@ public CompletableFuture<Boolean> drain(Duration timeout) throws TimeoutExceptio
21782178
// Wait for the timeout or the pending count to go to 0
21792179
executor.submit(() -> {
21802180
try {
2181-
Instant now = Instant.now();
2182-
2183-
while (timeout == null || timeout.equals(Duration.ZERO)
2184-
|| Duration.between(start, now).compareTo(timeout) < 0) {
2181+
long stop = (timeout == null || timeout.equals(Duration.ZERO))
2182+
? Long.MAX_VALUE
2183+
: System.nanoTime() + timeout.toNanos();
2184+
while (System.nanoTime() < stop && !Thread.interrupted())
2185+
{
21852186
consumers.removeIf(NatsConsumer::isDrained);
2186-
21872187
if (consumers.isEmpty()) {
21882188
break;
21892189
}
2190-
21912190
//noinspection BusyWait
21922191
Thread.sleep(1); // Sleep 1 milli
2193-
2194-
now = Instant.now();
21952192
}
21962193

21972194
// Stop publishing
@@ -2201,16 +2198,13 @@ public CompletableFuture<Boolean> drain(Duration timeout) throws TimeoutExceptio
22012198
if (timeout == null || timeout.equals(Duration.ZERO)) {
22022199
this.flush(Duration.ZERO);
22032200
} else {
2204-
now = Instant.now();
2205-
2201+
Instant now = Instant.now();
22062202
Duration passed = Duration.between(start, now);
22072203
Duration newTimeout = timeout.minus(passed);
2208-
22092204
if (newTimeout.toNanos() > 0) {
22102205
this.flush(newTimeout);
22112206
}
22122207
}
2213-
22142208
this.close(false, false); // close the connection after the last flush
22152209
tracker.complete(consumers.isEmpty());
22162210
} catch (TimeoutException e) {

src/main/java/io/nats/client/impl/NatsConnectionReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public void run() {
128128
this.gotCR = false;
129129
this.opPos = 0;
130130

131-
while (this.running.get()) {
131+
while (running.get() && !Thread.interrupted()) {
132132
this.bufferPosition = 0;
133133
int bytesRead = dataPort.read(this.buffer, 0, this.buffer.length);
134134

src/main/java/io/nats/client/impl/NatsConnectionWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public void run() {
193193
dataPort = this.dataPortFuture.get(); // Will wait for the future to complete
194194
StatisticsCollector stats = this.connection.getNatsStatistics();
195195

196-
while (this.running.get()) {
196+
while (running.get() && !Thread.interrupted()) {
197197
NatsMessage msg;
198198
if (this.reconnectMode.get()) {
199199
msg = this.reconnectOutgoing.accumulate(sendBufferLength.get(), Options.MAX_MESSAGES_IN_NETWORK_BUFFER, reconnectTimeout);

src/main/java/io/nats/client/impl/NatsConsumer.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,17 +205,15 @@ public CompletableFuture<Boolean> drain(Duration timeout) throws InterruptedExce
205205
// draining
206206
connection.getExecutor().submit(() -> {
207207
try {
208-
Instant now = Instant.now();
209-
210-
while (timeout == null || timeout.equals(Duration.ZERO)
211-
|| Duration.between(start, now).compareTo(timeout) < 0) {
208+
long stop = (timeout == null || timeout.equals(Duration.ZERO))
209+
? Long.MAX_VALUE
210+
: System.nanoTime() + timeout.toNanos();
211+
while (System.nanoTime() < stop && !Thread.interrupted()) {
212212
if (this.isDrained()) {
213213
break;
214214
}
215-
215+
//noinspection BusyWait
216216
Thread.sleep(1); // Sleep 1 milli
217-
218-
now = Instant.now();
219217
}
220218

221219
this.cleanUpAfterDrain();

src/main/java/io/nats/client/impl/NatsDispatcher.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,8 @@ boolean breakRunLoop() {
8484

8585
public void run() {
8686
try {
87-
while (this.running.get()) { // start
88-
87+
while (running.get() && !Thread.interrupted()) {
8988
NatsMessage msg = this.incoming.pop(this.waitForMessage);
90-
9189
if (msg != null) {
9290
NatsSubscription sub = msg.getNatsSubscription();
9391
if (sub != null && sub.isActive()) {

src/main/java/io/nats/client/impl/NatsDispatcherWithExecutor.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ class NatsDispatcherWithExecutor extends NatsDispatcher {
2424
@Override
2525
public void run() {
2626
try {
27-
while (this.running.get()) { // start
28-
27+
while (running.get() && !Thread.interrupted()) {
2928
NatsMessage msg = this.incoming.pop(this.waitForMessage);
30-
3129
if (msg != null) {
3230
NatsSubscription sub = msg.getNatsSubscription();
3331
if (sub != null && sub.isActive()) {

0 commit comments

Comments
 (0)