Skip to content

Commit

Permalink
Add scheduler config (apache#1493)
Browse files Browse the repository at this point in the history
* add scheduler config

* add scheduler config
  • Loading branch information
yu199195 authored May 19, 2021
1 parent d01626a commit d6f5d10
Showing 1 changed file with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package org.apache.shenyu.web.handler;

import java.util.List;
import java.util.Objects;
import org.apache.shenyu.plugin.api.ShenyuPlugin;
import org.apache.shenyu.plugin.api.ShenyuPluginChain;
import org.springframework.lang.NonNull;
Expand All @@ -28,14 +26,19 @@
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;

import java.util.List;
import java.util.Objects;

/**
* This is web handler request starter.
*/
public final class ShenyuWebHandler implements WebHandler {

private final List<ShenyuPlugin> plugins;

private final boolean scheduled;

private final Scheduler scheduler;
private Scheduler scheduler;

/**
* Instantiates a new shenyu web handler.
Expand All @@ -44,13 +47,17 @@ public final class ShenyuWebHandler implements WebHandler {
*/
public ShenyuWebHandler(final List<ShenyuPlugin> plugins) {
this.plugins = plugins;
String schedulerType = System.getProperty("shenyu.scheduler.type", "fixed");
if (Objects.equals(schedulerType, "fixed")) {
int threads = Integer.parseInt(System.getProperty(
"shenyu.work.threads", "" + Math.max((Runtime.getRuntime().availableProcessors() << 1) + 1, 16)));
scheduler = Schedulers.newParallel("shenyu-work-threads", threads);
} else {
scheduler = Schedulers.elastic();
String enabled = System.getProperty("shenyu.scheduler.enabled", "false");
this.scheduled = Boolean.parseBoolean(enabled);
if (scheduled) {
String schedulerType = System.getProperty("shenyu.scheduler.type", "fixed");
if (Objects.equals(schedulerType, "fixed")) {
int threads = Integer.parseInt(System.getProperty(
"shenyu.work.threads", "" + Math.max((Runtime.getRuntime().availableProcessors() << 1) + 1, 16)));
scheduler = Schedulers.newParallel("shenyu-work-threads", threads);
} else {
scheduler = Schedulers.elastic();
}
}
}

Expand All @@ -62,7 +69,11 @@ public ShenyuWebHandler(final List<ShenyuPlugin> plugins) {
*/
@Override
public Mono<Void> handle(@NonNull final ServerWebExchange exchange) {
return new DefaultShenyuPluginChain(plugins).execute(exchange).subscribeOn(scheduler);
Mono<Void> execute = new DefaultShenyuPluginChain(plugins).execute(exchange);
if (scheduled) {
return execute.subscribeOn(scheduler);
}
return execute;
}

private static class DefaultShenyuPluginChain implements ShenyuPluginChain {
Expand Down

0 comments on commit d6f5d10

Please sign in to comment.