-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
InProcessMessageBus.cs
205 lines (171 loc) · 6.37 KB
/
InProcessMessageBus.cs
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using SignalR.Infrastructure;
namespace SignalR
{
public class InProcessMessageBus : IMessageBus
{
private static List<Message> _emptyMessageList = new List<Message>();
private readonly ConcurrentDictionary<string, LockedList<Action<IEnumerable<Message>>>> _waitingTasks =
new ConcurrentDictionary<string, LockedList<Action<IEnumerable<Message>>>>();
private readonly ConcurrentDictionary<string, LockedList<Message>> _cache =
new ConcurrentDictionary<string, LockedList<Message>>();
private readonly object _messageCreationLock = new object();
private readonly TimeSpan _cleanupInterval = TimeSpan.FromSeconds(10);
private ulong _lastMessageId = 0;
private long _gcRunning = 0;
private readonly Timer _timer;
public InProcessMessageBus()
: this(garbageCollectMessages: true)
{
}
public InProcessMessageBus(bool garbageCollectMessages)
{
if (garbageCollectMessages)
{
_timer = new Timer(RemoveExpiredEntries, null, _cleanupInterval, _cleanupInterval);
}
}
public Task<IEnumerable<Message>> GetMessagesSince(IEnumerable<string> eventKeys, ulong? id = null)
{
if (id == null)
{
// Wait for new messages
return WaitForMessages(eventKeys);
}
var messages = eventKeys.SelectMany(key => GetMessagesSince(key, id.Value));
if (messages.Any())
{
// Messages already in store greater than last received id so return them
return TaskAsyncHelper.FromResult((IEnumerable<Message>)messages.OrderBy(msg => msg.Id));
}
// Wait for new messages
return WaitForMessages(eventKeys);
}
public Task Send(string eventKey, object value)
{
var list = _cache.GetOrAdd(eventKey, _ => new LockedList<Message>());
Message message = null;
lock (_messageCreationLock)
{
// Only 1 save allowed at a time, to ensure messages are added to the list in order
message = new Message(eventKey, GenerateId(), value);
list.Add(message);
}
// Send to waiting callers
LockedList<Action<IEnumerable<Message>>> taskCompletionSources;
if (_waitingTasks.TryGetValue(eventKey, out taskCompletionSources))
{
var delegates = taskCompletionSources.Copy();
var messages = new[] { message };
foreach (var callback in delegates)
{
if (callback != null)
{
callback.Invoke(messages);
}
}
}
return TaskAsyncHelper.Empty;
}
private ulong GenerateId()
{
// do some crazy shit here
return ++_lastMessageId;
}
private IEnumerable<Message> GetMessagesSince(string eventKey, ulong id)
{
LockedList<Message> store;
List<Message> list = null;
if (_cache.TryGetValue(eventKey, out store))
{
list = store.Copy();
}
if (list == null || list.Count == 0)
{
return _emptyMessageList;
}
if (list.Count > 0 && list[0].Id > id)
{
// All messages in the list are greater than the last message
return list;
}
var index = list.FindLastIndex(msg => msg.Id <= id);
if (index < 0)
{
return _emptyMessageList;
}
var startIndex = index + 1;
if (startIndex >= list.Count)
{
return _emptyMessageList;
}
return list.GetRange(startIndex, list.Count - startIndex);
}
private Task<IEnumerable<Message>> WaitForMessages(IEnumerable<string> eventKeys)
{
var tcs = new TaskCompletionSource<IEnumerable<Message>>();
int callbackCalled = 0;
Action<IEnumerable<Message>> callback = null;
callback = messages =>
{
if (Interlocked.Exchange(ref callbackCalled, 1) == 0)
{
tcs.SetResult(messages);
}
foreach (var eventKey in eventKeys)
{
LockedList<Action<IEnumerable<Message>>> callbacks;
if (_waitingTasks.TryGetValue(eventKey, out callbacks))
{
callbacks.Remove(callback);
}
}
};
foreach (var eventKey in eventKeys)
{
var handlers = _waitingTasks.GetOrAdd(eventKey, _ => new LockedList<Action<IEnumerable<Message>>>());
handlers.Add(callback);
}
return tcs.Task;
}
private void RemoveExpiredEntries(object state)
{
if (Interlocked.Exchange(ref _gcRunning, 1) == 1 || Debugger.IsAttached)
{
return;
}
try
{
// Take a snapshot of the entries
var entries = _cache.ToList();
// Remove all the expired ones
foreach (var entry in entries)
{
var messages = entry.Value.Copy();
foreach (var item in messages)
{
if (item.Expired)
{
entry.Value.Remove(item);
}
}
}
}
catch (Exception ex)
{
// Exception on bg thread, bad! Log and swallow to stop the process exploding
Trace.TraceError("Error during InProcessMessageStore clean up on background thread: {0}", ex);
}
finally
{
Interlocked.Exchange(ref _gcRunning, 0);
}
}
}
}