Skip to content

Commit

Permalink
Plugin lifecycle and Chain lifecycle #5475 (#5491)
Browse files Browse the repository at this point in the history
* Plugin lifecycle and Chain lifecycle #5475

* Export Selector Data should be combined with Discovery Data #5492

* Revert "Export Selector Data should be combined with Discovery Data #5492"

This reverts commit 457869e.

---------

Co-authored-by: loongs-zhang <[email protected]>
Co-authored-by: xiaoyu <[email protected]>
Co-authored-by: moremind <[email protected]>
  • Loading branch information
4 people authored Jul 20, 2024
1 parent d627cae commit 5c9a8b5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
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

0 comments on commit 5c9a8b5

Please sign in to comment.