-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSubscription.cs
More file actions
95 lines (76 loc) · 2.56 KB
/
Copy pathSubscription.cs
File metadata and controls
95 lines (76 loc) · 2.56 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using ManagedCode.Orleans.SignalR.Core.Interfaces;
using Orleans.Runtime;
namespace ManagedCode.Orleans.SignalR.Core.SignalR.Observers;
public sealed class Subscription(SignalRObserver observer) : IDisposable
{
// Use ConcurrentDictionary as a concurrent hash-set because batch group mutations can overlap disconnect cleanup.
private readonly ConcurrentDictionary<IObserverConnectionManager, bool> _grains = new();
private readonly ConcurrentDictionary<GrainId, bool> _heartbeatGrainIds = new();
private bool _disposed;
public ISignalRObserver Reference { get; private set; } = default!;
public string? HubKey { get; private set; }
public bool UsePartitioning { get; private set; }
public int PartitionId { get; private set; }
public IReadOnlyCollection<IObserverConnectionManager> Grains => _grains.IsEmpty ? [] : [.. _grains.Keys];
public void Dispose()
{
if (_disposed)
{
return;
}
_disposed = true;
observer?.Dispose();
_grains.Clear();
_heartbeatGrainIds.Clear();
Reference = null!;
HubKey = null;
UsePartitioning = false;
PartitionId = 0;
}
public void AddGrain(IObserverConnectionManager grain)
{
_grains.TryAdd(grain, true);
_heartbeatGrainIds.TryAdd(((GrainReference)grain).GrainId, true);
}
public void RemoveGrain(IObserverConnectionManager grain)
{
_grains.TryRemove(grain, out _);
_heartbeatGrainIds.TryRemove(((GrainReference)grain).GrainId, out _);
}
public void ClearGrains()
{
_grains.Clear();
_heartbeatGrainIds.Clear();
}
public void SetReference(ISignalRObserver reference)
{
Reference = reference;
}
public void SetConnectionMetadata(string hubKey, bool usePartitioning, int partitionId)
{
HubKey = hubKey;
UsePartitioning = usePartitioning;
PartitionId = partitionId;
}
public SignalRObserver GetObserver()
{
return observer;
}
public ImmutableArray<GrainId> GetHeartbeatGrainIds()
{
if (_heartbeatGrainIds.IsEmpty)
{
return ImmutableArray<GrainId>.Empty;
}
var builder = ImmutableArray.CreateBuilder<GrainId>(_heartbeatGrainIds.Count);
foreach (var grainId in _heartbeatGrainIds.Keys)
{
builder.Add(grainId);
}
return builder.MoveToImmutable();
}
}