@@ -63,6 +63,7 @@ class PluginChain<T> {
63
63
64
64
export class PluginManager {
65
65
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 > > ( ) ;
66
67
private static readonly ALL_METHODS : string = "*" ;
67
68
private static readonly CONNECT_METHOD = "connect" ;
68
69
private static readonly FORCE_CONNECT_METHOD = "forceConnect" ;
@@ -272,28 +273,65 @@ export class PluginManager {
272
273
}
273
274
274
275
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
+ }
279
292
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
+ }
282
300
}
283
301
}
284
302
285
303
return false ;
286
304
}
287
305
288
306
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 ) {
295
333
try {
296
- const host = plugin . getHostInfoByStrategy ( role , strategy , hosts ) ;
334
+ const host : HostInfo = plugin . getHostInfoByStrategy ( role , strategy , hosts ) ;
297
335
if ( host ) {
298
336
return host ;
299
337
}
0 commit comments