Skip to content

Commit

Permalink
fix: Remove over-Release on cancellation in WaitButRelease (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink authored Aug 22, 2023
1 parent a87e9f6 commit 8eef521
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/Propulsion/Internal.fs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ type Sem(max) =
member _.Wait(ct : CancellationToken) = inner.WaitAsync(ct)
member x.WaitButRelease(ct: CancellationToken) = // see https://stackoverflow.com/questions/31621644/task-whenany-and-semaphoreslim-class/73197290?noredirect=1#comment129334330_73197290
if x.TryTake() then x.Release(); Task.CompletedTask
else x.Wait(ct).ContinueWith((fun _ -> x.Release()), CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default)
else let tco = TaskContinuationOptions.OnlyOnRanToCompletion ||| TaskContinuationOptions.ExecuteSynchronously
x.Wait(ct).ContinueWith((fun _ -> x.Release()), ct, tco, TaskScheduler.Default)
/// Manage a controlled shutdown by accumulating reservations of the full capacity.
member x.WaitForCompleted(ct : CancellationToken) = task {
for _ in 1..max do do! x.Wait(ct)
Expand Down
16 changes: 7 additions & 9 deletions src/Propulsion/Streams.fs
Original file line number Diff line number Diff line change
Expand Up @@ -766,14 +766,12 @@ module Scheduling =
reportStats false
// 3. Do a minimal sleep so we don't run completely hot when empty (unless we did something non-trivial)
if idle then
let waitForIncomingBatches = hasCapacity
let waitForDispatcherCapacity = not hasCapacity && not wakeForResults
let sleepTs = Stopwatch.timestamp ()
let startWaits ct = [| if wakeForResults then awaitResults ct :> Task
elif waitForDispatcherCapacity then dispatcher.AwaitCapacity(ct)
if waitForIncomingBatches then awaitPending ct
Task.Delay(sleepIntervalMs, ct) |]
do! Task.runWithCancellation ct (fun ct -> Task.WhenAny(startWaits ct))
let sleepTs = ts ()
do! Task.runWithCancellation ct (fun ct ->
Task.WhenAny[| if hasCapacity then awaitPending ct :> Task
if wakeForResults then awaitResults ct
elif not hasCapacity then dispatcher.AwaitCapacity(ct)
Task.Delay(sleepIntervalMs, ct) |])
t.RecordSleep sleepTs
// 4. Record completion state once per iteration; dumping streams is expensive so needs to be done infrequently
let dispatcherState = if not hasCapacity then Full elif idle then Idle else Active
Expand Down Expand Up @@ -999,7 +997,7 @@ type Concurrent private () =
// Frequency of jettisoning Write Position state of inactive streams (held by the scheduler for deduplication purposes) to limit memory consumption
// NOTE: Purging can impair performance, increase write costs or result in duplicate event emissions due to redundant inputs not being deduplicated
?purgeInterval,
// Request optimal throughput by waking based on handler outcomes even if there is unused dispatch capacity
// Request optimal throughput by waking based on handler outcomes even if there is no unused dispatch capacity
?wakeForResults,
// Tune the sleep time when there are no items to schedule or responses to process. Default 1s.
?idleDelay,
Expand Down

0 comments on commit 8eef521

Please sign in to comment.