Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin lifecycle and Chain lifecycle #5475 #5491

Merged
merged 20 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,16 @@ public interface Constants {
*/
String PLUGIN_END_TIME = "pluginEndTime:";

/**
* the chain start time of chain lifecycle.
*/
String CHAIN_START_TIME = "chainStartTime:";

/**
* the chain end time of chain lifecycle.
*/
String CHAIN_END_TIME = "chainEndTime:";

/**
* ratelimiter plugin metrics.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.common.config.ShenyuConfig;
import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.dto.PluginData;
import org.apache.shenyu.common.enums.PluginHandlerEventEnum;
import org.apache.shenyu.plugin.api.ShenyuPlugin;
Expand Down Expand Up @@ -90,6 +91,28 @@ public ShenyuWebHandler(final List<ShenyuPlugin> plugins, final ShenyuLoaderServ
}
}

/**
* Chain before operation.
*
* @param exchange context
*/
public void before(final ServerWebExchange exchange) {
exchange.getAttributes().put(Constants.CHAIN_START_TIME, System.currentTimeMillis());
}

/**
* Plugin after operation.
*
* @param exchange context
*/
public void after(final ServerWebExchange exchange) {
Map<String, Object> attributes = exchange.getAttributes();
long currentTimeMillis = System.currentTimeMillis();
long startTime = (long) attributes.get(Constants.CHAIN_START_TIME);
LOG.debug("shenyu chain handle uri:{}, traceId:{}, cost:{}", exchange.getRequest().getPath(), exchange.getLogPrefix(), currentTimeMillis - startTime);
attributes.remove(Constants.CHAIN_START_TIME);
}

/**
* Handle the web server exchange.
*
Expand All @@ -98,11 +121,16 @@ public ShenyuWebHandler(final List<ShenyuPlugin> plugins, final ShenyuLoaderServ
*/
@Override
public Mono<Void> handle(@NonNull final ServerWebExchange exchange) {
Mono<Void> execute = new DefaultShenyuPluginChain(plugins).execute(exchange);
if (scheduled) {
return execute.subscribeOn(scheduler);
try {
before(exchange);
Mono<Void> execute = new DefaultShenyuPluginChain(plugins).execute(exchange);
if (scheduled) {
return execute.subscribeOn(scheduler);
}
return execute;
} finally {
after(exchange);
}
return execute;
}

/**
Expand Down
Loading