Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add try-with-resources support for Entry class #550

Merged
merged 1 commit into from
Mar 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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