Skip to content

Optimize circuit breaking state transformation using CAS in DegradeRule #538

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

Merged
merged 1 commit into from
Mar 5, 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory;
Expand Down Expand Up @@ -80,7 +81,7 @@ public DegradeRule(String resourceName) {
*/
private int grade = RuleConstant.DEGRADE_GRADE_RT;

private volatile boolean cut = false;
private final AtomicBoolean cut = new AtomicBoolean(false);

public int getGrade() {
return grade;
Expand All @@ -93,8 +94,6 @@ public DegradeRule setGrade(int grade) {

private AtomicLong passCount = new AtomicLong(0);

private final Object lock = new Object();

public double getCount() {
return count;
}
Expand All @@ -104,12 +103,12 @@ public DegradeRule setCount(double count) {
return this;
}

public boolean isCut() {
return cut;
private boolean isCut() {
return cut.get();
}

private void setCut(boolean cut) {
this.cut = cut;
this.cut.set(cut);
}

public AtomicLong getPassCount() {
Expand Down Expand Up @@ -162,7 +161,7 @@ public int hashCode() {

@Override
public boolean passCheck(Context context, DefaultNode node, int acquireCount, Object... args) {
if (cut) {
if (cut.get()) {
return false;
}

Expand Down Expand Up @@ -206,16 +205,12 @@ public boolean passCheck(Context context, DefaultNode node, int acquireCount, Ob
}
}

synchronized (lock) {
if (!cut) {
// Automatically degrade.
cut = true;
ResetTask resetTask = new ResetTask(this);
pool.schedule(resetTask, timeWindow, TimeUnit.SECONDS);
}

return false;
if (cut.compareAndSet(false, true)) {
ResetTask resetTask = new ResetTask(this);
pool.schedule(resetTask, timeWindow, TimeUnit.SECONDS);
}

return false;
}

@Override
Expand All @@ -240,7 +235,7 @@ private static final class ResetTask implements Runnable {
@Override
public void run() {
rule.getPassCount().set(0);
rule.setCut(false);
rule.cut.set(false);
}
}
}
Expand Down