1 people like it.
Like the snippet!
RateLimitedMessage
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
|
module RateLimiting
open System
open FSharp.Control
type RateLimitedMessage<'a,'b> = {payload: 'a ; response: ('b -> Async<unit>) option}
type RateLimitedAgent<'a,'b>(
operation: 'a -> 'b,
maxQueueCount,
workerCount,
?workerCoolDown: int,
?errorHandler: Exception -> 'a -> Async<unit>
) = class
let blockingQueue = new BlockingQueueAgent<RateLimitedMessage<'a, 'b>>(maxQueueCount)
let semaphore = new System.Threading.Semaphore(workerCount, workerCount)
let errorHandler = defaultArg errorHandler (fun _ _ -> async{()})
let workerCoolDown = defaultArg workerCoolDown -1
let createWorker () =
async {
while true do
semaphore.WaitOne()
|> ignore
let! message = blockingQueue.AsyncGet()
let messageProcessor =
async {
try
let response = operation message.payload
if message.response.IsSome then
do! message.response.Value response
with
| ex ->
errorHandler ex message.payload
|> Async.Start
}
seq {
yield messageProcessor
yield async {
do! Async.Sleep(workerCoolDown)
semaphore.Release()
|> ignore
}
}
|> Async.Parallel
|> Async.Ignore
|> Async.Start
}
do
{1 .. workerCount}
|> AsyncSeq.ofSeq
|> AsyncSeq.iterAsync(fun _ -> createWorker ())
|> Async.Start
member x.QueueItem item =
blockingQueue.AsyncAdd item
end
|
module RateLimiting
namespace System
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Control
type RateLimitedMessage<'a,'b> =
{payload: 'a;
response: ('b -> Async<unit>) option;}
Full name: RateLimiting.RateLimitedMessage<_,_>
RateLimitedMessage.payload: 'a
RateLimitedMessage.response: ('b -> Async<unit>) option
Multiple items
type Async
static member AsBeginEnd : computation:('Arg -> Async<'T>) -> ('Arg * AsyncCallback * obj -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit)
static member AwaitEvent : event:IEvent<'Del,'T> * ?cancelAction:(unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate)
static member AwaitIAsyncResult : iar:IAsyncResult * ?millisecondsTimeout:int -> Async<bool>
static member AwaitTask : task:Task<'T> -> Async<'T>
static member AwaitWaitHandle : waitHandle:WaitHandle * ?millisecondsTimeout:int -> Async<bool>
static member CancelDefaultToken : unit -> unit
static member Catch : computation:Async<'T> -> Async<Choice<'T,exn>>
static member FromBeginEnd : beginAction:(AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg:'Arg1 * beginAction:('Arg1 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * beginAction:('Arg1 * 'Arg2 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * arg3:'Arg3 * beginAction:('Arg1 * 'Arg2 * 'Arg3 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromContinuations : callback:(('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T>
static member Ignore : computation:Async<'T> -> Async<unit>
static member OnCancel : interruption:(unit -> unit) -> Async<IDisposable>
static member Parallel : computations:seq<Async<'T>> -> Async<'T []>
static member RunSynchronously : computation:Async<'T> * ?timeout:int * ?cancellationToken:CancellationToken -> 'T
static member Sleep : millisecondsDueTime:int -> Async<unit>
static member Start : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions * ?cancellationToken:CancellationToken -> Task<'T>
static member StartChild : computation:Async<'T> * ?millisecondsTimeout:int -> Async<Async<'T>>
static member StartChildAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions -> Async<Task<'T>>
static member StartImmediate : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartWithContinuations : computation:Async<'T> * continuation:('T -> unit) * exceptionContinuation:(exn -> unit) * cancellationContinuation:(OperationCanceledException -> unit) * ?cancellationToken:CancellationToken -> unit
static member SwitchToContext : syncContext:SynchronizationContext -> Async<unit>
static member SwitchToNewThread : unit -> Async<unit>
static member SwitchToThreadPool : unit -> Async<unit>
static member TryCancelled : computation:Async<'T> * compensation:(OperationCanceledException -> unit) -> Async<'T>
static member CancellationToken : Async<CancellationToken>
static member DefaultCancellationToken : CancellationToken
Full name: Microsoft.FSharp.Control.Async
--------------------
type Async<'T>
Full name: Microsoft.FSharp.Control.Async<_>
type unit = Unit
Full name: Microsoft.FSharp.Core.unit
type 'T option = Option<'T>
Full name: Microsoft.FSharp.Core.option<_>
Multiple items
type RateLimitedAgent<'a,'b> =
new : operation:('a -> 'b) * maxQueueCount:obj * workerCount:int * ?workerCoolDown:int * ?errorHandler:(Exception -> 'a -> Async<unit>) -> RateLimitedAgent<'a,'b>
member QueueItem : item:'a0 -> 'b1
Full name: RateLimiting.RateLimitedAgent<_,_>
--------------------
new : operation:('a -> 'b) * maxQueueCount:obj * workerCount:int * ?workerCoolDown:int * ?errorHandler:(Exception -> 'a -> Async<unit>) -> RateLimitedAgent<'a,'b>
val operation : ('a -> 'b)
val maxQueueCount : obj
val workerCount : int
val workerCoolDown : int option
Multiple items
val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
val errorHandler : (Exception -> 'a -> Async<unit>) option
Multiple items
type Exception =
new : unit -> Exception + 2 overloads
member Data : IDictionary
member GetBaseException : unit -> Exception
member GetObjectData : info:SerializationInfo * context:StreamingContext -> unit
member GetType : unit -> Type
member HelpLink : string with get, set
member InnerException : Exception
member Message : string
member Source : string with get, set
member StackTrace : string
...
Full name: System.Exception
--------------------
Exception() : unit
Exception(message: string) : unit
Exception(message: string, innerException: exn) : unit
val blockingQueue : obj
val semaphore : Threading.Semaphore
namespace System.Threading
Multiple items
type Semaphore =
inherit WaitHandle
new : initialCount:int * maximumCount:int -> Semaphore + 3 overloads
member GetAccessControl : unit -> SemaphoreSecurity
member Release : unit -> int + 1 overload
member SetAccessControl : semaphoreSecurity:SemaphoreSecurity -> unit
static member OpenExisting : name:string -> Semaphore + 1 overload
Full name: System.Threading.Semaphore
--------------------
Threading.Semaphore(initialCount: int, maximumCount: int) : unit
Threading.Semaphore(initialCount: int, maximumCount: int, name: string) : unit
Threading.Semaphore(initialCount: int, maximumCount: int, name: string, createdNew: byref<bool>) : unit
Threading.Semaphore(initialCount: int, maximumCount: int, name: string, createdNew: byref<bool>, semaphoreSecurity: Security.AccessControl.SemaphoreSecurity) : unit
val errorHandler : (Exception -> 'a -> Async<unit>)
val defaultArg : arg:'T option -> defaultValue:'T -> 'T
Full name: Microsoft.FSharp.Core.Operators.defaultArg
val async : AsyncBuilder
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
val workerCoolDown : int
val createWorker : (unit -> Async<unit>)
Threading.WaitHandle.WaitOne() : bool
Threading.WaitHandle.WaitOne(timeout: TimeSpan) : bool
Threading.WaitHandle.WaitOne(millisecondsTimeout: int) : bool
Threading.WaitHandle.WaitOne(timeout: TimeSpan, exitContext: bool) : bool
Threading.WaitHandle.WaitOne(millisecondsTimeout: int, exitContext: bool) : bool
val ignore : value:'T -> unit
Full name: Microsoft.FSharp.Core.Operators.ignore
val message : RateLimitedMessage<'a,'b>
val messageProcessor : Async<unit>
val response : 'b
property Option.IsSome: bool
property Option.Value: 'b -> Async<unit>
val ex : exn
static member Async.Start : computation:Async<unit> * ?cancellationToken:Threading.CancellationToken -> unit
Multiple items
val seq : sequence:seq<'T> -> seq<'T>
Full name: Microsoft.FSharp.Core.Operators.seq
--------------------
type seq<'T> = Collections.Generic.IEnumerable<'T>
Full name: Microsoft.FSharp.Collections.seq<_>
static member Async.Sleep : millisecondsDueTime:int -> Async<unit>
Threading.Semaphore.Release() : int
Threading.Semaphore.Release(releaseCount: int) : int
static member Async.Parallel : computations:seq<Async<'T>> -> Async<'T []>
static member Async.Ignore : computation:Async<'T> -> Async<unit>
val x : RateLimitedAgent<'a,'b>
member RateLimitedAgent.QueueItem : item:'a0 -> 'b1
Full name: RateLimiting.RateLimitedAgent`2.QueueItem
val item : 'a
More information