tgstation-server 5.12.7
The /tg/station 13 server suite
Loading...
Searching...
No Matches
IdentityCacheObject.cs
Go to the documentation of this file.
1using System;
2using System.Threading;
3using System.Threading.Tasks;
4
6
8{
13 {
18
22 readonly CancellationTokenSource cancellationTokenSource;
23
27 readonly Task task;
28
36 public IdentityCacheObject(ISystemIdentity systemIdentity, IAsyncDelayer asyncDelayer, Action onExpiry, DateTimeOffset expiry)
37 {
38 SystemIdentity = systemIdentity ?? throw new ArgumentNullException(nameof(systemIdentity));
39
40 ArgumentNullException.ThrowIfNull(asyncDelayer);
41
42 ArgumentNullException.ThrowIfNull(onExpiry);
43 var now = DateTimeOffset.UtcNow;
44 if (expiry < now)
45 throw new ArgumentOutOfRangeException(nameof(expiry), expiry, "expiry must be greater than DateTimeOffset.UtcNow!");
46
47 cancellationTokenSource = new CancellationTokenSource();
48
49 async Task DisposeOnExipiry(CancellationToken cancellationToken)
50 {
51 using (SystemIdentity)
52 try
53 {
54 await asyncDelayer.Delay(expiry - now, cancellationToken);
55 }
56 finally
57 {
58 onExpiry();
59 }
60 }
61
62 task = DisposeOnExipiry(cancellationTokenSource.Token);
63 }
64
66 public void Dispose()
67 {
69 try
70 {
71 task.GetAwaiter().GetResult();
72 }
73 catch (OperationCanceledException)
74 {
75 }
76
78 }
79 }
80}
For keeping a specific ISystemIdentity alive for a period of time.
readonly Task task
The Task to clean up SystemIdentity.
IdentityCacheObject(ISystemIdentity systemIdentity, IAsyncDelayer asyncDelayer, Action onExpiry, DateTimeOffset expiry)
Initializes a new instance of the IdentityCacheObject class.
ISystemIdentity SystemIdentity
The ISystemIdentity the IdentityCache manages.
readonly CancellationTokenSource cancellationTokenSource
The cancellationTokenSource for the IdentityCache.
Represents a user on the current global::System.Runtime.InteropServices.OSPlatform.
Task Delay(TimeSpan timeSpan, CancellationToken cancellationToken)
Create a Task that completes after a given timeSpan .