Skip to content

Commit

Permalink
Merge pull request #139 from jd-opensource/138-support-for-configurin…
Browse files Browse the repository at this point in the history
…g-write-protected-methods

Add write protect methods config
  • Loading branch information
hexiaofeng authored Nov 8, 2024
2 parents e732a30 + f48be4b commit 0583d31
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public boolean isAccessible(AccessMode accessMode) {
case READ_WRITE:
return true;
case READ:
return !serviceMetadata.isWrite();
return !serviceMetadata.isWriteProtect();
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class ServiceMetadata {
/**
* Indicates if the service request is a write operation.
*/
private boolean write;
private boolean writeProtect;

/**
* The service metadata for this invocation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public ServiceMetadata parse() {
String path = service == null ? parsePath() : service.getServiceType().normalize(parsePath());
String method = parseMethod();
ServicePolicy servicePolicy = parseServicePolicy(service, serviceGroup, path, method);
boolean isWrite = parseWrite(servicePolicy);
boolean writeProtect = parseWriteProtect(servicePolicy);
URI uri = URI.builder().host(serviceName).path(path).build();
if (serviceGroup != null && !serviceGroup.isEmpty()) {
uri = uri.parameters(KEY_SERVICE_GROUP, serviceGroup, KEY_SERVICE_METHOD, method);
Expand All @@ -97,7 +97,7 @@ public ServiceMetadata parse() {
service(service).
uri(uri).
servicePolicy(servicePolicy).
write(isWrite).
writeProtect(writeProtect).
build();
}

Expand Down Expand Up @@ -170,9 +170,9 @@ protected ServicePolicy parseServicePolicy(Service service, String serviceGroup,
* @param servicePolicy the service policy from which to parse the write protection status
* @return {@code true} if write protection is enabled, {@code false} otherwise
*/
protected boolean parseWrite(ServicePolicy servicePolicy) {
protected boolean parseWriteProtect(ServicePolicy servicePolicy) {
ServiceLivePolicy livePolicy = servicePolicy == null ? null : servicePolicy.getLivePolicy();
Boolean result = livePolicy != null ? livePolicy.getWriteProtect() : null;
Boolean result = livePolicy != null ? livePolicy.isWriteProtect(request.getMethod()) : null;
return result != null && result;
}

Expand Down Expand Up @@ -257,7 +257,7 @@ public HttpInboundServiceMetadataParser(ServiceRequest request,
}

@Override
protected boolean parseWrite(ServicePolicy servicePolicy) {
protected boolean parseWriteProtect(ServicePolicy servicePolicy) {
ServiceLivePolicy livePolicy = servicePolicy == null ? null : servicePolicy.getLivePolicy();
Boolean result = livePolicy != null ? livePolicy.getWriteProtect() : null;
if (result != null) {
Expand Down Expand Up @@ -333,7 +333,7 @@ public ServiceMetadata configure(ServiceMetadata metadata, UnitRule unitRule) {
}
livePolicy = livePolicy != null ? livePolicy.clone() : new ServiceLivePolicy();
livePolicy.setUnitPolicy(UnitPolicy.UNIT);
livePolicy.setWriteProtect(metadata.isWrite());
livePolicy.setWriteProtect(metadata.isWriteProtect());
ServicePolicy result = policy != null ? policy.clone() : new ServicePolicy();
// TODO policyId;
result.setLivePolicy(livePolicy);
Expand All @@ -345,7 +345,7 @@ public ServiceMetadata configure(ServiceMetadata metadata, UnitRule unitRule) {
method(metadata.getMethod()).
service(metadata.getService()).
servicePolicy(metadata.getServicePolicy()).
write(metadata.isWrite()).build();
writeProtect(metadata.isWriteProtect()).build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ public void supplement(RetryPolicy source) {
if ((exceptions == null || exceptions.isEmpty()) && source.exceptions != null) {
exceptions = source.exceptions;
}
if ((methods == null || methods.isEmpty()) && source.methods != null) {
methods = source.methods;
}
if ((methodPrefixes == null || methodPrefixes.isEmpty()) && source.methodPrefixes != null) {
methodPrefixes = source.methodPrefixes;
}
}

public long getDeadline(long startTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
* Service live policy
Expand All @@ -51,6 +52,20 @@ public class ServiceLivePolicy implements LiveStrategy, Cloneable, PolicyInherit
@JsonAlias("writeProtected")
private Boolean writeProtect;

/**
* A set of method names that should be retried in case of failure.
*/
@Getter
@Setter
private Set<String> methods;

/**
* A set of method name prefixes that should be retried in case of failure.
*/
@Getter
@Setter
private Set<String> methodPrefixes;

/**
* Expression for parsing unit variable
*/
Expand Down Expand Up @@ -152,6 +167,12 @@ public void supplement(ServiceLivePolicy source) {
if (writeProtect == null) {
writeProtect = source.getWriteProtect();
}
if ((methods == null || methods.isEmpty()) && source.methods != null) {
methods = source.methods;
}
if ((methodPrefixes == null || methodPrefixes.isEmpty()) && source.methodPrefixes != null) {
methodPrefixes = source.methodPrefixes;
}
if (unitPolicy == null) {
unitPolicy = source.getUnitPolicy();
defaultUnitThreshold = source.getDefaultUnitThreshold();
Expand All @@ -176,6 +197,34 @@ public void supplement(ServiceLivePolicy source) {
}
}

/**
* Checks whether the specified method is write-protected.
*
* @param methodName The name of the method to check.
* @return <code>true</code> if the method is write-protected, otherwise <code>false</code>.
*/
public boolean isWriteProtect(String methodName) {
if (writeProtect == null || !writeProtect || methodName == null || methodName.isEmpty()) {
return false;
}
boolean allowList = false;
if (methods != null && !methods.isEmpty()) {
allowList = true;
if (methods.contains(methodName)) {
return true;
}
}
if (methodPrefixes != null && !methodPrefixes.isEmpty()) {
allowList = true;
for (String methodPrefix : methodPrefixes) {
if (methodName.startsWith(methodPrefix)) {
return true;
}
}
}
return !allowList;
}

public ServiceLivePolicy clone() {
try {
return (ServiceLivePolicy) super.clone();
Expand Down

0 comments on commit 0583d31

Please sign in to comment.