Skip to content

Commit

Permalink
Merge branch 'init-caches' into merge-queue-attempt-1
Browse files Browse the repository at this point in the history
  • Loading branch information
geekingfrog committed Jul 6, 2024
2 parents 3201139 + 24702e5 commit c6daa91
Show file tree
Hide file tree
Showing 15 changed files with 443 additions and 211 deletions.
91 changes: 91 additions & 0 deletions lib/teiserver/account/caches/permission_cache.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
defmodule Teiserver.Account.PermissionCache do
@moduledoc """
Define caches and set them up for permission related things
"""

use Supervisor
import Teiserver.Account.AuthLib, only: [add_permission_set: 3]
alias Teiserver.Helpers.CacheHelper

def start_link(opts) do
with {:ok, sup} <- Supervisor.start_link(__MODULE__, :ok, opts),
:ok <- warm_permission_cache() do
warm_restriction_cache()
{:ok, sup}
end
end

@impl true
def init(:ok) do
children = [
CacheHelper.concache_perm_sup(:auth_group_store),
CacheHelper.concache_perm_sup(:restriction_lookup_store)
]

Supervisor.init(children, strategy: :one_for_all)
end

defp warm_permission_cache() do
add_permission_set("admin", "debug", ~w(debug))
add_permission_set("admin", "dev", ~w(developer structure))
add_permission_set("admin", "admin", ~w(limited full))
add_permission_set("admin", "report", ~w(show update delete report))
add_permission_set("admin", "user", ~w(show create update delete report))
add_permission_set("admin", "group", ~w(show create update delete report config))
add_permission_set("teiserver", "admin", ~w(account battle clan queue))

add_permission_set(
"teiserver",
"staff",
~w(overwatch reviewer moderator admin communication clan telemetry server)
)

add_permission_set("teiserver", "dev", ~w(infolog))
add_permission_set("teiserver", "reports", ~w(client server match ratings infolog))
add_permission_set("teiserver", "api", ~w(battle))

add_permission_set(
"teiserver",
"player",
~w(account tester contributor dev streamer donor verified bot moderator)
)

:ok = Teiserver.Logging.Startup.startup()

:ok
end

defp warm_restriction_cache() do
# Chat stuff
Teiserver.Account.UserLib.add_report_restriction_types("Chat", [
"Bridging",
"Game chat",
"Room chat",
"All chat"
])

# Lobby interaction
Teiserver.Account.UserLib.add_report_restriction_types("Game", [
"Low priority",
"All lobbies",
"Login",
"Permanently banned"
])

Teiserver.Account.UserLib.add_report_restriction_types("Other", [
"Accolades",
"Boss",
"Reporting",
"Renaming",
"Matchmaking"
])

Teiserver.Account.UserLib.add_report_restriction_types("Warnings", [
"Warning reminder"
])

Teiserver.Account.UserLib.add_report_restriction_types("Internal", [
"Note"
])
end
end
57 changes: 11 additions & 46 deletions lib/teiserver/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ defmodule Teiserver.Application do
alias Phoenix.PubSub
require Logger

import Teiserver.Helpers.CacheHelper,
only: [concache_sup: 1, concache_sup: 2, concache_perm_sup: 1]

@impl true
def start(_type, _args) do
# List all child processes to be supervised
Expand Down Expand Up @@ -46,18 +49,13 @@ defmodule Teiserver.Application do
concache_sup(:account_avoiding_this_cache),
concache_sup(:account_blocking_this_cache),
concache_perm_sup(:recently_used_cache),
concache_perm_sup(:auth_group_store),
concache_perm_sup(:restriction_lookup_store),
concache_perm_sup(:config_user_type_store),
concache_perm_sup(:config_site_type_store),
concache_perm_sup(:config_site_cache),
concache_perm_sup(:application_metadata_cache),
Teiserver.Account.PermissionCache,
Teiserver.Config.UserConfigTypes.Cache,
Teiserver.Config.SiteConfigTypes.Cache,
Teiserver.MetadataCache,
concache_sup(:application_temp_cache),
concache_sup(:config_user_cache),

# Tachyon schemas
concache_perm_sup(:tachyon_schemas),
concache_perm_sup(:tachyon_dispatches),
Teiserver.Tachyon.Cache,

# Teiserver stuff
# Global/singleton registries
Expand All @@ -79,8 +77,7 @@ defmodule Teiserver.Application do

# Stores - Tables where changes are not propagated across the cluster
# Possible stores
concache_perm_sup(:teiserver_queues),
concache_perm_sup(:lobby_policies_cache),
Teiserver.Data.LobbyPolicyCache,

# Telemetry
concache_perm_sup(:telemetry_property_types_cache),
Expand All @@ -98,7 +95,7 @@ defmodule Teiserver.Application do

# Caches
# Caches - Meta
concache_perm_sup(:lists),
Teiserver.Data.MatchmakingCache,

# Caches - User
# concache_sup(:users_lookup_name_with_id, [global_ttl: 300_000]),
Expand Down Expand Up @@ -135,7 +132,7 @@ defmodule Teiserver.Application do
concache_perm_sup(:discord_command_cache),

# Lobbies
concache_perm_sup(:lobby_command_cache),
Teiserver.Lobby.Cache,
{DynamicSupervisor, strategy: :one_for_one, name: Teiserver.LobbySupervisor},
{DynamicSupervisor, strategy: :one_for_one, name: Teiserver.ClientSupervisor},
{DynamicSupervisor, strategy: :one_for_one, name: Teiserver.PartySupervisor},
Expand All @@ -162,10 +159,6 @@ defmodule Teiserver.Application do
# Telemetry
{Teiserver.Telemetry.TelemetryServer, name: Teiserver.Telemetry.TelemetryServer},

# Text callbacks
concache_perm_sup(:text_callback_trigger_lookup),
concache_perm_sup(:text_callback_store),

# Ranch servers
%{
id: Teiserver.SSLSpringTcpServer,
Expand Down Expand Up @@ -206,34 +199,6 @@ defmodule Teiserver.Application do
end
end

defp concache_sup(name, opts \\ []) do
Supervisor.child_spec(
{
ConCache,
[
name: name,
ttl_check_interval: 10_000,
global_ttl: opts[:global_ttl] || 60_000,
touch_on_read: true
]
},
id: {ConCache, name}
)
end

defp concache_perm_sup(name) do
Supervisor.child_spec(
{
ConCache,
[
name: name,
ttl_check_interval: false
]
},
id: {ConCache, name}
)
end

def startup_sub_functions({:error, _}), do: :error

def startup_sub_functions(_) do
Expand Down
25 changes: 25 additions & 0 deletions lib/teiserver/communication/libs/cache.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule Teiserver.Communication.Cache do
@moduledoc """
Cache and setup for communication stuff
"""

use Supervisor
alias Teiserver.Helpers.CacheHelper

def start_link(opts) do
with {:ok, sup} <- Supervisor.start_link(__MODULE__, :ok, opts),
:ok <- Teiserver.Communication.build_text_callback_cache() do
{:ok, sup}
end
end

@impl true
def init(:ok) do
children = [
CacheHelper.concache_perm_sup(:text_callback_trigger_lookup),
CacheHelper.concache_perm_sup(:text_callback_store)
]

Supervisor.init(children, strategy: :one_for_all)
end
end
26 changes: 26 additions & 0 deletions lib/teiserver/config/site_config_types/cache.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule Teiserver.Config.SiteConfigTypes.Cache do
@moduledoc """
Cache and setup for site configuration
"""

use Supervisor
alias Teiserver.Helpers.CacheHelper

def start_link(opts) do
with {:ok, sup} <- Supervisor.start_link(__MODULE__, :ok, opts),
:ok <- Teiserver.Config.SiteConfigTypes.SystemConfigs.create(),
:ok <- Teiserver.TeiserverConfigs.teiserver_configs() do
{:ok, sup}
end
end

@impl true
def init(:ok) do
children = [
CacheHelper.concache_perm_sup(:config_site_type_store),
CacheHelper.concache_perm_sup(:config_site_cache)
]

Supervisor.init(children, strategy: :one_for_all)
end
end
25 changes: 25 additions & 0 deletions lib/teiserver/config/user_config_types/cache.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule Teiserver.Config.UserConfigTypes.Cache do
@moduledoc """
Cache and setup for profile configuration
"""

use Supervisor
alias Teiserver.Helpers.CacheHelper

def start_link(opts) do
with {:ok, sup} <- Supervisor.start_link(__MODULE__, :ok, opts),
:ok <- Teiserver.Config.UserConfigTypes.ProfileConfigs.create(),
:ok <- Teiserver.Config.UserConfigTypes.PrivacyConfigs.create() do
{:ok, sup}
end
end

@impl true
def init(:ok) do
children = [
CacheHelper.concache_perm_sup(:config_user_type_store)
]

Supervisor.init(children, strategy: :one_for_one)
end
end
23 changes: 22 additions & 1 deletion lib/teiserver/config/user_config_types/profile_configs.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
defmodule Teiserver.Config.UserConfigTypes.ProfileConfigs do
@moduledoc false
@moduledoc """
Cache and setup for profile configuration
"""

use Supervisor

import Teiserver.Config, only: [add_user_config_type: 1]

def start_link(opts) do
with {:ok, sup} <- Supervisor.start_link(__MODULE__, :ok, opts),
:ok <- create() do
{:ok, sup}
end
end

@impl true
def init(:ok) do
children = [
{ConCache, [name: :config_user_type_store, ttl_check_interval: false]}
]

Supervisor.init(children, strategy: :one_for_one)
end

@spec create() :: :ok
def create() do
add_user_config_type(%{
Expand Down
28 changes: 28 additions & 0 deletions lib/teiserver/data/matchmaking_cache.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule Teiserver.Data.MatchmakingCache do
@moduledoc """
Define cache for matchmaking and set it up
"""

use Supervisor
alias Teiserver.Helpers.CacheHelper

def start_link(opts) do
with {:ok, sup} <- Supervisor.start_link(__MODULE__, :ok, opts) do
Teiserver.cache_put(:lists, :rooms, [])
Teiserver.cache_put(:lists, :lobby_policies, [])
Teiserver.Data.Matchmaking.pre_cache_queues()

{:ok, sup}
end
end

@impl true
def init(:ok) do
children = [
CacheHelper.concache_perm_sup(:teiserver_queues),
CacheHelper.concache_perm_sup(:lists)
]

Supervisor.init(children, strategy: :one_for_all)
end
end
24 changes: 24 additions & 0 deletions lib/teiserver/game/libs/lobby_policy_cache.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Teiserver.Data.LobbyPolicyCache do
@moduledoc """
Define cache for lobby policies and set it up
"""

use Supervisor
alias Teiserver.Helpers.CacheHelper

def start_link(opts) do
with {:ok, sup} <- Supervisor.start_link(__MODULE__, :ok, opts) do
Teiserver.Game.pre_cache_policies()
{:ok, sup}
end
end

@impl true
def init(:ok) do
children = [
CacheHelper.concache_perm_sup(:lobby_policies_cache)
]

Supervisor.init(children, strategy: :one_for_one)
end
end
29 changes: 29 additions & 0 deletions lib/teiserver/helpers/cache_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,33 @@ defmodule Teiserver.Helpers.CacheHelper do
def store_get_or_store(table, key, func) do
ConCache.get_or_store(table, key, func)
end

# Setup and supervisors
def concache_sup(name, opts \\ []) do
Supervisor.child_spec(
{
ConCache,
[
name: name,
ttl_check_interval: 10_000,
global_ttl: opts[:global_ttl] || 60_000,
touch_on_read: true
]
},
id: {ConCache, name}
)
end

def concache_perm_sup(name) do
Supervisor.child_spec(
{
ConCache,
[
name: name,
ttl_check_interval: false
]
},
id: {ConCache, name}
)
end
end
Loading

0 comments on commit c6daa91

Please sign in to comment.