-
Notifications
You must be signed in to change notification settings - Fork 18
/
Router_RoundRobin.cs
404 lines (383 loc) · 17.2 KB
/
Router_RoundRobin.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
using LanguageExt;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Echo.Process;
using static LanguageExt.Prelude;
namespace Echo
{
public static partial class Router
{
/// <summary>
/// Spawns a new process with Count worker processes, each message is sent to one worker
/// process in a round robin fashion.
/// </summary>
/// <typeparam name="S">State type</typeparam>
/// <typeparam name="T">Message type</typeparam>
/// <param name="Name">Delegator process name</param>
/// <param name="Count">Number of worker processes</param>
/// <param name="Inbox">Worker message handler</param>
/// <param name="Flags">Process flags</param>
/// <param name="Strategy">Failure supervision strategy</param>
/// <returns>Process ID of the delegator process</returns>
public static ProcessId roundRobin<S, T>(
ProcessName Name,
int Count,
Func<S> Setup,
Func<S, T, S> Inbox,
ProcessFlags Flags = ProcessFlags.Default,
State<StrategyContext, Unit> Strategy = null,
int MaxMailboxSize = ProcessSetting.DefaultMailboxSize,
string WorkerName = "worker"
)
{
if (Inbox == null) throw new ArgumentNullException(nameof(Inbox));
if (Setup == null) throw new ArgumentNullException(nameof(Setup));
if (Count < 1) throw new ArgumentException($"{nameof(Count)} should be greater than 0");
return spawn<int, T>(
Name,
() =>
{
spawnMany(Count, WorkerName, Setup, Inbox, Flags);
return 0;
},
(index, msg) =>
{
index = index % Children.Count;
var child = Children.Skip(index).Take(1).ToArray();
if (child.Length == 0)
{
throw new NoRouterWorkersException();
}
else
{
fwd(child[0].Value);
}
return index + 1;
},
Flags,
Strategy,
MaxMailboxSize
);
}
/// <summary>
/// Spawns a new process with that routes each message to the Workers
/// in a round robin fashion.
/// </summary>
/// <typeparam name="S">State type</typeparam>
/// <typeparam name="T">Message type</typeparam>
/// <param name="Name">Delegator process name</param>
/// <param name="Count">Number of worker processes</param>
/// <param name="Inbox">Worker message handler</param>
/// <param name="Flags">Process flags</param>
/// <param name="Strategy">Failure supervision strategy</param>
/// <returns>Process ID of the delegator process</returns>
public static ProcessId roundRobin<T>(
ProcessName Name,
IEnumerable<ProcessId> Workers,
RouterOption Options = RouterOption.Default,
ProcessFlags Flags = ProcessFlags.Default,
int MaxMailboxSize = ProcessSetting.DefaultMailboxSize
)
{
if (Workers == null) throw new ArgumentNullException(nameof(Workers));
var workers = Workers.ToArray();
if (workers.Length < 1) throw new ArgumentException($"{nameof(Workers)} should have a length of at least 1");
var router = spawn<int, T>(
Name,
() => 0,
(index, msg) =>
{
index = index % workers.Length;
fwd(workers[index]);
return index + 1;
},
Flags,
DefaultStrategy,
MaxMailboxSize,
Terminated: (index, pid) => { workers = workers.Where(x => x != pid).ToArray(); return index; }
);
return WatchWorkers(router, workers, Options);
}
/// <summary>
/// Spawns a new process with Count worker processes, each message is mapped
/// and sent to one worker process in a round robin fashion.
/// </summary>
/// <typeparam name="S">State type</typeparam>
/// <typeparam name="T">Message type</typeparam>
/// <param name="Name">Delegator process name</param>
/// <param name="Count">Number of worker processes</param>
/// <param name="Inbox">Worker message handler</param>
/// <param name="Flags">Process flags</param>
/// <param name="Strategy">Failure supervision strategy</param>
/// <returns>Process ID of the delegator process</returns>
public static ProcessId roundRobinMap<S, T, U>(
ProcessName Name,
int Count,
Func<S> Setup,
Func<S, U, S> Inbox,
Func<T, U> Map,
ProcessFlags Flags = ProcessFlags.Default,
State<StrategyContext, Unit> Strategy = null,
int MaxMailboxSize = ProcessSetting.DefaultMailboxSize,
string WorkerName = "worker"
)
{
if (Inbox == null) throw new ArgumentNullException(nameof(Inbox));
if (Setup == null) throw new ArgumentNullException(nameof(Setup));
if (Count < 1) throw new ArgumentException($"{nameof(Count)} should be greater than 0");
return spawn<int, T>(
Name,
() =>
{
spawnMany(Count, WorkerName, Setup, Inbox, Flags);
return 0;
},
(index, msg) =>
{
var umsg = Map(msg);
index = index % Children.Count;
var child = Children.Skip(index).Take(1).ToArray();
if (child.Length == 0)
{
throw new NoRouterWorkersException();
}
else
{
fwd(child[0].Value, umsg);
}
return index + 1;
},
Flags,
Strategy,
MaxMailboxSize
);
}
/// <summary>
/// Spawns a new process with that routes each message is mapped and
/// sent to the Workers in a round robin fashion.
/// </summary>
/// <typeparam name="S">State type</typeparam>
/// <typeparam name="T">Message type</typeparam>
/// <param name="Name">Delegator process name</param>
/// <param name="Count">Number of worker processes</param>
/// <param name="Inbox">Worker message handler</param>
/// <param name="Flags">Process flags</param>
/// <param name="Strategy">Failure supervision strategy</param>
/// <returns>Process ID of the delegator process</returns>
public static ProcessId roundRobinMap<T, U>(
ProcessName Name,
IEnumerable<ProcessId> Workers,
Func<T, U> Map,
RouterOption Options = RouterOption.Default,
ProcessFlags Flags = ProcessFlags.Default,
int MaxMailboxSize = ProcessSetting.DefaultMailboxSize
)
{
if (Workers == null) throw new ArgumentNullException(nameof(Workers));
var workers = Workers.ToArray();
if (workers.Length < 1) throw new ArgumentException($"{nameof(Workers)} should have a length of at least 1");
var router = spawn<int, T>(
Name,
() => 0,
(index, msg) =>
{
var umsg = Map(msg);
index = index % workers.Length;
fwd(workers[index],umsg);
return index + 1;
},
Flags,
DefaultStrategy,
MaxMailboxSize,
Terminated: (index, pid) => { workers = workers.Where(x => x != pid).ToArray(); return index; }
);
return WatchWorkers(router, workers, Options);
}
/// <summary>
/// Spawns a new process with N worker processes, each message is mapped
/// from T to IEnumerable U before each resulting U is passed to the worker
/// processes in a round robin fashion.
/// </summary>
/// <typeparam name="T">Message type</typeparam>
/// <typeparam name="U">Mapped message type</typeparam>
/// <param name="Name">Delegator process name</param>
/// <param name="Count">Number of worker processes</param>
/// <param name="map">Maps the message from T to IEnumerable U before each one is passed to the workers</param>
/// <param name="Inbox">Worker message handler</param>
/// <param name="Flags">Process flags</param>
/// <param name="Strategy">Failure supervision strategy</param>
/// <returns>Process ID of the delegator process</returns>
public static ProcessId roundRobinMapMany<S, T, U>(
ProcessName Name,
int Count,
Func<S> Setup,
Func<S, U, S> Inbox,
Func<T, IEnumerable<U>> MapMany,
ProcessFlags Flags = ProcessFlags.Default,
State<StrategyContext, Unit> Strategy = null,
int MaxMailboxSize = ProcessSetting.DefaultMailboxSize,
string WorkerName = "worker"
)
{
if (Inbox == null) throw new ArgumentNullException(nameof(Inbox));
if (WorkerName == null) throw new ArgumentNullException(nameof(WorkerName));
if (Count < 1) throw new ArgumentException($"{nameof(Count)} should be greater than 0");
return spawn<int, T>(
Name,
() =>
{
spawnMany(Count, WorkerName, Setup, Inbox, Flags);
return 0;
},
(index, msg) =>
{
var us = MapMany(msg);
index = index % Children.Count;
var kids = Children.Skip(index);
foreach (var u in us)
{
index = index % Children.Count;
var child = kids.Take(1).ToArray();
if (child.Length == 0)
{
kids = Children;
child = kids.Take(1).ToArray();
if (child.Length == 0)
{
throw new NoRouterWorkersException();
}
}
fwd(child[0].Value, u);
kids = kids.Skip(1);
index++;
}
return index;
},
Flags,
Strategy,
MaxMailboxSize
);
}
/// <summary>
/// Spawns a new process with N worker processes, each message is mapped
/// from T to IEnumerable U before each resulting U is passed to the worker
/// processes in a round robin fashion.
/// </summary>
/// <typeparam name="T">Message type</typeparam>
/// <typeparam name="U">Mapped message type</typeparam>
/// <param name="Name">Delegator process name</param>
/// <param name="Count">Number of worker processes</param>
/// <param name="map">Maps the message from T to IEnumerable U before each one is passed to the workers</param>
/// <param name="Inbox">Worker message handler</param>
/// <param name="Flags">Process flags</param>
/// <param name="Strategy">Failure supervision strategy</param>
/// <returns>Process ID of the delegator process</returns>
public static ProcessId roundRobinMapMany<T, U>(
ProcessName Name,
IEnumerable<ProcessId> Workers,
Func<T, IEnumerable<U>> MapMany,
RouterOption Options = RouterOption.Default,
ProcessFlags Flags = ProcessFlags.Default,
int MaxMailboxSize = ProcessSetting.DefaultMailboxSize
)
{
if (Workers == null) throw new ArgumentNullException(nameof(Workers));
var workers = Workers.ToArray();
if (workers.Length < 1) throw new ArgumentException($"{nameof(Workers)} should have a length of at least 1");
var router = spawn<int, T>(
Name,
() => 0,
(index, msg) =>
{
var us = MapMany(msg);
foreach (var u in us)
{
index = index % workers.Length;
var worker = workers[index];
fwd(worker, u);
index++;
}
return index;
},
Flags,
DefaultStrategy,
MaxMailboxSize,
Terminated: (index, pid) => { workers = workers.Where(x => x != pid).ToArray(); return index; }
);
return WatchWorkers(router, workers, Options);
}
/// <summary>
/// Spawns a new process with Count worker processes, each message is sent to one worker
/// process in a round robin fashion.
/// </summary>
/// <typeparam name="S">State type</typeparam>
/// <typeparam name="T">Message type</typeparam>
/// <param name="Name">Delegator process name</param>
/// <param name="Count">Number of worker processes</param>
/// <param name="Inbox">Worker message handler</param>
/// <param name="Flags">Process flags</param>
/// <param name="Strategy">Failure supervision strategy</param>
/// <returns>Process ID of the delegator process</returns>
public static ProcessId roundRobin<T>(
ProcessName Name,
int Count,
Action<T> Inbox,
ProcessFlags Flags = ProcessFlags.Default,
State<StrategyContext, Unit> Strategy = null,
int MaxMailboxSize = ProcessSetting.DefaultMailboxSize,
string WorkerName = "worker"
) =>
roundRobin<Unit, T>(Name, Count, () => unit, (_, msg) => { Inbox(msg); return unit; }, Flags, Strategy, MaxMailboxSize, WorkerName);
/// <summary>
/// Spawns a new process with Count worker processes, each message is mapped
/// and sent to one worker process in a round robin fashion.
/// </summary>
/// <typeparam name="S">State type</typeparam>
/// <typeparam name="T">Message type</typeparam>
/// <param name="Name">Delegator process name</param>
/// <param name="Count">Number of worker processes</param>
/// <param name="Inbox">Worker message handler</param>
/// <param name="Flags">Process flags</param>
/// <param name="Strategy">Failure supervision strategy</param>
/// <returns>Process ID of the delegator process</returns>
public static ProcessId roundRobinMap<T, U>(
ProcessName Name,
int Count,
Action<U> Inbox,
Func<T, U> Map,
ProcessFlags Flags = ProcessFlags.Default,
State<StrategyContext, Unit> Strategy = null,
int MaxMailboxSize = ProcessSetting.DefaultMailboxSize,
string WorkerName = "worker"
) =>
roundRobinMap(Name, Count, () => unit, (_, umsg) => { Inbox(umsg); return unit; }, Map, Flags, Strategy, MaxMailboxSize, WorkerName);
/// <summary>
/// Spawns a new process with N worker processes, each message is mapped
/// from T to IEnumerable U before each resulting U is passed to the worker
/// processes in a round robin fashion.
/// </summary>
/// <typeparam name="T">Message type</typeparam>
/// <typeparam name="U">Mapped message type</typeparam>
/// <param name="Name">Delegator process name</param>
/// <param name="Count">Number of worker processes</param>
/// <param name="map">Maps the message from T to IEnumerable U before each one is passed to the workers</param>
/// <param name="Inbox">Worker message handler</param>
/// <param name="Flags">Process flags</param>
/// <param name="Strategy">Failure supervision strategy</param>
/// <returns>Process ID of the delegator process</returns>
public static ProcessId roundRobinMapMany<T, U>(
ProcessName Name,
int Count,
Action<U> Inbox,
Func<T, IEnumerable<U>> MapMany,
ProcessFlags Flags = ProcessFlags.Default,
State<StrategyContext, Unit> Strategy = null,
int MaxMailboxSize = ProcessSetting.DefaultMailboxSize,
string WorkerName = "worker"
) =>
roundRobinMapMany(Name, Count, () => unit, (_, umsg) => { Inbox(umsg); return unit; }, MapMany, Flags, Strategy, MaxMailboxSize, WorkerName);
}
}