-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deterministic session management. (#1268)
* Deterministic session management. * Create session collection. * Fix test. * Fix theme test. * Remove wait dbugger.
- Loading branch information
1 parent
e70a84e
commit 5c12794
Showing
10 changed files
with
150 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using UniversalDashboard.Models; | ||
|
||
namespace UniversalDashboard.Services | ||
{ | ||
public interface ISessionManager | ||
{ | ||
void StartSession(string id); | ||
void EndSession(string id); | ||
bool SessionExists(string id); | ||
SessionState GetSession(string id); | ||
ConcurrentDictionary<string, SessionState> Sessions { get; } | ||
void ClearTimedOutSessions(TimeSpan timespan); | ||
} | ||
|
||
public class SessionManager : ISessionManager | ||
{ | ||
public ConcurrentDictionary<string, SessionState> Sessions { get; private set; } | ||
|
||
public SessionManager() | ||
{ | ||
Sessions = new ConcurrentDictionary<string, SessionState>(); | ||
} | ||
|
||
public void StartSession(string id) | ||
{ | ||
if (Sessions.ContainsKey(id)) | ||
{ | ||
var session = Sessions[id]; | ||
session.LastTouched = DateTime.UtcNow; | ||
session.Connections++; | ||
} | ||
else | ||
{ | ||
Sessions.TryAdd(id, new SessionState(id)); | ||
} | ||
} | ||
|
||
public void EndSession(string id) | ||
{ | ||
var session = Sessions[id]; | ||
session.LastTouched = DateTime.UtcNow; | ||
session.Connections--; | ||
} | ||
|
||
public bool SessionExists(string id) | ||
{ | ||
return Sessions.ContainsKey(id); | ||
} | ||
|
||
public SessionState GetSession(string id) | ||
{ | ||
var session = Sessions[id]; | ||
session.LastTouched = DateTime.UtcNow; | ||
return session; | ||
} | ||
|
||
public void ClearTimedOutSessions(TimeSpan timespan) | ||
{ | ||
var toRemove = new List<string>(); | ||
foreach(var key in Sessions.Keys) | ||
{ | ||
var session = Sessions[key]; | ||
if (session.LastTouched < DateTime.UtcNow - timespan) | ||
{ | ||
toRemove.Add(session.Id); | ||
} | ||
} | ||
|
||
toRemove.ForEach(x => { | ||
Sessions.TryRemove(x, out SessionState value); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Quartz; | ||
using UniversalDashboard.Execution; | ||
|
||
namespace UniversalDashboard.Services | ||
{ | ||
public class SessionTimeoutJob : IJob | ||
{ | ||
public TimeSpan IdleTimeout { get; set; } | ||
public async Task Execute(IJobExecutionContext context) | ||
{ | ||
await Task.CompletedTask; | ||
EndpointService.Instance.SessionManager.ClearTimedOutSessions(IdleTimeout); | ||
} | ||
} | ||
} |