Created
March 3, 2023 23:45
-
-
Save TheAngryByrd/aa2c4dcd6096c93d887ff39f69e07b00 to your computer and use it in GitHub Desktop.
BlockingObjectPool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type BlockingObjectPool<'T when 'T : (new : unit -> 'T)>(maxCount : int) = | |
let pool = Stack<'T>(maxCount) | |
let semaphoreSlim = new SemaphoreSlim(maxCount, maxCount) | |
do for i=0 to maxCount do | |
let foo = new 'T() | |
pool.Push(foo) | |
member _.Get() = | |
printf $"Getting from pool : {semaphoreSlim.CurrentCount}" | |
semaphoreSlim.Wait() | |
pool.Pop() | |
member _.GetAsync() = task { | |
printf $"Getting from pool : {semaphoreSlim.CurrentCount}" | |
do! semaphoreSlim.WaitAsync() | |
return pool.Pop() | |
} | |
member _.Return(item : 'T) = | |
printf "Returning to pool" | |
pool.Push(item) | |
semaphoreSlim.Release() |> ignore | |
interface IDisposable with | |
member this.Dispose(): unit = | |
pool.Clear() | |
semaphoreSlim.Dispose() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment