3
3
using System . Diagnostics ;
4
4
using System . IO ;
5
5
using System . Linq ;
6
- using System . Net ;
7
6
using System . Reflection ;
8
7
using System . Runtime . Serialization ;
9
8
using System . Security . Cryptography . X509Certificates ;
17
16
18
17
namespace MicroPlumberd . Services ;
19
18
20
-
21
19
class CommandBus : ICommandBus , IEventHandler
22
20
{
23
21
private readonly IPlumber _plumber ;
22
+ private readonly ICommandBusPool _pool ;
24
23
private readonly ILogger < CommandBus > _log ;
25
24
private readonly string _streamIn ;
26
25
private readonly string _streamOut ;
@@ -31,9 +30,10 @@ class CommandBus : ICommandBus, IEventHandler
31
30
private readonly object _sync = new object ( ) ;
32
31
private IAsyncDisposable ? _subscription ;
33
32
public Guid SessionId { get ; } = Guid . NewGuid ( ) ;
34
- public CommandBus ( IPlumber plumber , ILogger < CommandBus > log )
33
+ public CommandBus ( IPlumber plumber , ICommandBusPool pool , ILogger < CommandBus > log )
35
34
{
36
35
_plumber = plumber ;
36
+ _pool = pool ;
37
37
_log = log ;
38
38
var servicesConventions = plumber . Config . Conventions . ServicesConventions ( ) ;
39
39
_streamIn = servicesConventions . SessionInStreamFromSessionIdConvention ( SessionId ) ;
@@ -75,8 +75,8 @@ private bool TryMapEventResponse(string type, out Type t)
75
75
76
76
public async Task QueueAsync ( object recipientId , object command , TimeSpan ? timeout = null , bool fireAndForget = true , CancellationToken token = default )
77
77
{
78
- await using CommandBus bus = new CommandBus ( this . _plumber , this . _log ) ;
79
- await bus . SendAsync ( recipientId , command , timeout ?? TimeSpan . MaxValue , fireAndForget , token ) ;
78
+ using var scope = await _pool . RentScope ( token ) ;
79
+ await scope . SendAsync ( recipientId , command , timeout ?? TimeSpan . MaxValue , fireAndForget , token ) ;
80
80
}
81
81
public async Task SendAsync ( object recipientId , object command , TimeSpan ? timeout = null , bool fireAndForget = false , CancellationToken token = default )
82
82
{
@@ -170,56 +170,7 @@ async Task IEventHandler.Handle(Metadata m, object ev)
170
170
public ValueTask DisposeAsync ( ) => _subscription ? . DisposeAsync ( ) ?? ValueTask . CompletedTask ;
171
171
}
172
172
173
-
174
- [ AttributeUsage ( AttributeTargets . Method | AttributeTargets . Class , AllowMultiple = true ) ]
175
- public class ThrowsFaultExceptionAttribute < TMessage > ( ) : ThrowsFaultExceptionAttribute ( typeof ( TMessage ) ) ;
176
-
177
173
public abstract class ThrowsFaultExceptionAttribute ( Type thrownType ) : Attribute
178
174
{
179
175
public Type ThrownType { get ; init ; } = thrownType ;
180
- }
181
-
182
-
183
- public class CommandExecutionResults
184
- {
185
- public async ValueTask < bool > Handle ( Metadata m , object ev )
186
- {
187
- switch ( ev )
188
- {
189
- case CommandExecuted ce :
190
- {
191
- IsSuccess = true ;
192
- IsReady . SetResult ( true ) ;
193
- return true ;
194
- }
195
- case ICommandFailedEx ef :
196
- {
197
- IsSuccess = false ;
198
- ErrorMessage = ef . Message ;
199
- ErrorData = ef . Fault ;
200
- ErrorCode = ef . Code ;
201
- IsReady . SetResult ( true ) ;
202
- return true ;
203
- }
204
- case ICommandFailed cf :
205
- {
206
- IsSuccess = false ;
207
- ErrorMessage = cf . Message ;
208
- ErrorCode = cf . Code ;
209
- IsReady . SetResult ( true ) ;
210
- return true ;
211
- }
212
- }
213
-
214
- return false ;
215
- }
216
-
217
- public HttpStatusCode ErrorCode { get ; private set ; }
218
-
219
-
220
- public string ErrorMessage { get ; private set ; }
221
- public object ? ErrorData { get ; private set ; }
222
- public bool IsSuccess { get ; private set ; }
223
- public TaskCompletionSource < bool > IsReady { get ; private set ; } = new TaskCompletionSource < bool > ( ) ;
224
-
225
176
}
0 commit comments