|
1 |
| -using RabbitMQ.Util; |
2 |
| -using System; |
3 |
| -using System.Collections.Generic; |
| 1 | +using System; |
| 2 | +using System.Collections.Concurrent; |
4 | 3 | using System.Threading;
|
5 |
| -using System.Threading.Tasks; |
6 | 4 |
|
7 | 5 | namespace RabbitMQ.Client
|
8 | 6 | {
|
9 | 7 | public class ConsumerWorkService
|
10 | 8 | {
|
11 |
| - public const int MAX_THUNK_EXECUTION_BATCH_SIZE = 16; |
12 |
| - private TaskScheduler scheduler; |
13 |
| - private BatchingWorkPool<IModel, Action> workPool; |
| 9 | + readonly ConcurrentDictionary<IModel, WorkPool> workPools = new ConcurrentDictionary<IModel, WorkPool>(); |
14 | 10 |
|
15 |
| - public ConsumerWorkService() : |
16 |
| - this(TaskScheduler.Default) { } |
17 |
| - |
18 |
| - public ConsumerWorkService(TaskScheduler scheduler) |
19 |
| - { |
20 |
| - this.scheduler = scheduler; |
21 |
| - this.workPool = new BatchingWorkPool<IModel, Action>(); |
22 |
| - } |
23 |
| - |
24 |
| - public void ExecuteThunk() |
| 11 | + public void AddWork(IModel model, Action fn) |
25 | 12 | {
|
26 |
| - var actions = new List<Action>(MAX_THUNK_EXECUTION_BATCH_SIZE); |
| 13 | + // two step approach is taken, as TryGetValue does not aquire locks |
| 14 | + // if this fails, GetOrAdd is called, which takes a lock |
27 | 15 |
|
28 |
| - try |
| 16 | + WorkPool workPool; |
| 17 | + if (workPools.TryGetValue(model, out workPool) == false) |
29 | 18 | {
|
30 |
| - IModel key = this.workPool.NextWorkBlock(ref actions, MAX_THUNK_EXECUTION_BATCH_SIZE); |
31 |
| - if (key == null) { return; } |
| 19 | + var newWorkPool = new WorkPool(model); |
| 20 | + workPool = workPools.GetOrAdd(model, newWorkPool); |
32 | 21 |
|
33 |
| - try |
34 |
| - { |
35 |
| - foreach (var fn in actions) |
36 |
| - { |
37 |
| - fn(); |
38 |
| - } |
39 |
| - } |
40 |
| - finally |
| 22 | + // start if it's only the workpool that has been just created |
| 23 | + if (newWorkPool == workPool) |
41 | 24 | {
|
42 |
| - if (this.workPool.FinishWorkBlock(key)) |
43 |
| - { |
44 |
| - var t = new Task(new Action(ExecuteThunk)); |
45 |
| - t.Start(this.scheduler); |
46 |
| - } |
| 25 | + newWorkPool.Start(); |
47 | 26 | }
|
48 | 27 | }
|
49 |
| - catch (Exception) |
50 |
| - { |
51 |
| -#if NETFX_CORE |
52 |
| - // To end a task, return |
53 |
| - return; |
54 |
| -#else |
55 |
| - //Thread.CurrentThread.Interrupt(); //TODO: what to do? |
56 |
| -#endif |
57 |
| - } |
| 28 | + |
| 29 | + workPool.Enqueue(fn); |
58 | 30 | }
|
59 | 31 |
|
60 |
| - public void AddWork(IModel model, Action fn) |
| 32 | + public void StopWork(IModel model) |
61 | 33 | {
|
62 |
| - if (this.workPool.AddWorkItem(model, fn)) |
| 34 | + WorkPool workPool; |
| 35 | + if (workPools.TryRemove(model, out workPool)) |
63 | 36 | {
|
64 |
| - var t = new Task(new Action(ExecuteThunk)); |
65 |
| - t.Start(this.scheduler); |
| 37 | + workPool.Stop(); |
66 | 38 | }
|
67 | 39 | }
|
68 | 40 |
|
69 |
| - public void RegisterKey(IModel model) |
| 41 | + public void StopWork() |
70 | 42 | {
|
71 |
| - this.workPool.RegisterKey(model); |
| 43 | + foreach (var model in workPools.Keys) |
| 44 | + { |
| 45 | + StopWork(model); |
| 46 | + } |
72 | 47 | }
|
73 | 48 |
|
74 |
| - public void StopWork(IModel model) |
| 49 | + class WorkPool |
75 | 50 | {
|
76 |
| - this.workPool.UnregisterKey(model); |
77 |
| - } |
| 51 | + readonly ConcurrentQueue<Action> actions; |
| 52 | + readonly AutoResetEvent messageArrived; |
| 53 | + readonly TimeSpan waitTime; |
| 54 | + readonly CancellationTokenSource tokenSource; |
| 55 | + readonly string name; |
78 | 56 |
|
79 |
| - public void StopWork() |
80 |
| - { |
81 |
| - this.workPool.UnregisterAllKeys(); |
| 57 | + public WorkPool(IModel model) |
| 58 | + { |
| 59 | + name = model.ToString(); |
| 60 | + actions = new ConcurrentQueue<Action>(); |
| 61 | + messageArrived = new AutoResetEvent(false); |
| 62 | + waitTime = TimeSpan.FromMilliseconds(100); |
| 63 | + tokenSource = new CancellationTokenSource(); |
| 64 | + } |
| 65 | + |
| 66 | + public void Start() |
| 67 | + { |
| 68 | +#if NETFX_CORE |
| 69 | + Task.Factory.StartNew(Loop, TaskCreationOptions.LongRunning); |
| 70 | +#else |
| 71 | + var thread = new Thread(Loop) |
| 72 | + { |
| 73 | + Name = "WorkPool-" + name, |
| 74 | + IsBackground = true |
| 75 | + }; |
| 76 | + thread.Start(); |
| 77 | +#endif |
| 78 | + } |
| 79 | + |
| 80 | + public void Enqueue(Action action) |
| 81 | + { |
| 82 | + actions.Enqueue(action); |
| 83 | + messageArrived.Set(); |
| 84 | + } |
| 85 | + |
| 86 | + void Loop() |
| 87 | + { |
| 88 | + while (tokenSource.IsCancellationRequested == false) |
| 89 | + { |
| 90 | + Action action; |
| 91 | + while (actions.TryDequeue(out action)) |
| 92 | + { |
| 93 | + try |
| 94 | + { |
| 95 | + action(); |
| 96 | + } |
| 97 | + catch (Exception) |
| 98 | + { |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + messageArrived.WaitOne(waitTime); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public void Stop() |
| 107 | + { |
| 108 | + tokenSource.Cancel(); |
| 109 | + } |
82 | 110 | }
|
83 | 111 | }
|
84 | 112 | }
|
0 commit comments