Skip to content

Commit 9b62ed7

Browse files
committed
add option to ignore duplicates
1 parent e59b4be commit 9b62ed7

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

src/DatatypeChannels.ASB/Consumer.fs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ type internal MessageContext =
2525
CancellationTokenSource = new CancellationTokenSource()
2626
Reciever = reciever }
2727

28+
type internal Options() =
29+
inherit ServiceBusReceiverOptions()
30+
member val IgnoreDuplicates = true with get,set
2831

29-
let mkNew (options:ServiceBusReceiverOptions)
32+
let mkNew (options: Options)
3033
(startRenewal: MessageContext -> Task)
3134
(OfReceived ofReceived)
3235
(withClient: (ServiceBusClient -> _) -> _)
@@ -70,12 +73,18 @@ let mkNew (options:ServiceBusReceiverOptions)
7073
activity.Dispose()
7174
return None
7275
}
73-
return msg |> Option.map (fun msg ->
76+
return msg |> Option.bind (fun msg ->
7477
let msgCtx = MessageContext.From received activity receiver
75-
if receiver.ReceiveMode = ServiceBusReceiveMode.PeekLock then
76-
if msgCtxs.TryAdd(received.MessageId, msgCtx) then startRenewal msgCtx |> ignore
77-
else failwith "Unable to add the message"
78-
{ Msg = msg; Id = received.MessageId })
78+
match receiver.ReceiveMode with
79+
| ServiceBusReceiveMode.ReceiveAndDelete ->
80+
Some { Msg = msg; Id = received.MessageId }
81+
| ServiceBusReceiveMode.PeekLock when msgCtxs.TryAdd(received.MessageId, msgCtx) ->
82+
startRenewal msgCtx |> ignore
83+
Some { Msg = msg; Id = received.MessageId }
84+
| ServiceBusReceiveMode.PeekLock when not options.IgnoreDuplicates ->
85+
failwith $"Unable to add the message, duplicate id: {received.MessageId}"
86+
| _ -> None
87+
)
7988
}
8089

8190
member __.Ack receivedId =

src/DatatypeChannels.ASB/DatatypeChannels.fs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,27 @@ open System.Threading.Tasks
55
open Azure.Messaging.ServiceBus
66
open Azure.Messaging.ServiceBus.Administration
77

8+
type ChannelOptions =
9+
{ Prefetch: uint16 option // optional prefetch limit
10+
IgnoreDuplicates: bool
11+
TempIdle: TimeSpan } // temporary queue idle lifetime
12+
static member Default =
13+
{ Prefetch = None
14+
TempIdle = TimeSpan.FromMinutes 5.
15+
IgnoreDuplicates = true }
16+
817
[<RequireQualifiedAccess>]
918
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
1019
module Channels =
1120
/// constructs event-stream publishers and consumers.
1221
/// mkClient: function to construct the client.
1322
/// mkAdminClient: function to construct the admin client.
1423
/// log: function for diagnostics logging.
15-
/// prefetch: optional prefetch limit.
16-
/// tempIdle: temporary queue idle lifetime.
24+
/// options: channel options.
1725
let mkNew (mkClient: unit -> ServiceBusClient)
1826
(mkAdminClient: unit -> ServiceBusAdministrationClient)
1927
(log: Log)
20-
(prefetch: uint16 option)
21-
(tempIdle: TimeSpan) =
28+
(options: ChannelOptions) =
2229
let client = lazy mkClient()
2330
let adminClient = lazy mkAdminClient()
2431
let withClient cont = cont client.Value
@@ -32,23 +39,23 @@ module Channels =
3239
let renew = Consumer.Renewable.mkNew binding.Subscription.LockDuration
3340
binding.Subscription.LockDuration <- min binding.Subscription.LockDuration Consumer.Renewable.maxLockDuration
3441
Subscription.withBinding log withAdminClient binding,
35-
ServiceBusReceiverOptions(ReceiveMode = ServiceBusReceiveMode.PeekLock),
42+
Consumer.Options(ReceiveMode = ServiceBusReceiveMode.PeekLock),
3643
renew
3744
| Persistent (queueOptions, bindings) ->
3845
let renew = Consumer.Renewable.mkNew queueOptions.LockDuration
3946
queueOptions.LockDuration <- min queueOptions.LockDuration Consumer.Renewable.maxLockDuration
4047
Queue.withBindings log withAdminClient queueOptions bindings,
41-
ServiceBusReceiverOptions(ReceiveMode = ServiceBusReceiveMode.PeekLock),
48+
Consumer.Options(ReceiveMode = ServiceBusReceiveMode.PeekLock),
4249
renew
4350
| Temporary bindings ->
44-
Queue.withBindings log withAdminClient (CreateQueueOptions(Guid.NewGuid().ToString(), AutoDeleteOnIdle = tempIdle)) bindings,
45-
ServiceBusReceiverOptions(ReceiveMode = ServiceBusReceiveMode.ReceiveAndDelete),
51+
Queue.withBindings log withAdminClient (CreateQueueOptions(Guid.NewGuid().ToString(), AutoDeleteOnIdle = options.TempIdle)) bindings,
52+
Consumer.Options(ReceiveMode = ServiceBusReceiveMode.ReceiveAndDelete),
4653
Consumer.Renewable.noop
4754
| DeadLetter path ->
4855
(fun cont -> Task.FromResult path |> cont),
49-
ServiceBusReceiverOptions(ReceiveMode = ServiceBusReceiveMode.PeekLock, SubQueue = SubQueue.DeadLetter),
56+
Consumer.Options(ReceiveMode = ServiceBusReceiveMode.PeekLock, SubQueue = SubQueue.DeadLetter),
5057
Consumer.Renewable.noop
51-
prefetch |> Option.iter (fun v -> receiveOptions.PrefetchCount <- int v)
58+
options.Prefetch |> Option.iter (fun v -> receiveOptions.PrefetchCount <- int v)
5259
Consumer.mkNew receiveOptions renew ofRecevied withClient withBindings
5360

5461
member __.GetPublisher<'msg> toSend (Topic topic) : Publisher<'msg> =
@@ -72,12 +79,14 @@ module Channels =
7279

7380
/// Build an instance using FQDN of the namespace and Azure TokenCredential
7481
let fromFqdn (fqNamespace: string) (credential: Azure.Core.TokenCredential) (log: Log) =
75-
mkNew (fun _ -> ServiceBusClient(fqNamespace, credential))
76-
(fun _ -> ServiceBusAdministrationClient(fqNamespace, credential))
77-
log None (TimeSpan.FromMinutes 5.)
82+
ChannelOptions.Default
83+
|> mkNew (fun _ -> ServiceBusClient(fqNamespace, credential))
84+
(fun _ -> ServiceBusAdministrationClient(fqNamespace, credential))
85+
log
7886

7987
/// Build an instance using connection string
8088
let fromConnectionString (connString: string) (log: Log) =
81-
mkNew (fun _ -> ServiceBusClient connString)
82-
(fun _ -> ServiceBusAdministrationClient connString)
83-
log None (TimeSpan.FromMinutes 5.)
89+
ChannelOptions.Default
90+
|> mkNew (fun _ -> ServiceBusClient connString)
91+
(fun _ -> ServiceBusAdministrationClient connString)
92+
log

0 commit comments

Comments
 (0)