Support passing parameters via SentinelReactorTransformer

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
Eric Zhao 2019-04-02 10:38:57 +08:00
parent afc77c2b3e
commit fc550343c7
5 changed files with 37 additions and 16 deletions

View File

@ -15,6 +15,8 @@
*/
package com.alibaba.csp.sentinel.adapter.reactor;
import java.util.Arrays;
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.util.AssertUtil;
@ -26,6 +28,8 @@ public class EntryConfig {
private final String resourceName;
private final EntryType entryType;
private final int acquireCount;
private final Object[] args;
private final ContextConfig contextConfig;
public EntryConfig(String resourceName) {
@ -37,9 +41,18 @@ public class EntryConfig {
}
public EntryConfig(String resourceName, EntryType entryType, ContextConfig contextConfig) {
checkParams(resourceName, entryType);
this(resourceName, entryType, 1, new Object[0], contextConfig);
}
public EntryConfig(String resourceName, EntryType entryType, int acquireCount, Object[] args,
ContextConfig contextConfig) {
AssertUtil.assertNotBlank(resourceName, "resourceName cannot be blank");
AssertUtil.notNull(entryType, "entryType cannot be null");
AssertUtil.isTrue(acquireCount > 0, "acquireCount should be positive");
this.resourceName = resourceName;
this.entryType = entryType;
this.acquireCount = acquireCount;
this.args = args;
// Constructed ContextConfig should be valid here. Null is allowed here.
this.contextConfig = contextConfig;
}
@ -52,25 +65,25 @@ public class EntryConfig {
return entryType;
}
public int getAcquireCount() {
return acquireCount;
}
public Object[] getArgs() {
return args;
}
public ContextConfig getContextConfig() {
return contextConfig;
}
public static void assertValid(EntryConfig config) {
AssertUtil.notNull(config, "entry config cannot be null");
checkParams(config.resourceName, config.entryType);
}
private static void checkParams(String resourceName, EntryType entryType) {
AssertUtil.assertNotBlank(resourceName, "resourceName cannot be blank");
AssertUtil.notNull(entryType, "entryType cannot be null");
}
@Override
public String toString() {
return "EntryConfig{" +
"resourceName='" + resourceName + '\'' +
", entryType=" + entryType +
", acquireCount=" + acquireCount +
", args=" + Arrays.toString(args) +
", contextConfig=" + contextConfig +
'}';
}

View File

@ -15,6 +15,8 @@
*/
package com.alibaba.csp.sentinel.adapter.reactor;
import com.alibaba.csp.sentinel.util.AssertUtil;
import reactor.core.CoreSubscriber;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxOperator;
@ -29,7 +31,7 @@ public class FluxSentinelOperator<T> extends FluxOperator<T, T> {
public FluxSentinelOperator(Flux<? extends T> source, EntryConfig entryConfig) {
super(source);
EntryConfig.assertValid(entryConfig);
AssertUtil.notNull(entryConfig, "entryConfig cannot be null");
this.entryConfig = entryConfig;
}

View File

@ -15,6 +15,8 @@
*/
package com.alibaba.csp.sentinel.adapter.reactor;
import com.alibaba.csp.sentinel.util.AssertUtil;
import reactor.core.CoreSubscriber;
import reactor.core.publisher.Mono;
import reactor.core.publisher.MonoOperator;
@ -29,7 +31,7 @@ public class MonoSentinelOperator<T> extends MonoOperator<T, T> {
public MonoSentinelOperator(Mono<? extends T> source, EntryConfig entryConfig) {
super(source);
EntryConfig.assertValid(entryConfig);
AssertUtil.notNull(entryConfig, "entryConfig cannot be null");
this.entryConfig = entryConfig;
}

View File

@ -23,6 +23,7 @@ import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.csp.sentinel.util.function.Supplier;
import org.reactivestreams.Subscription;
@ -53,7 +54,7 @@ public class SentinelReactorSubscriber<T> extends InheritableBaseSubscriber<T> {
}
private void checkEntryConfig(EntryConfig config) {
EntryConfig.assertValid(config);
AssertUtil.notNull(config, "entryConfig cannot be null");
}
@Override
@ -88,7 +89,8 @@ public class SentinelReactorSubscriber<T> extends InheritableBaseSubscriber<T> {
ContextUtil.enter(sentinelContextConfig.getContextName(), sentinelContextConfig.getOrigin());
}
try {
AsyncEntry entry = SphU.asyncEntry(entryConfig.getResourceName(), entryConfig.getEntryType());
AsyncEntry entry = SphU.asyncEntry(entryConfig.getResourceName(), entryConfig.getEntryType(),
entryConfig.getAcquireCount(), entryConfig.getArgs());
this.currentEntry = entry;
actual.onSubscribe(this);
} catch (BlockException ex) {

View File

@ -17,6 +17,8 @@ package com.alibaba.csp.sentinel.adapter.reactor;
import java.util.function.Function;
import com.alibaba.csp.sentinel.util.AssertUtil;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@ -36,7 +38,7 @@ public class SentinelReactorTransformer<T> implements Function<Publisher<T>, Pub
}
public SentinelReactorTransformer(EntryConfig entryConfig) {
EntryConfig.assertValid(entryConfig);
AssertUtil.notNull(entryConfig, "entryConfig cannot be null");
this.entryConfig = entryConfig;
}