From 16c51b505cd47e82b85e59690b18d6352b6838d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Fri, 8 Jan 2021 12:16:53 +0100 Subject: [PATCH] Handle review comments --- lib/live_book/application.ex | 6 +++--- lib/live_book/session.ex | 18 +++++++++--------- lib/live_book/session_supervisor.ex | 14 +++++++------- lib/live_book/utils.ex | 13 +++++++++++++ mix.exs | 3 +-- mix.lock | 1 - 6 files changed, 33 insertions(+), 22 deletions(-) create mode 100644 lib/live_book/utils.ex diff --git a/lib/live_book/application.ex b/lib/live_book/application.ex index b095587c4..a743adf20 100644 --- a/lib/live_book/application.ex +++ b/lib/live_book/application.ex @@ -11,10 +11,10 @@ defmodule LiveBook.Application do LiveBookWeb.Telemetry, # Start the PubSub system {Phoenix.PubSub, name: LiveBook.PubSub}, - # Start the Endpoint (http/https) - LiveBookWeb.Endpoint, # Start the supervisor dynamically managing sessions - LiveBook.SessionSupervisor + LiveBook.SessionSupervisor, + # Start the Endpoint (http/https) + LiveBookWeb.Endpoint ] # See https://hexdocs.pm/elixir/Supervisor.html diff --git a/lib/live_book/session.ex b/lib/live_book/session.ex index 7f265df91..a8c648a2c 100644 --- a/lib/live_book/session.ex +++ b/lib/live_book/session.ex @@ -1,19 +1,19 @@ defmodule LiveBook.Session do - @moduledoc """ - Server corresponding to a single notebook session. + @moduledoc false - The process keeps the current notebook state and serves - as a source of truth that multiple clients talk to. - Receives update requests from the clients and notifies - them of any changes applied to the notebook. - """ + # Server corresponding to a single notebook session. + # + # The process keeps the current notebook state and serves + # as a source of truth that multiple clients talk to. + # Receives update requests from the clients and notifies + # them of any changes applied to the notebook. use GenServer, restart: :temporary @typedoc """ - A UUID assigned to every running session process. + An id assigned to every running session process. """ - @type session_id :: String.t() + @type session_id :: LiveBook.Utils.id() ## API diff --git a/lib/live_book/session_supervisor.ex b/lib/live_book/session_supervisor.ex index 6052f67c2..a477d0864 100644 --- a/lib/live_book/session_supervisor.ex +++ b/lib/live_book/session_supervisor.ex @@ -1,14 +1,14 @@ defmodule LiveBook.SessionSupervisor do - @moduledoc """ - Supervisor responsible for managing running notebook sessions. + @moduledoc false - Allows for creating new session processes on demand - and managing them using UUIDs. - """ + # Supervisor responsible for managing running notebook sessions. + # + # Allows for creating new session processes on demand + # and managing them using random ids. use DynamicSupervisor - alias LiveBook.Session + alias LiveBook.{Session, Utils} @name __MODULE__ @@ -28,7 +28,7 @@ defmodule LiveBook.SessionSupervisor do """ @spec create_session() :: {:ok, Session.session_id()} | {:error, any()} def create_session() do - id = UUID.uuid4() + id = Utils.random_id() case DynamicSupervisor.start_child(@name, {Session, id}) do {:ok, _} -> diff --git a/lib/live_book/utils.ex b/lib/live_book/utils.ex new file mode 100644 index 000000000..aa330a5d3 --- /dev/null +++ b/lib/live_book/utils.ex @@ -0,0 +1,13 @@ +defmodule LiveBook.Utils do + @moduledoc false + + @type id :: binary() + + @doc """ + Generates a random binary id. + """ + @spec random_id() :: binary() + def random_id() do + :crypto.strong_rand_bytes(20) |> Base.encode32(case: :lower) + end +end diff --git a/mix.exs b/mix.exs index 77e807b4d..e407de340 100644 --- a/mix.exs +++ b/mix.exs @@ -41,8 +41,7 @@ defmodule LiveBook.MixProject do {:telemetry_metrics, "~> 0.4"}, {:telemetry_poller, "~> 0.4"}, {:jason, "~> 1.0"}, - {:plug_cowboy, "~> 2.0"}, - {:elixir_uuid, "~> 1.2"} + {:plug_cowboy, "~> 2.0"} ] end diff --git a/mix.lock b/mix.lock index 6fe38bea4..31d2a80ec 100644 --- a/mix.lock +++ b/mix.lock @@ -2,7 +2,6 @@ "cowboy": {:hex, :cowboy, "2.8.0", "f3dc62e35797ecd9ac1b50db74611193c29815401e53bac9a5c0577bd7bc667d", [:rebar3], [{:cowlib, "~> 2.9.1", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "4643e4fba74ac96d4d152c75803de6fad0b3fa5df354c71afdd6cbeeb15fac8a"}, "cowboy_telemetry": {:hex, :cowboy_telemetry, "0.3.1", "ebd1a1d7aff97f27c66654e78ece187abdc646992714164380d8a041eda16754", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3a6efd3366130eab84ca372cbd4a7d3c3a97bdfcfb4911233b035d117063f0af"}, "cowlib": {:hex, :cowlib, "2.9.1", "61a6c7c50cf07fdd24b2f45b89500bb93b6686579b069a89f88cb211e1125c78", [:rebar3], [], "hexpm", "e4175dc240a70d996156160891e1c62238ede1729e45740bdd38064dad476170"}, - "elixir_uuid": {:hex, :elixir_uuid, "1.2.1", "dce506597acb7e6b0daeaff52ff6a9043f5919a4c3315abb4143f0b00378c097", [:mix], [], "hexpm", "f7eba2ea6c3555cea09706492716b0d87397b88946e6380898c2889d68585752"}, "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"}, "floki": {:hex, :floki, "0.29.0", "b1710d8c93a2f860dc2d7adc390dd808dc2fb8f78ee562304457b75f4c640881", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "008585ce64b9f74c07d32958ec9866f4b8a124bf4da1e2941b28e41384edaaad"}, "html_entities": {:hex, :html_entities, "0.5.1", "1c9715058b42c35a2ab65edc5b36d0ea66dd083767bef6e3edb57870ef556549", [:mix], [], "hexpm", "30efab070904eb897ff05cd52fa61c1025d7f8ef3a9ca250bc4e6513d16c32de"},