diff options
author | Logan Hunt <loganhunt@simponic.xyz> | 2022-12-30 05:53:15 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-30 05:53:15 -0700 |
commit | 52fbc0b0161d2d30583d7f04eef57a29e441e1d2 (patch) | |
tree | f882037a2820973ba6dc5f64206b01dba03266d8 /lib/chessh/schema/player_session.ex | |
parent | c143bb549c53f2737c41cdfce6cc2598c5489bdc (diff) | |
parent | e7d8c61487815bd90ec5834dad5e6f64dd951b53 (diff) | |
download | chessh-52fbc0b0161d2d30583d7f04eef57a29e441e1d2.tar.gz chessh-52fbc0b0161d2d30583d7f04eef57a29e441e1d2.zip |
Merge pull request #2 from Simponic/erlang_ssh_server
Erlang ssh server #2
Diffstat (limited to 'lib/chessh/schema/player_session.ex')
-rw-r--r-- | lib/chessh/schema/player_session.ex | 52 |
1 files changed, 49 insertions, 3 deletions
diff --git a/lib/chessh/schema/player_session.ex b/lib/chessh/schema/player_session.ex index 84f15ee..ce3fc1f 100644 --- a/lib/chessh/schema/player_session.ex +++ b/lib/chessh/schema/player_session.ex @@ -1,10 +1,12 @@ defmodule Chessh.PlayerSession do - alias Chessh.Repo + alias Chessh.{Repo, Player, PlayerSession, Utils} use Ecto.Schema import Ecto.{Query, Changeset} + require Logger schema "player_sessions" do - field(:login, :utc_datetime) + field(:process, :string) + field(:login, :utc_datetime_usec) belongs_to(:node, Chessh.Node, type: :string) belongs_to(:player, Chessh.Player) @@ -17,7 +19,7 @@ defmodule Chessh.PlayerSession do def concurrent_sessions(player) do Repo.aggregate( - from(p in Chessh.PlayerSession, + from(p in PlayerSession, where: p.player_id == ^player.id ), :count @@ -31,4 +33,48 @@ defmodule Chessh.PlayerSession do ) ) end + + def player_within_concurrent_sessions_and_satisfies(username, auth_fn) do + max_sessions = + Application.get_env(:chessh, RateLimits) + |> Keyword.get(:max_concurrent_user_sessions) + + Repo.transaction(fn -> + case Repo.one( + from(p in Player, + where: p.username == ^String.Chars.to_string(username), + lock: "FOR UPDATE" + ) + ) do + nil -> + Logger.error("Player with username #{username} does not exist") + send(self(), {:authed, false}) + + player -> + authed = + auth_fn.(player) && + PlayerSession.concurrent_sessions(player) < max_sessions + + Repo.insert(%PlayerSession{ + login: DateTime.utc_now(), + node_id: System.fetch_env!("NODE_ID"), + player: player, + # TODO: This PID may be wrong - need to determine if this PID is shared with disconnectfun + process: Utils.pid_to_str(self()) + }) + + player + |> Player.authentications_changeset(%{authentications: player.authentications + 1}) + |> Repo.update() + + send(self(), {:authed, authed}) + end + end) + + receive do + {:authed, authed} -> authed + after + 3_000 -> false + end + end end |