Skip to content

Commit

Permalink
Update timer to not be runaway timer
Browse files Browse the repository at this point in the history
  • Loading branch information
cschuchardt88 committed Sep 26, 2023
1 parent f281516 commit 2c0391d
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/RestServer/WalletSessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,25 @@ namespace Neo.Plugins.RestServer
{
public class WalletSessionManager : ConcurrentDictionary<Guid, WalletSession>
{
private readonly Timer _timer;
private readonly PeriodicTimer _timer;

public WalletSessionManager()
{
_timer = new(SessionTimeout, null, TimeSpan.Zero, TimeSpan.FromSeconds(1));
_timer = new(TimeSpan.FromSeconds(1));
_ = Task.Run(SessionTimeout);
}

private void SessionTimeout(object data)
private async Task SessionTimeout()
{
var killAll = this.Where(w => w.Value.Expires <= DateTime.Now)
.Select(s => Task.Run(() =>
{
TryRemove(s);
}));
Task.WhenAll(killAll);
while (await _timer.WaitForNextTickAsync())
{
var killAll = this.Where(w => w.Value.Expires <= DateTime.Now)
.Select(s => Task.Run(() =>
{
TryRemove(s);
}));
await Task.WhenAll(killAll);
}
}
}

Expand Down

0 comments on commit 2c0391d

Please sign in to comment.