Skip to content

Commit 5fb327c

Browse files
authored
Expose close connection (#18)
1 parent f3bb98e commit 5fb327c

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

src/Insurello.RabbitMqClient/MqClient.fs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ module MqClient =
1313
type private ModelData =
1414
{ channelConsumer: AsyncEventingBasicConsumer
1515
rpcConsumer: AsyncEventingBasicConsumer
16-
pendingRequests: System.Collections.Concurrent.ConcurrentDictionary<string, Result<ReceivedMessage, string> System.Threading.Tasks.TaskCompletionSource> }
16+
pendingRequests: System.Collections.Concurrent.ConcurrentDictionary<string, Result<ReceivedMessage, string> System.Threading.Tasks.TaskCompletionSource>
17+
connection: IConnection }
1718

1819
and Message<'event> = private | Message of 'event * ModelData
1920

2021
and ReceivedMessage = Message<BasicDeliverEventArgs>
2122

2223
type RawBody = string
2324

24-
type Model = private | Model of ModelData
25+
type Model = private Model of ModelData
2526

2627
/// <summary>
2728
/// The maximum number of MQ messages to be fetched from queues and get processed at a time by the RabbitMQ client.
@@ -195,6 +196,18 @@ module MqClient =
195196
else
196197
()
197198

199+
let private closeRpcConsumer: Model -> unit =
200+
fun (Model model) -> if model.rpcConsumer.Model.IsOpen then model.rpcConsumer.Model.Close() else ()
201+
202+
let private closeChannelConsumer: Model -> unit =
203+
fun (Model model) -> if model.channelConsumer.Model.IsOpen then model.channelConsumer.Model.Close() else ()
204+
205+
let close: Model -> unit =
206+
fun (Model model) ->
207+
closeChannelConsumer (Model model)
208+
closeRpcConsumer (Model model)
209+
closeConnection 0 model.connection
210+
198211
let private createChannel: ChannelConfig -> ExceptionCallback -> IConnection -> Result<IModel, string> =
199212
fun config exCallback connection ->
200213
try
@@ -310,7 +323,10 @@ module MqClient =
310323
(PublishResult.ReturnError
311324
(sprintf
312325
"Failed to publish to queue: ReplyCode: %i, ReplyText: %s, Exchange: %s, RoutingKey: %s"
313-
args.ReplyCode args.ReplyText args.Exchange args.RoutingKey))
326+
args.ReplyCode
327+
args.ReplyText
328+
args.Exchange
329+
args.RoutingKey))
314330
|> ignore
315331
else
316332
())
@@ -395,11 +411,15 @@ module MqClient =
395411

396412
createChannel
397413
{ withConfirmSelect = true
398-
prefetchCount = prefetchCount } exCallback connection
414+
prefetchCount = prefetchCount }
415+
exCallback
416+
connection
399417
|> Result.bind (fun channel ->
400418
createChannel
401419
{ withConfirmSelect = false
402-
prefetchCount = prefetchCount } exCallback connection
420+
prefetchCount = prefetchCount }
421+
exCallback
422+
connection
403423
|> Result.map (fun rpcChannel ->
404424
(connection,
405425
Model
@@ -409,7 +429,8 @@ module MqClient =
409429

410430
pendingRequests =
411431
System.Collections.Concurrent.ConcurrentDictionary<string, Result<ReceivedMessage, string> System.Threading.Tasks.TaskCompletionSource>
412-
() }))))
432+
()
433+
connection = connection }))))
413434
|> Result.bind (fun (connection, model) ->
414435
let declareAQueue = declareQueue model
415436
let bindAQueue = bindQueueToExchange model
@@ -442,7 +463,9 @@ module MqClient =
442463
(callback =
443464
(fun () ->
444465
tcs.SetResult
445-
((sprintf "Publish to queue '%s' timedout after %ss" routingKey
466+
((sprintf
467+
"Publish to queue '%s' timedout after %ss"
468+
routingKey
446469
(timeout.TotalSeconds.ToString()))
447470
|> PublishResult.Timeout)
448471
|> ignore),
@@ -494,7 +517,9 @@ module MqClient =
494517
let! publishResult = tcs.Task |> (Async.AwaitTask >> Async.Catch)
495518

496519
model.channelConsumer.Model.BasicAcks.RemoveHandler basicAckEventHandler
520+
497521
model.channelConsumer.Model.BasicNacks.RemoveHandler basicNackEventHandler
522+
498523
model.channelConsumer.Model.BasicReturn.RemoveHandler basicReturnEventHandler
499524

500525
return match publishResult with
@@ -566,7 +591,9 @@ module MqClient =
566591

567592
tcs.TrySetResult
568593
(Error
569-
(sprintf "Publish to queue '%s' timedout after %ss" routingKey
594+
(sprintf
595+
"Publish to queue '%s' timedout after %ss"
596+
routingKey
570597
(timeout.TotalSeconds.ToString())))
571598
|> ignore),
572599
useSynchronizationContext = false)

0 commit comments

Comments
 (0)