Skip to content

Commit

Permalink
Change the way the cut attribute is modified
Browse files Browse the repository at this point in the history
  • Loading branch information
ATAXGT committed Mar 5, 2019
1 parent ec0883d commit 609aa31
Showing 1 changed file with 12 additions and 17 deletions.
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

0 comments on commit 609aa31

Please sign in to comment.