@@ -13,7 +13,7 @@ public class NatsBuilder
13
13
{
14
14
private readonly IServiceCollection _services ;
15
15
16
- private int _poolSize = 1 ;
16
+ private Func < IServiceProvider , int > _poolSizeConfigurer = _ => 1 ;
17
17
private Func < IServiceProvider , NatsOpts , NatsOpts > ? _configureOpts ;
18
18
private Action < IServiceProvider , NatsConnection > ? _configureConnection ;
19
19
private object ? _diKey = null ;
@@ -25,7 +25,14 @@ public NatsBuilder(IServiceCollection services)
25
25
26
26
public NatsBuilder WithPoolSize ( int size )
27
27
{
28
- _poolSize = Math . Max ( size , 1 ) ;
28
+ _poolSizeConfigurer = _ => Math . Max ( size , 1 ) ;
29
+
30
+ return this ;
31
+ }
32
+
33
+ public NatsBuilder WithPoolSize ( Func < IServiceProvider , int > sizeConfigurer )
34
+ {
35
+ _poolSizeConfigurer = sp => Math . Max ( sizeConfigurer ( sp ) , 1 ) ;
29
36
30
37
return this ;
31
38
}
@@ -120,43 +127,23 @@ public NatsBuilder WithSerializerRegistry(INatsSerializerRegistry registry)
120
127
121
128
internal IServiceCollection Build ( )
122
129
{
123
- if ( _poolSize != 1 )
130
+ if ( _diKey == null )
124
131
{
125
- if ( _diKey == null )
126
- {
127
- _services . TryAddSingleton < NatsConnectionPool > ( provider => PoolFactory ( provider ) ) ;
128
- _services . TryAddSingleton < INatsConnectionPool > ( static provider => provider . GetRequiredService < NatsConnectionPool > ( ) ) ;
129
- _services . TryAddTransient < NatsConnection > ( static provider => PooledConnectionFactory ( provider , null ) ) ;
130
- _services . TryAddTransient < INatsConnection > ( static provider => provider . GetRequiredService < NatsConnection > ( ) ) ;
131
- _services . TryAddTransient < INatsClient > ( static provider => provider . GetRequiredService < NatsConnection > ( ) ) ;
132
- }
133
- else
134
- {
135
- #if NET8_0_OR_GREATER
136
- _services . TryAddKeyedSingleton < NatsConnectionPool > ( _diKey , PoolFactory ) ;
137
- _services . TryAddKeyedSingleton < INatsConnectionPool > ( _diKey , static ( provider , key ) => provider . GetRequiredKeyedService < NatsConnectionPool > ( key ) ) ;
138
- _services . TryAddKeyedTransient < NatsConnection > ( _diKey , PooledConnectionFactory ) ;
139
- _services . TryAddKeyedTransient < INatsConnection > ( _diKey , static ( provider , key ) => provider . GetRequiredKeyedService < NatsConnection > ( key ) ) ;
140
- _services . TryAddKeyedTransient < INatsClient > ( _diKey , static ( provider , key ) => provider . GetRequiredKeyedService < NatsConnection > ( key ) ) ;
141
- #endif
142
- }
132
+ _services . TryAddSingleton < NatsConnectionPool > ( provider => PoolFactory ( provider ) ) ;
133
+ _services . TryAddSingleton < INatsConnectionPool > ( static provider => provider . GetRequiredService < NatsConnectionPool > ( ) ) ;
134
+ _services . TryAddTransient < NatsConnection > ( static provider => PooledConnectionFactory ( provider , null ) ) ;
135
+ _services . TryAddTransient < INatsConnection > ( static provider => provider . GetRequiredService < NatsConnection > ( ) ) ;
136
+ _services . TryAddTransient < INatsClient > ( static provider => provider . GetRequiredService < NatsConnection > ( ) ) ;
143
137
}
144
138
else
145
139
{
146
- if ( _diKey == null )
147
- {
148
- _services . TryAddSingleton < NatsConnection > ( provider => SingleConnectionFactory ( provider ) ) ;
149
- _services . TryAddSingleton < INatsConnection > ( static provider => provider . GetRequiredService < NatsConnection > ( ) ) ;
150
- _services . TryAddSingleton < INatsClient > ( static provider => provider . GetRequiredService < NatsConnection > ( ) ) ;
151
- }
152
- else
153
- {
154
140
#if NET8_0_OR_GREATER
155
- _services . TryAddKeyedSingleton ( _diKey , SingleConnectionFactory ) ;
156
- _services . TryAddKeyedSingleton < INatsConnection > ( _diKey , static ( provider , key ) => provider . GetRequiredKeyedService < NatsConnection > ( key ) ) ;
157
- _services . TryAddKeyedSingleton < INatsClient > ( _diKey , static ( provider , key ) => provider . GetRequiredKeyedService < NatsConnection > ( key ) ) ;
141
+ _services . TryAddKeyedSingleton < NatsConnectionPool > ( _diKey , PoolFactory ) ;
142
+ _services . TryAddKeyedSingleton < INatsConnectionPool > ( _diKey , static ( provider , key ) => provider . GetRequiredKeyedService < NatsConnectionPool > ( key ) ) ;
143
+ _services . TryAddKeyedTransient < NatsConnection > ( _diKey , PooledConnectionFactory ) ;
144
+ _services . TryAddKeyedTransient < INatsConnection > ( _diKey , static ( provider , key ) => provider . GetRequiredKeyedService < NatsConnection > ( key ) ) ;
145
+ _services . TryAddKeyedTransient < INatsClient > ( _diKey , static ( provider , key ) => provider . GetRequiredKeyedService < NatsConnection > ( key ) ) ;
158
146
#endif
159
- }
160
147
}
161
148
162
149
return _services ;
@@ -179,17 +166,7 @@ private NatsConnectionPool PoolFactory(IServiceProvider provider, object? diKey
179
166
{
180
167
var options = GetNatsOpts ( provider ) ;
181
168
182
- return new NatsConnectionPool ( _poolSize , options , con => _configureConnection ? . Invoke ( provider , con ) ) ;
183
- }
184
-
185
- private NatsConnection SingleConnectionFactory ( IServiceProvider provider , object ? diKey = null )
186
- {
187
- var options = GetNatsOpts ( provider ) ;
188
-
189
- var conn = new NatsConnection ( options ) ;
190
- _configureConnection ? . Invoke ( provider , conn ) ;
191
-
192
- return conn ;
169
+ return new NatsConnectionPool ( _poolSizeConfigurer ( provider ) , options , con => _configureConnection ? . Invoke ( provider , con ) ) ;
193
170
}
194
171
195
172
private NatsOpts GetNatsOpts ( IServiceProvider provider )
0 commit comments