From 4e702caa6a0a875246724b45477a2e500468ac8b Mon Sep 17 00:00:00 2001 From: Kodi Craft Date: Tue, 15 Oct 2024 11:29:23 +0200 Subject: [PATCH] Parallelize BlockRegistry --- apps/amethyst/lib/amethyst.ex | 12 ++++++++++-- apps/amethyst/lib/blockregistry.ex | 14 +++++++++----- config/runtime.exs | 1 + 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/apps/amethyst/lib/amethyst.ex b/apps/amethyst/lib/amethyst.ex index b9d7921..564e1af 100644 --- a/apps/amethyst/lib/amethyst.ex +++ b/apps/amethyst/lib/amethyst.ex @@ -31,9 +31,17 @@ defmodule Amethyst.Application do child_spec: DynamicSupervisor.child_spec([]), name: Amethyst.GameMetaSupervisor }, - {Amethyst.BlockRegistry, %{}}, + {PartitionSupervisor, + child_spec: Amethyst.BlockRegistry.child_spec(%{}), + name: Amethyst.BlockRegistry}, Supervisor.child_spec( - {Task, fn -> Amethyst.DataGenerator.generate_and_populate_data(:latest, "/tmp/server.jar", "/tmp/amethyst-generated") end}, id: Amethyst.DataGenerator) + {Task, fn -> + Amethyst.DataGenerator.generate_and_populate_data( + :latest, + "/tmp/server.jar", + "/tmp/amethyst-generated") + end}, + id: Amethyst.DataGenerator) ] children = case Application.fetch_env!(:amethyst, :port) do diff --git a/apps/amethyst/lib/blockregistry.ex b/apps/amethyst/lib/blockregistry.ex index 505a32d..0b23117 100644 --- a/apps/amethyst/lib/blockregistry.ex +++ b/apps/amethyst/lib/blockregistry.ex @@ -22,9 +22,8 @@ defmodule Amethyst.BlockRegistry do """ require Logger - def start_link(initial) do - Logger.info("Starting BlockRegistry") - GenServer.start_link(__MODULE__, initial, name: __MODULE__) + def start_link(map) do + GenServer.start_link(__MODULE__, map) end def init(map) do @@ -44,6 +43,11 @@ defmodule Amethyst.BlockRegistry do end end + def handle_call(:debug, _from, state) do + Logger.debug("BlockRegistry state: #{inspect(state)}") + {:reply, :ok, state} + end + @doc """ Adds a block state to the registry. @@ -54,7 +58,7 @@ defmodule Amethyst.BlockRegistry do """ @spec add(String.t(), map(), integer()) :: :ok def add(id, bs, bsi) do - GenServer.cast(__MODULE__, {:add, id, bs, bsi}) + GenServer.cast({:via, PartitionSupervisor, {__MODULE__, id}}, {:add, id, bs, bsi}) end @doc """ @@ -64,6 +68,6 @@ defmodule Amethyst.BlockRegistry do """ @spec get(String.t(), map() | nil) :: integer() | nil def get(id, bs \\ %{}) do - GenServer.call(__MODULE__, {:get, id, bs}) + GenServer.call({:via, PartitionSupervisor, {__MODULE__, id}}, {:get, id, bs}) end end diff --git a/config/runtime.exs b/config/runtime.exs index 640bae0..f1301cc 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -6,4 +6,5 @@ config :amethyst, auth: true, # Whether or not users should be authenticated with Mojang. compression: 256, # Packets larger than this amount are sent compressed. Set to nil to disable compression. default_game: Example.Game, # Which game new players should be sent to + release: config_env() == :prod # If this is set to false, Amethyst will perform additional checks at runtime and will handle errors more loosely