Skip to content

Commit 9e6405a

Browse files
committed
Processing stats size limited, convenient methods
1 parent 14cd889 commit 9e6405a

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace WhatNow.Contracts.Statistics
2+
{
3+
using System.Collections.Generic;
4+
5+
public class FixedSizedQueue<T> : Queue<T>
6+
{
7+
public int MaxCapacity { get; }
8+
9+
public FixedSizedQueue(int capacity) : base(capacity)
10+
=> MaxCapacity = capacity;
11+
12+
public new void Enqueue(T item)
13+
{
14+
if (MaxCapacity != 0 && MaxCapacity < Count + 1)
15+
Dequeue();
16+
17+
base.Enqueue(item);
18+
}
19+
}
20+
}

WhatNow/WhatNow.Essentials/ActionPipe.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ public class ActionPipe : IActionPipe
3131
public bool BreakRequested => Current.Any(a => a.BreakRequested);
3232

3333
readonly object executionsLock = new object();
34-
readonly Dictionary<Type, Queue<TimeSpan>> executions;
34+
readonly Dictionary<Type, FixedSizedQueue<TimeSpan>> executions;
3535

3636
public IEnumerable<BreakRequestReason> BreakReasons => actions
3737
.Select(a => a.Value.BreakRequestReason)
3838
.Where(br => br != null)
3939
.ToArray();
4040

41+
public const int MAX_QUEUE_SIZE = 100;
42+
4143
public ActionPipe(IActionPipeMap map, ActionToken actionToken, IDependencyResolver dependencyResolver)
4244
{
4345
ActionToken = actionToken;
@@ -50,7 +52,7 @@ public ActionPipe(IActionPipeMap map, ActionToken actionToken, IDependencyResolv
5052

5153
Current = new IAction[0];
5254

53-
executions = actions.Keys.ToDictionary(k => k, _ => new Queue<TimeSpan>());
55+
executions = actions.Keys.ToDictionary(k => k, _ => new FixedSizedQueue<TimeSpan>(MAX_QUEUE_SIZE));
5456
}
5557

5658
public void Restart()
@@ -63,6 +65,10 @@ public void Restart()
6365
.ToDictionary(k => k, t => (IAction)dependencyResolver.Resolve(t));
6466

6567
Current = new IAction[0];
68+
69+
lock (executionsLock)
70+
foreach (var type in executions.Values)
71+
type.Clear();
6672
}
6773

6874
public bool TryGetNextTask(IResourceManager resourceManager, TaskFactory taskFactory, out Task task)

WhatNow/WhatNow.Essentials/TaskList.cs

+21-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
using System;
2-
using System.Threading;
3-
using System.Threading.Tasks;
4-
using System.Collections.Generic;
5-
using Helios.Concurrency;
6-
using WhatNow.Contracts.ThreadPool;
7-
8-
namespace WhatNow.Essentials
1+
namespace WhatNow.Essentials
92
{
3+
using System;
4+
using System.Threading;
5+
using Helios.Concurrency;
6+
using System.Threading.Tasks;
7+
using System.Collections.Generic;
8+
using WhatNow.Contracts.ThreadPool;
9+
1010
public class TaskList : ITaskList
1111
{
1212
readonly CancellationTokenSource cancellationTokenSource;
@@ -59,7 +59,7 @@ public ITaskList With(IEnumerable<Action> actions)
5959

6060
return this;
6161
}
62-
62+
6363
public ITaskList With(Action<CancellationToken> action)
6464
{
6565
ThrowIfDisposed();
@@ -82,7 +82,16 @@ public void WaitAllFinished()
8282

8383
Task.WaitAll(tasks.ToArray());
8484

85-
ClearTasks();
85+
ClearCompletedTasks();
86+
}
87+
88+
public async Task WaitAllFinishedAsync()
89+
{
90+
ThrowIfDisposed();
91+
92+
await Task.WhenAll(tasks.ToArray());
93+
94+
ClearCompletedTasks();
8695
}
8796

8897
void ThrowIfDisposed()
@@ -102,13 +111,13 @@ public void Dispose()
102111
cancellationTokenSource?.Dispose();
103112
pool?.Dispose();
104113

105-
ClearTasks();
114+
ClearCompletedTasks();
106115

107116
disposed = true;
108117
}
109118
}
110119

111-
void ClearTasks()
120+
void ClearCompletedTasks()
112121
{
113122
foreach (var task in tasks)
114123
task.Dispose();

0 commit comments

Comments
 (0)