Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: OOM occured when using ab to test custom endpoints provided by p…
…lugin
- Loading branch information
Showing
4 changed files
with
51 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
application/src/main/java/run/halo/app/plugin/PluginCustomEndpointInitialization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package run.halo.app.plugin; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.reactive.function.server.RouterFunction; | ||
import run.halo.app.core.extension.endpoint.CustomEndpoint; | ||
import run.halo.app.core.extension.endpoint.CustomEndpointsBuilder; | ||
import run.halo.app.plugin.event.HaloPluginStartedEvent; | ||
|
||
/** | ||
* <p>An Initialization class for plugin's {@link RouterFunction}.</p> | ||
* | ||
* @author guqing | ||
* @since 2.11.0 | ||
*/ | ||
@Component | ||
@RequiredArgsConstructor | ||
public class PluginCustomEndpointInitialization { | ||
|
||
private final HaloPluginManager haloPluginManager; | ||
|
||
/** | ||
* Get all {@link CustomEndpoint} beans from plugin's {@link PluginApplicationContext} and | ||
* create a {@link RouterFunction} for them to register to the bean factory on plugin startup. | ||
*/ | ||
@Async | ||
@EventListener(HaloPluginStartedEvent.class) | ||
public void onPluginStartUp(HaloPluginStartedEvent event) { | ||
var pluginName = event.getPlugin().getPluginId(); | ||
var applicationContext = haloPluginManager.getPluginApplicationContext(pluginName); | ||
|
||
var endpoints = applicationContext.getBeansOfType(CustomEndpoint.class).values(); | ||
if (endpoints.isEmpty()) { | ||
return; | ||
} | ||
var endpointBuilder = new CustomEndpointsBuilder(); | ||
endpoints.forEach(endpointBuilder::add); | ||
applicationContext.registerBean("pluginCustomEndpointRouter", | ||
RouterFunction.class, endpointBuilder::build); | ||
} | ||
} |
9 changes: 4 additions & 5 deletions
9
application/src/main/java/run/halo/app/plugin/event/HaloPluginStartedEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,23 @@ | ||
package run.halo.app.plugin.event; | ||
|
||
import lombok.Getter; | ||
import org.pf4j.PluginWrapper; | ||
import org.springframework.context.ApplicationEvent; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* This event will be published to <b>application context</b> once plugin is started. | ||
* | ||
* @author guqing | ||
*/ | ||
@Getter | ||
public class HaloPluginStartedEvent extends ApplicationEvent { | ||
|
||
private final PluginWrapper plugin; | ||
|
||
|
||
public HaloPluginStartedEvent(Object source, PluginWrapper plugin) { | ||
super(source); | ||
Assert.notNull(plugin, "Plugin must not be null."); | ||
this.plugin = plugin; | ||
} | ||
|
||
public PluginWrapper getPlugin() { | ||
return plugin; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters