Skip to content

Commit

Permalink
Refactor AbstractLicensee
Browse files Browse the repository at this point in the history
  • Loading branch information
hexiaofeng committed Jan 9, 2025
1 parent 2edca77 commit f9df43b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.jd.live.agent.governance.policy.service.circuitbreak.CircuitBreakPolicy;
import lombok.Getter;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

/**
Expand All @@ -33,13 +32,8 @@ public abstract class AbstractCircuitBreaker extends AbstractLicensee<CircuitBre
@Getter
protected final URI uri;

@Getter
protected volatile long lastAccessTime;

protected final AtomicReference<CircuitBreakerStateWindow> windowRef = new AtomicReference<>();

protected final AtomicBoolean started = new AtomicBoolean(true);

public AbstractCircuitBreaker(CircuitBreakPolicy policy, URI uri) {
this.policy = policy;
this.uri = uri;
Expand Down Expand Up @@ -112,21 +106,6 @@ public void onSuccess(long durationInMs) {
}
}

@Override
public void close() {
// When the circuit breaker is not accessed for a long time, it will be automatically garbage collected.
if (started.compareAndSet(true, false)) {
doClose();
}
}

/**
* Closes the circuit breaker.
*/
protected void doClose() {

}

/**
* Performs the actual acquisition logic.
* Subclasses must implement this method to define the specific acquisition behavior.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import com.jd.live.agent.governance.invoke.permission.AbstractLicensee;
import com.jd.live.agent.governance.policy.service.limit.ConcurrencyLimitPolicy;
import lombok.Getter;

import java.util.concurrent.atomic.AtomicBoolean;

/**
* AbstractConcurrencyLimiter
Expand All @@ -13,11 +10,6 @@
*/
public abstract class AbstractConcurrencyLimiter extends AbstractLicensee<ConcurrencyLimitPolicy> implements ConcurrencyLimiter {

@Getter
protected long lastAccessTime;

protected final AtomicBoolean started = new AtomicBoolean(true);

public AbstractConcurrencyLimiter(ConcurrencyLimitPolicy policy) {
this.policy = policy;
}
Expand All @@ -31,25 +23,11 @@ public boolean acquire() {
return doAcquire();
}

@Override
public void close() {
if (started.compareAndSet(true, false)) {
doClose();
}
}

/**
* Performs the actual acquisition logic.
* Subclasses must implement this method to define the specific acquisition behavior.
*
* @return true if the acquisition is successful, false otherwise.
*/
protected abstract boolean doAcquire();

/**
* Closes the limiter.
*/
protected void doClose() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,26 @@
import com.jd.live.agent.governance.policy.PolicyVersion;
import lombok.Getter;

import java.util.concurrent.atomic.AtomicBoolean;

/**
* An abstract class representing a licensee with a policy version.
*
* @param <P> the type of policy version
*/
@Getter
public abstract class AbstractLicensee<P extends PolicyVersion> implements Licensee<P> {

/**
* The policy associated with the licensee.
*/
@Getter
protected P policy;

@Getter
protected volatile long lastAccessTime;

protected final AtomicBoolean started = new AtomicBoolean(true);

@Override
public void exchange(P policy) {
P old = this.policy;
Expand All @@ -40,6 +47,20 @@ public void exchange(P policy) {
}
}

@Override
public void close() {
if (started.compareAndSet(true, false)) {
doClose();
}
}

/**
* Closes the limiter.
*/
protected void doClose() {

}

/**
* Performs the exchange of the policy from the older version to the newer version.
* This method can be overridden by subclasses to add additional logic during the policy exchange.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
import com.jd.live.agent.core.util.option.Option;
import com.jd.live.agent.governance.invoke.permission.AbstractLicensee;
import com.jd.live.agent.governance.policy.service.limit.RateLimitPolicy;
import lombok.Getter;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* AbstractRateLimiter is an abstract implementation of the RateLimiter interface,
Expand Down Expand Up @@ -51,11 +49,6 @@ public abstract class AbstractRateLimiter extends AbstractLicensee<RateLimitPoli
*/
protected final Option option;

@Getter
protected long lastAccessTime;

protected final AtomicBoolean started = new AtomicBoolean(true);

/**
* Constructs an instance of the AbstractRateLimiter class with the given rate limit policy and time unit.
* @param policy the rate limit policy to use
Expand Down Expand Up @@ -95,13 +88,6 @@ public boolean acquire(int permits, long timeout, TimeUnit timeUnit) {
return permits <= 0 || doAcquire(permits, timeout, timeUnit);
}

@Override
public void close() {
if (started.compareAndSet(true, false)) {
doClose();
}
}

/**
* Try to get some permits within a duration and return the result
*
Expand All @@ -112,12 +98,5 @@ public void close() {
*/
protected abstract boolean doAcquire(int permits, long timeout, TimeUnit timeUnit);

/**
* Closes the limiter.
*/
protected void doClose() {

}

}

0 comments on commit f9df43b

Please sign in to comment.