tgstation-server 6.12.0
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 catch (OperationCanceledException)
57 {
58 }
59 finally
60 {
61 onExpiry();
62 }
63 }
64
65 task = DisposeOnExipiry(cancellationTokenSource.Token);
66 }
67
69 public async ValueTask DisposeAsync()
70 {
73 await task;
74 }
75 }
76}
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.
ValueTask Delay(TimeSpan timeSpan, CancellationToken cancellationToken)
Create a Task that completes after a given timeSpan .