-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathReconnectGuard.cs
More file actions
55 lines (50 loc) · 1.89 KB
/
ReconnectGuard.cs
File metadata and controls
55 lines (50 loc) · 1.89 KB
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
namespace StreamVideo.Core.LowLevelClient
{
/// <summary>
/// Guards reconnection attempts based on the current <see cref="CallingState"/>.
/// Prevents reconnection in states where it would be unsafe or redundant
/// (e.g. already reconnecting, joining, leaving, or left).
/// </summary>
internal class ReconnectGuard
{
/// <summary>
/// Whether a reconnection attempt is currently in progress.
/// This flag protects against race conditions when multiple peer connections
/// (Publisher and Subscriber) trigger reconnection simultaneously, before
/// the <see cref="CallingState"/> has been updated.
/// </summary>
public bool IsReconnecting { get; private set; }
/// <summary>
/// Attempts to begin a reconnection. Returns true if the guard allows it,
/// false if the request should be silently ignored.
/// </summary>
public bool TryBeginReconnection(CallingState currentState)
{
if (IsIgnoredState(currentState))
{
return false;
}
if (IsReconnecting)
{
return false;
}
IsReconnecting = true;
return true;
}
/// <summary>
/// Marks the current reconnection attempt as complete.
/// Must be called in a finally block to ensure the guard is released.
/// </summary>
public void EndReconnection()
{
IsReconnecting = false;
}
private static bool IsIgnoredState(CallingState state)
=> state == CallingState.Reconnecting
|| state == CallingState.Migrating
|| state == CallingState.Joining
|| state == CallingState.Leaving
|| state == CallingState.Left
|| state == CallingState.ReconnectingFailed;
}
}