Skip to content

Commit 02bf0c9

Browse files
authored
chore: optimize getHostInfoByStrategy in PluginManager (#332)
1 parent 95f1b6f commit 02bf0c9

File tree

1 file changed

+51
-13
lines changed

1 file changed

+51
-13
lines changed

common/lib/plugin_manager.ts

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class PluginChain<T> {
6363

6464
export class PluginManager {
6565
private static readonly PLUGIN_CHAIN_CACHE = new Map<[string, HostInfo], PluginChain<any>>();
66+
private static readonly STRATEGY_PLUGIN_CHAIN_CACHE = new Map<ConnectionPlugin[], Set<ConnectionPlugin>>();
6667
private static readonly ALL_METHODS: string = "*";
6768
private static readonly CONNECT_METHOD = "connect";
6869
private static readonly FORCE_CONNECT_METHOD = "forceConnect";
@@ -272,28 +273,65 @@ export class PluginManager {
272273
}
273274

274275
acceptsStrategy(role: HostRole, strategy: string) {
275-
for (const plugin of this._plugins) {
276-
const pluginSubscribedMethods = plugin.getSubscribedMethods();
277-
const isSubscribed =
278-
pluginSubscribedMethods.has(PluginManager.ALL_METHODS) || pluginSubscribedMethods.has(PluginManager.ACCEPTS_STRATEGY_METHOD);
276+
let chain: Set<ConnectionPlugin> = PluginManager.STRATEGY_PLUGIN_CHAIN_CACHE.get(this._plugins);
277+
if (!chain) {
278+
chain = new Set();
279+
let acceptsStrategy: boolean = false;
280+
281+
for (const plugin of this._plugins) {
282+
if (
283+
plugin.getSubscribedMethods().has(PluginManager.ALL_METHODS) ||
284+
plugin.getSubscribedMethods().has(PluginManager.ACCEPTS_STRATEGY_METHOD)
285+
) {
286+
chain.add(plugin);
287+
if (!acceptsStrategy && plugin.acceptsStrategy(role, strategy)) {
288+
acceptsStrategy = true;
289+
}
290+
}
291+
}
279292

280-
if (isSubscribed && plugin.acceptsStrategy(role, strategy)) {
281-
return true;
293+
PluginManager.STRATEGY_PLUGIN_CHAIN_CACHE.set(this._plugins, chain);
294+
return acceptsStrategy;
295+
} else {
296+
for (const plugin of chain) {
297+
if (plugin.acceptsStrategy(role, strategy)) {
298+
return true;
299+
}
282300
}
283301
}
284302

285303
return false;
286304
}
287305

288306
getHostInfoByStrategy(role: HostRole, strategy: string, hosts?: HostInfo[]): HostInfo {
289-
for (const plugin of this._plugins) {
290-
const pluginSubscribedMethods = plugin.getSubscribedMethods();
291-
const isSubscribed =
292-
pluginSubscribedMethods.has(PluginManager.ALL_METHODS) || pluginSubscribedMethods.has(PluginManager.GET_HOST_INFO_BY_STRATEGY_METHOD);
293-
294-
if (isSubscribed) {
307+
let chain: Set<ConnectionPlugin> = PluginManager.STRATEGY_PLUGIN_CHAIN_CACHE.get(this._plugins);
308+
if (!chain) {
309+
chain = new Set();
310+
let host: HostInfo;
311+
312+
for (const plugin of this._plugins) {
313+
if (
314+
plugin.getSubscribedMethods().has(PluginManager.ALL_METHODS) ||
315+
plugin.getSubscribedMethods().has(PluginManager.GET_HOST_INFO_BY_STRATEGY_METHOD)
316+
) {
317+
chain.add(plugin);
318+
if (!host) {
319+
try {
320+
host = plugin.getHostInfoByStrategy(role, strategy, hosts);
321+
} catch (error) {
322+
// This plugin does not support the provided strategy, ignore the exception and move on
323+
}
324+
}
325+
}
326+
}
327+
PluginManager.STRATEGY_PLUGIN_CHAIN_CACHE.set(this._plugins, chain);
328+
if (host) {
329+
return host;
330+
}
331+
} else {
332+
for (const plugin of chain) {
295333
try {
296-
const host = plugin.getHostInfoByStrategy(role, strategy, hosts);
334+
const host: HostInfo = plugin.getHostInfoByStrategy(role, strategy, hosts);
297335
if (host) {
298336
return host;
299337
}

0 commit comments

Comments
 (0)