Skip to content

Commit e030fc1

Browse files
committed
Added immutable keys for state items Name and ManagedThreadId.
1 parent 2a5d7c1 commit e030fc1

4 files changed

Lines changed: 31 additions & 5 deletions

File tree

Shuttle.Core.Threading/ProcessorThread.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public class ProcessorThread
1414
private readonly CancellationTokenSource _cancellationTokenSource = new();
1515
private readonly ProcessorThreadOptions _processorThreadOptions;
1616

17-
private readonly Dictionary<string, object> _state = new();
1817
private readonly ProcessorThreadEventArgs _eventArgs;
1918

2019
private bool _started;
@@ -36,6 +35,9 @@ public ProcessorThread(string name, IServiceScopeFactory serviceScopeFactory, IP
3635
_thread.Priority = _processorThreadOptions.Priority;
3736

3837
_eventArgs = new(Name, _thread.ManagedThreadId);
38+
39+
State.Add("Name", Name);
40+
State.Add("ManagedThreadId", _thread.ManagedThreadId);
3941
}
4042

4143
public CancellationToken CancellationToken { get; }

Shuttle.Core.Threading/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Shuttle.Core.Threading/Resources.resx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,8 @@
154154
<data name="ProcessorThreadJoinTimeoutException" xml:space="preserve">
155155
<value>Processor thread did not join within the required time.</value>
156156
</data>
157+
<data name="ImmutableKeyException" xml:space="preserve">
158+
<value>Cannot remove or replace key '{0}'.</value>
159+
<comment>{0} = the immutable key</comment>
160+
</data>
157161
</root>

Shuttle.Core.Threading/State.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
using System;
12
using System.Collections.Generic;
3+
using System.Threading;
24
using Shuttle.Core.Contract;
35

46
namespace Shuttle.Core.Threading;
57

68
public class State : IState
79
{
10+
private readonly List<string> _immutableKeys = ["Name", "ManagedThreadId"];
811
private readonly Dictionary<string, object?> _state = new();
912

1013
public void Clear()
@@ -14,29 +17,37 @@ public void Clear()
1417

1518
public void Add(string key, object? value)
1619
{
17-
_state.Add(Guard.AgainstNull(key), value);
20+
_state.Add(Guard.AgainstNullOrEmptyString(key), value);
1821
}
1922

2023
public void Replace(string key, object? value)
2124
{
22-
Guard.AgainstNull(key);
25+
if (_immutableKeys.Contains(Guard.AgainstNullOrEmptyString(key)))
26+
{
27+
throw new InvalidOperationException(string.Format(Resources.ImmutableKeyException, key));
28+
}
2329

2430
_state.Remove(key);
2531
_state.Add(key, value);
2632
}
2733

2834
public object? Get(string key)
2935
{
30-
return _state.TryGetValue(Guard.AgainstNull(key), out var result) ? result : default;
36+
return _state.TryGetValue(Guard.AgainstNullOrEmptyString(key), out var result) ? result : default;
3137
}
3238

3339
public bool Contains(string key)
3440
{
35-
return _state.ContainsKey(Guard.AgainstNull(key));
41+
return _state.ContainsKey(Guard.AgainstNullOrEmptyString(key));
3642
}
3743

3844
public bool Remove(string key)
3945
{
46+
if (_immutableKeys.Contains(Guard.AgainstNullOrEmptyString(key)))
47+
{
48+
throw new InvalidOperationException(string.Format(Resources.ImmutableKeyException, key));
49+
}
50+
4051
return _state.Remove(key);
4152
}
4253
}

0 commit comments

Comments
 (0)