-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abort any latches when a client receives an invalid message (#13)
- Loading branch information
Showing
2 changed files
with
74 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/io/playpen/core/utils/AbortableCountDownLatch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.playpen.core.utils; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
// credit to https://stackoverflow.com/a/10455821/646180 | ||
public class AbortableCountDownLatch extends CountDownLatch { | ||
protected boolean aborted = false; | ||
|
||
public AbortableCountDownLatch(int count) { | ||
super(count); | ||
} | ||
|
||
|
||
/** | ||
* Unblocks all threads waiting on this latch and cause them to receive an | ||
* AbortedException. If the latch has already counted all the way down, | ||
* this method does nothing. | ||
*/ | ||
public void abort() { | ||
if( getCount()==0 ) | ||
return; | ||
|
||
this.aborted = true; | ||
while(getCount()>0) | ||
countDown(); | ||
} | ||
|
||
|
||
@Override | ||
public boolean await(long timeout, TimeUnit unit) throws InterruptedException { | ||
final boolean rtrn = super.await(timeout,unit); | ||
if (aborted) | ||
throw new AbortedException(); | ||
return rtrn; | ||
} | ||
|
||
@Override | ||
public void await() throws InterruptedException { | ||
super.await(); | ||
if (aborted) | ||
throw new AbortedException(); | ||
} | ||
|
||
|
||
public static class AbortedException extends InterruptedException { | ||
public AbortedException() { | ||
} | ||
|
||
public AbortedException(String detailMessage) { | ||
super(detailMessage); | ||
} | ||
} | ||
} |