99
1010namespace Cleipnir . ResilientFunctions . CoreRuntime ;
1111
12- public record FlowStatus ( StoredId Id , Action Suspend , QueueManager QueueManager , int Threads , int SuspendedThreads , FlowTimeouts Timeouts ) ;
13-
1412public class FlowsManager : IDisposable
1513{
16- private readonly Dictionary < StoredId , FlowStatus > _dict = new ( ) ;
14+ private readonly Dictionary < StoredId , FlowState > _dict = new ( ) ;
1715 private readonly Lock _lock = new ( ) ;
1816 private readonly UtcNow _utcNow ;
1917 private volatile bool _disposed ;
@@ -28,14 +26,14 @@ private async Task TimeoutCheckLoop()
2826 {
2927 while ( ! _disposed )
3028 {
31- var expiredStatuses = new List < FlowStatus > ( ) ;
29+ var expiredStates = new List < FlowState > ( ) ;
3230 var now = _utcNow ( ) ;
3331 lock ( _lock )
3432 foreach ( var ( _, status ) in _dict )
3533 if ( status . Timeouts . HasExpiredTimeouts ( now ) )
36- expiredStatuses . Add ( status ) ;
34+ expiredStates . Add ( status ) ;
3735
38- foreach ( var status in expiredStatuses )
36+ foreach ( var status in expiredStates )
3937 status . Timeouts . SignalExpiredTimeouts ( now ) ;
4038
4139 await Task . Delay ( 10 ) ;
@@ -47,7 +45,7 @@ private async Task TimeoutCheckLoop()
4745 public void AddFlow ( StoredId id , Action suspend , QueueManager queueManager , FlowTimeouts timeouts )
4846 {
4947 lock ( _lock )
50- _dict [ id ] = new FlowStatus ( id , suspend , queueManager , Threads : 1 , SuspendedThreads : 0 , timeouts ) ;
48+ _dict [ id ] = new FlowState ( id , suspend , queueManager , threads : 1 , suspendedThreads : 0 , timeouts ) ;
5149 }
5250
5351 public void RemoveFlow ( StoredId id )
@@ -62,62 +60,53 @@ public void Interrupt(IEnumerable<StoredId> ids)
6260 {
6361 foreach ( var id in ids )
6462 {
65- if ( ! _dict . ContainsKey ( id ) )
63+ if ( ! _dict . TryGetValue ( id , out var flowState ) )
6664 continue ;
67-
68- var queueManager = _dict [ id ] . QueueManager ;
69- InterruptThreads ( id ) ;
70- Task . Run ( ( ) => queueManager . FetchMessagesOnce ( ) ) ;
65+
66+ flowState . Interrupt ( ) ;
67+ Task . Run ( ( ) => flowState . QueueManager . FetchMessagesOnce ( ) ) ;
7168 }
69+
7270 }
7371 }
7472
7573 public void StartThread ( StoredId id )
7674 {
7775 lock ( _lock )
78- {
79- if ( ! _dict . ContainsKey ( id ) )
80- return ;
81-
82- var status = _dict [ id ] ;
83- _dict [ id ] = status with { Threads = status . Threads + 1 } ;
84- }
76+ if ( _dict . TryGetValue ( id , out var flowState ) )
77+ flowState . NewThreadStarted ( ) ;
8578 }
8679
8780 public void CompleteThread ( StoredId id )
8881 {
8982 lock ( _lock )
90- {
91- if ( ! _dict . ContainsKey ( id ) )
92- return ;
93-
94- var status = _dict [ id ] ;
95- _dict [ id ] = status with { Threads = status . Threads - 1 } ;
96- }
83+ if ( _dict . TryGetValue ( id , out var flowState ) )
84+ flowState . ThreadCompleted ( ) ;
9785 }
9886
99- public void InterruptThreads ( StoredId id )
87+ public async Task SuspendThread ( StoredId id )
10088 {
89+ FlowState ? flowState ;
10190 lock ( _lock )
102- {
103- if ( ! _dict . ContainsKey ( id ) )
104- return ;
91+ _dict . TryGetValue ( id , out flowState ) ;
10592
106- var status = _dict [ id ] ;
107- _dict [ id ] = status with { SuspendedThreads = 0 } ;
108- }
93+ if ( flowState != null )
94+ await flowState . ThreadSuspended ( ) ;
10995 }
11096
111- public void SuspendThread ( StoredId id )
97+ public Task GetInterruptedSignal ( StoredId id )
11298 {
11399 lock ( _lock )
114- {
115- if ( ! _dict . ContainsKey ( id ) )
116- return ;
100+ return _dict . TryGetValue ( id , out var flowState )
101+ ? flowState . Signal . Wait ( )
102+ : Task . CompletedTask ;
103+ }
117104
118- var status = _dict [ id ] ;
119- _dict [ id ] = status with { SuspendedThreads = status . SuspendedThreads + 1 } ;
120- }
105+ public void SignalInterrupt ( StoredId id )
106+ {
107+ lock ( _lock )
108+ if ( _dict . TryGetValue ( id , out var flowState ) )
109+ flowState . Signal . Fire ( ) ;
121110 }
122111
123112 [ DoesNotReturn ]
0 commit comments