summaryrefslogtreecommitdiff
path: root/lib/chessh/schema
diff options
context:
space:
mode:
authorSimponic <loganhunt@simponic.xyz>2022-12-31 16:32:56 -0700
committerSimponic <loganhunt@simponic.xyz>2022-12-31 16:33:12 -0700
commit52a3ed7c5700fa398efb8a4aff9d586a850e3d58 (patch)
tree0ac4b127c99d399265159182edf4dc63bd9ba675 /lib/chessh/schema
parent58d0b1a89c461467c9ea6229f9a6b3d5ed573da5 (diff)
downloadchessh-52a3ed7c5700fa398efb8a4aff9d586a850e3d58.tar.gz
chessh-52a3ed7c5700fa398efb8a4aff9d586a850e3d58.zip
Better logging, close previous sessions once session threshold has been reached
Diffstat (limited to 'lib/chessh/schema')
-rw-r--r--lib/chessh/schema/player_session.ex29
1 files changed, 25 insertions, 4 deletions
diff --git a/lib/chessh/schema/player_session.ex b/lib/chessh/schema/player_session.ex
index 57803cb..8ca338c 100644
--- a/lib/chessh/schema/player_session.ex
+++ b/lib/chessh/schema/player_session.ex
@@ -34,7 +34,7 @@ defmodule Chessh.PlayerSession do
)
end
- def player_within_concurrent_sessions_and_satisfies(username, auth_fn) do
+ def update_sessions_and_player_satisfies(username, auth_fn) do
max_sessions =
Application.get_env(:chessh, RateLimits)
|> Keyword.get(:max_concurrent_user_sessions)
@@ -51,9 +51,7 @@ defmodule Chessh.PlayerSession do
send(self(), {:authed, false})
player ->
- authed =
- auth_fn.(player) &&
- PlayerSession.concurrent_sessions(player) < max_sessions
+ authed = auth_fn.(player)
if authed do
Logger.debug(
@@ -67,6 +65,29 @@ defmodule Chessh.PlayerSession do
process: Utils.pid_to_str(self())
})
+ concurrent_sessions = PlayerSession.concurrent_sessions(player)
+
+ if concurrent_sessions > max_sessions do
+ expired_sessions =
+ Repo.all(
+ from(p in PlayerSession,
+ select: p.id,
+ order_by: [asc: :login],
+ limit: ^(concurrent_sessions - max_sessions)
+ )
+ )
+
+ Logger.debug(
+ "Player #{player.username} has #{length(expired_sessions)} expired sessions - attempting to close them"
+ )
+
+ Enum.map(expired_sessions, fn session_id ->
+ :syn.publish(:player_sessions, {:session, session_id}, :session_closed)
+ end)
+
+ Repo.delete_all(from(p in PlayerSession, where: p.id in ^expired_sessions))
+ end
+
player
|> Player.authentications_changeset(%{authentications: player.authentications + 1})
|> Repo.update()