@@ -754,18 +754,25 @@ type internal ConsumerImpl<'T> (consumerConfig: ConsumerConfiguration<'T>, clien
754
754
if incomingMessages.Count > 0 then
755
755
replyWithMessage channel <| dequeueMessage()
756
756
else
757
+ let mutable synchronouslyCanceled = false
757
758
let tokenRegistration =
758
759
if cancellationToken.CanBeCanceled then
759
- let rec cancellationTokenRegistration =
760
+ let mutable cancellationTokenRegistration = None
761
+ cancellationTokenRegistration <-
760
762
cancellationToken.Register(( fun () ->
761
763
Log.Logger.LogDebug( " {0} receive cancelled" , prefix)
762
- post this.Mb ( CancelWaiter( cancellationTokenRegistration, channel))
764
+ match cancellationTokenRegistration with
765
+ | None -> synchronouslyCanceled <- true
766
+ | Some _ -> post this.Mb ( CancelWaiter( cancellationTokenRegistration, channel))
763
767
), false ) |> Some
764
768
cancellationTokenRegistration
765
769
else
766
770
None
767
- waiters.AddLast( struct ( tokenRegistration, channel)) |> ignore
768
- Log.Logger.LogDebug( " {0} Receive waiting" , prefix)
771
+ if synchronouslyCanceled then
772
+ channel.SetCanceled()
773
+ else
774
+ waiters.AddLast( struct ( tokenRegistration, channel)) |> ignore
775
+ Log.Logger.LogDebug( " {0} Receive waiting" , prefix)
769
776
770
777
let batchReceive ( receiveCallbacks : ReceiveCallbacks < 'T >) =
771
778
Log.Logger.LogDebug( " {0} BatchReceive" , prefix)
@@ -778,23 +785,32 @@ type internal ConsumerImpl<'T> (consumerConfig: ConsumerConfiguration<'T>, clien
778
785
replyWithBatch channel
779
786
else
780
787
let batchCts = new CancellationTokenSource()
781
- let registration =
788
+ let mutable synchronouslyCanceled = false
789
+ let tokenRegistration =
782
790
if cancellationToken.CanBeCanceled then
783
- let rec cancellationTokenRegistration =
791
+ let mutable cancellationTokenRegistration = None
792
+ cancellationTokenRegistration <-
784
793
cancellationToken.Register(( fun () ->
785
794
Log.Logger.LogDebug( " {0} batch receive cancelled" , prefix)
786
- post this.Mb ( CancelBatchWaiter( batchCts, cancellationTokenRegistration, channel))
795
+ match cancellationTokenRegistration with
796
+ | None -> synchronouslyCanceled <- true
797
+ | Some _ -> post this.Mb ( CancelBatchWaiter( batchCts, cancellationTokenRegistration, channel))
787
798
), false ) |> Some
788
799
cancellationTokenRegistration
789
800
else
790
801
None
791
- batchWaiters.AddLast( struct ( batchCts, registration, channel)) |> ignore
792
- asyncDelay
793
- consumerConfig.BatchReceivePolicy.Timeout
794
- ( fun () ->
795
- if not batchCts.IsCancellationRequested then
796
- post this.Mb SendBatchByTimeout)
797
- Log.Logger.LogDebug( " {0} BatchReceive waiting" , prefix)
802
+ if synchronouslyCanceled then
803
+ channel.SetCanceled()
804
+ else
805
+ batchWaiters.AddLast( struct ( batchCts, tokenRegistration, channel)) |> ignore
806
+ asyncDelay
807
+ consumerConfig.BatchReceivePolicy.Timeout
808
+ ( fun () ->
809
+ if not batchCts.IsCancellationRequested then
810
+ post this.Mb SendBatchByTimeout
811
+ else
812
+ batchCts.Dispose())
813
+ Log.Logger.LogDebug( " {0} BatchReceive waiting" , prefix)
798
814
799
815
let consumerOperations = {
800
816
MessageReceived = fun rawMessage -> post this.Mb ( MessageReceived rawMessage)
0 commit comments