From 6b14f5b8ce3f401f5d6a29ebe51eedd993bef431 Mon Sep 17 00:00:00 2001 From: Garry Hill Date: Mon, 25 Sep 2023 09:55:08 +0100 Subject: [PATCH] lazily load the internal schema to avoid race conditions --- .../lib/electric/postgres/extension/schema_cache.ex | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/components/electric/lib/electric/postgres/extension/schema_cache.ex b/components/electric/lib/electric/postgres/extension/schema_cache.ex index bcb22111d1..b961f1a9df 100644 --- a/components/electric/lib/electric/postgres/extension/schema_cache.ex +++ b/components/electric/lib/electric/postgres/extension/schema_cache.ex @@ -217,7 +217,7 @@ defmodule Electric.Postgres.Extension.SchemaCache do internal_schema: nil } - {:ok, state, {:continue, :init}} + {:ok, state} end @impl GenServer @@ -267,6 +267,7 @@ defmodule Electric.Postgres.Extension.SchemaCache do end def handle_call(:internal_schema, _from, state) do + state = load_internal_schema(state) {:reply, state.internal_schema, state} end @@ -368,8 +369,12 @@ defmodule Electric.Postgres.Extension.SchemaCache do {:noreply, state} end - def handle_continue(:init, state) do - {:noreply, %{state | internal_schema: SchemaLoader.internal_schema(state.backend)}} + defp load_internal_schema(%{internal_schema: nil} = state) do + %{state | internal_schema: SchemaLoader.internal_schema(state.backend)} + end + + defp load_internal_schema(state) do + state end defp current_schema(%{current: nil} = state) do