feat: add ClientFlags bitfield with thread-safe holder
This commit is contained in:
41
src/NATS.Server/ClientFlags.cs
Normal file
41
src/NATS.Server/ClientFlags.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
namespace NATS.Server;
|
||||
|
||||
/// <summary>
|
||||
/// Connection state flags tracked per client.
|
||||
/// Corresponds to Go server/client.go clientFlag bitfield.
|
||||
/// Thread-safe via Interlocked operations on the backing int.
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum ClientFlags
|
||||
{
|
||||
ConnectReceived = 1 << 0,
|
||||
FirstPongSent = 1 << 1,
|
||||
HandshakeComplete = 1 << 2,
|
||||
CloseConnection = 1 << 3,
|
||||
WriteLoopStarted = 1 << 4,
|
||||
IsSlowConsumer = 1 << 5,
|
||||
ConnectProcessFinished = 1 << 6,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Thread-safe holder for client flags using Interlocked operations.
|
||||
/// </summary>
|
||||
public sealed class ClientFlagHolder
|
||||
{
|
||||
private int _flags;
|
||||
|
||||
public void SetFlag(ClientFlags flag)
|
||||
{
|
||||
Interlocked.Or(ref _flags, (int)flag);
|
||||
}
|
||||
|
||||
public void ClearFlag(ClientFlags flag)
|
||||
{
|
||||
Interlocked.And(ref _flags, ~(int)flag);
|
||||
}
|
||||
|
||||
public bool HasFlag(ClientFlags flag)
|
||||
{
|
||||
return (Volatile.Read(ref _flags) & (int)flag) != 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user