Skip to content

Commit 2969215

Browse files
emilklassonPhilip Pålsson
andauthored
Close connection if any queue in the topology fails to init. (#14)
Co-authored-by: Philip Pålsson <[email protected]>
1 parent 76c5ea6 commit 2969215

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

src/Insurello.RabbitMqClient/MqClient.fs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ module MqClient =
5252
| Json of string
5353
| Binary of byte array
5454

55+
[<RequireQualifiedAccess>]
5556
type CorrelationId =
5657
| Generate
5758
| Id of string
@@ -176,13 +177,8 @@ module MqClient =
176177

177178
| :? System.ArgumentException as ex -> Error ex.Message
178179

179-
let private closeConnection: IConnection -> unit =
180-
fun connection -> if connection.IsOpen then connection.Close() else ()
181-
182-
let private closeConnectionAsync: System.TimeSpan -> IConnection -> unit =
183-
fun waitTimeout connection ->
184-
Async.Start
185-
(async { closeConnection connection }, (new System.Threading.CancellationTokenSource(waitTimeout)).Token)
180+
let private closeConnection: int -> IConnection -> unit =
181+
fun timeout connection -> if connection.IsOpen then connection.Close(timeout) else ()
186182

187183
let private createChannel: ChannelConfig -> ExceptionCallback -> IConnection -> Result<IModel, string> =
188184
fun config exCallback connection ->
@@ -350,7 +346,7 @@ module MqClient =
350346
let exCallback =
351347
(fun ex context connection ->
352348
logError (ex, "Unhandled exception on channel in context {$c}", context)
353-
closeConnectionAsync (System.TimeSpan.FromSeconds 3.0) connection)
349+
closeConnection 3000 connection)
354350

355351
createChannel
356352
{ withConfirmSelect = true
@@ -360,15 +356,16 @@ module MqClient =
360356
{ withConfirmSelect = false
361357
prefetchCount = prefetchCount } exCallback connection
362358
|> Result.map (fun rpcChannel ->
363-
Model
364-
{ channelConsumer = AsyncEventingBasicConsumer channel
359+
(connection,
360+
Model
361+
{ channelConsumer = AsyncEventingBasicConsumer channel
365362

366-
rpcConsumer = AsyncEventingBasicConsumer rpcChannel
363+
rpcConsumer = AsyncEventingBasicConsumer rpcChannel
367364

368-
pendingRequests =
369-
System.Collections.Concurrent.ConcurrentDictionary<string, Result<ReceivedMessage, string> System.Threading.Tasks.TaskCompletionSource>
370-
() })))
371-
|> Result.bind (fun model ->
365+
pendingRequests =
366+
System.Collections.Concurrent.ConcurrentDictionary<string, Result<ReceivedMessage, string> System.Threading.Tasks.TaskCompletionSource>
367+
() }))))
368+
|> Result.bind (fun (connection, model) ->
372369
let declareAQueue = declareQueue model
373370
let bindAQueue = bindQueueToExchange model
374371

@@ -381,6 +378,9 @@ module MqClient =
381378
declareAQueue queueTopology
382379
|> Result.bind bindAQueue
383380
|> Result.map consumeAQueue) prevResult) (Ok model)
381+
|> Result.mapError (fun error ->
382+
closeConnection 3000 connection
383+
error)
384384
|> Result.map initReplyQueue)
385385

386386
/// Will publish with confirm.
@@ -437,8 +437,8 @@ module MqClient =
437437
MessageId = messageId,
438438
CorrelationId =
439439
(match message.CorrelationId with
440-
| Generate -> ""
441-
| Id correlationId -> correlationId),
440+
| CorrelationId.Generate -> ""
441+
| CorrelationId.Id correlationId -> correlationId),
442442
Headers =
443443
(message.Headers
444444
|> Map.map (fun _ v -> v :> obj)

0 commit comments

Comments
 (0)