Browse Source

Add concurrency token request/release operation in TokenService

Signed-off-by: yunfeiyanggzq <yunfeiyang@buaa.edu.cn>
master
yunfeiyanggzq Eric Zhao 4 years ago
parent
commit
473cc84262
5 changed files with 62 additions and 7 deletions
  1. +9
    -0
      sentinel-cluster/sentinel-cluster-client-default/src/main/java/com/alibaba/csp/sentinel/cluster/client/DefaultClusterTokenClient.java
  2. +9
    -0
      sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/server/DefaultEmbeddedTokenServer.java
  3. +18
    -6
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/cluster/TokenResult.java
  4. +10
    -1
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/cluster/TokenResultStatus.java
  5. +16
    -0
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/cluster/TokenService.java

+ 9
- 0
sentinel-cluster/sentinel-cluster-client-default/src/main/java/com/alibaba/csp/sentinel/cluster/client/DefaultClusterTokenClient.java View File

@@ -182,6 +182,15 @@ public class DefaultClusterTokenClient implements ClusterTokenClient {
}
}

@Override
public TokenResult requestConcurrentToken(String clientAddress, Long ruleId, int acquireCount) {
return null;
}

@Override
public void releaseConcurrentToken(Long tokenId) {
}

private void logForResult(TokenResult result) {
switch (result.getStatus()) {
case TokenResultStatus.NO_RULE_EXISTS:


+ 9
- 0
sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/server/DefaultEmbeddedTokenServer.java View File

@@ -58,4 +58,13 @@ public class DefaultEmbeddedTokenServer implements EmbeddedClusterTokenServer {
}
return new TokenResult(TokenResultStatus.FAIL);
}

@Override
public TokenResult requestConcurrentToken(String clientAddress, Long ruleId, int acquireCount) {
return null;
}

@Override
public void releaseConcurrentToken(Long tokenId) {
}
}

+ 18
- 6
sentinel-core/src/main/java/com/alibaba/csp/sentinel/cluster/TokenResult.java View File

@@ -30,14 +30,25 @@ public class TokenResult {
private int remaining;
private int waitInMs;

private long tokenId;

private Map<String, String> attachments;

public TokenResult() {}
public TokenResult() {
}

public TokenResult(Integer status) {
this.status = status;
}

public long getTokenId() {
return tokenId;
}

public void setTokenId(long tokenId) {
this.tokenId = tokenId;
}

public Integer getStatus() {
return status;
}
@@ -77,10 +88,11 @@ public class TokenResult {
@Override
public String toString() {
return "TokenResult{" +
"status=" + status +
", remaining=" + remaining +
", waitInMs=" + waitInMs +
", attachments=" + attachments +
'}';
"status=" + status +
", remaining=" + remaining +
", waitInMs=" + waitInMs +
", attachments=" + attachments +
", tokenId=" + tokenId +
'}';
}
}

+ 10
- 1
sentinel-core/src/main/java/com/alibaba/csp/sentinel/cluster/TokenResultStatus.java View File

@@ -59,6 +59,15 @@ public final class TokenResultStatus {
* Token acquire failed (strategy not available).
*/
public static final int NOT_AVAILABLE = 5;
/**
* Token is successfully released.
*/
public static final int RELEASE_OK = 6;
/**
* Token already is released before the request arrives.
*/
public static final int ALREADY_RELEASE=7;

private TokenResultStatus() {}
private TokenResultStatus() {
}
}

+ 16
- 0
sentinel-core/src/main/java/com/alibaba/csp/sentinel/cluster/TokenService.java View File

@@ -44,4 +44,20 @@ public interface TokenService {
* @return result of the token request
*/
TokenResult requestParamToken(Long ruleId, int acquireCount, Collection<Object> params);

/**
* Request acquire concurrent tokens from remote token server.
*
* @param clientAddress the address of the request belong.
* @param ruleId ruleId the unique rule ID
* @param acquireCount token count to acquire
* @return result of the token request
*/
TokenResult requestConcurrentToken(String clientAddress,Long ruleId,int acquireCount);
/**
* Request release concurrent tokens from remote token server asynchronously.
*
* @param tokenId the unique token ID
*/
void releaseConcurrentToken(Long tokenId);
}

Loading…
Cancel
Save