Skip to content

Commit

Permalink
Add try-with-resources support for Entry class (alibaba#550)
Browse files Browse the repository at this point in the history
- `Entry` class now implements `AutoCloseable` so it supports try-with-resources (`close` method adapts to `exit`)

Signed-off-by: Eric Zhao <[email protected]>
  • Loading branch information
sczyh30 authored Mar 8, 2019
1 parent e857b5c commit 775484b
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions sentinel-core/src/main/java/com/alibaba/csp/sentinel/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
* @see Context
* @see ContextUtil
*/
public abstract class Entry {
public abstract class Entry implements AutoCloseable {

private static final Object[] OBJECTS0 = new Object[0];

Expand All @@ -70,6 +70,11 @@ public ResourceWrapper getResourceWrapper() {
return resourceWrapper;
}

/**
* Complete the current resource entry and restore the entry stack in context.
*
* @throws ErrorEntryFreeException if entry in current context does not match current entry
*/
public void exit() throws ErrorEntryFreeException {
exit(1, OBJECTS0);
}
Expand All @@ -78,11 +83,21 @@ public void exit(int count) throws ErrorEntryFreeException {
exit(count, OBJECTS0);
}

/**
* Equivalent to {@link #exit()}. Support try-with-resources since JDK 1.7.
*
* @since 1.5.0
*/
@Override
public void close() {
exit();
}

/**
* Exit this entry. This method should invoke if and only if once at the end of the resource protection.
*
* @param count tokens to release.
* @param args
* @param args extra parameters
* @throws ErrorEntryFreeException, if {@link Context#getCurEntry()} is not this entry.
*/
public abstract void exit(int count, Object... args) throws ErrorEntryFreeException;
Expand All @@ -91,7 +106,7 @@ public void exit(int count) throws ErrorEntryFreeException {
* Exit this entry.
*
* @param count tokens to release.
* @param args
* @param args extra parameters
* @return next available entry after exit, that is the parent entry.
* @throws ErrorEntryFreeException, if {@link Context#getCurEntry()} is not this entry.
*/
Expand Down

0 comments on commit 775484b

Please sign in to comment.