Skip to content

Commit 0b273c1

Browse files
committed
add consumer suspend to feature flags
1 parent eb69206 commit 0b273c1

5 files changed

Lines changed: 22 additions & 11 deletions

File tree

‎packages/sync-service/lib/electric/config.ex‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ defmodule Electric.Config do
3838

3939
@build_env Mix.env()
4040

41-
@known_feature_flags ~w[allow_subqueries]
41+
@known_feature_flags ~w[allow_subqueries suspend_consumers]
4242

4343
@defaults [
4444
## Database

‎packages/sync-service/lib/electric/shapes/consumer.ex‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,7 @@ defmodule Electric.Shapes.Consumer do
370370
# a. we have no dependent shapes
371371
# b. we don't have a materializer subscribed
372372

373-
can_suspend? =
374-
Electric.StackConfig.lookup(state.stack_id, :shape_enable_suspend?, true) and
375-
state.snapshot_started and Enum.empty?(state.shape.shape_dependencies_handles) and
376-
not state.materializer_subscribed?
377-
378-
if can_suspend? do
373+
if consumer_suspend_enabled?(state) and consumer_can_suspend?(state) do
379374
Logger.debug(fn -> ["Suspending consumer ", to_string(state.shape_handle)] end)
380375
{:stop, ShapeCleaner.consumer_suspend_reason(), state}
381376
else
@@ -385,6 +380,17 @@ defmodule Electric.Shapes.Consumer do
385380
end
386381
end
387382

383+
defp consumer_suspend_enabled?(%{stack_id: stack_id}) do
384+
# TODO: remove additional feature flag check once feature is stabilised
385+
Electric.StackConfig.lookup(stack_id, :shape_enable_suspend?, true) and
386+
"suspend_consumers" in Electric.StackConfig.lookup(stack_id, :feature_flags, [])
387+
end
388+
389+
defp consumer_can_suspend?(state) do
390+
state.snapshot_started and Enum.empty?(state.shape.shape_dependencies_handles) and
391+
not state.materializer_subscribed?
392+
end
393+
388394
@impl GenServer
389395
def terminate(reason, state) do
390396
Logger.debug(fn ->

‎packages/sync-service/lib/electric/stack_config.ex‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ defmodule Electric.StackConfig do
2323
[
2424
snapshot_timeout_to_first_data: :timer.seconds(30),
2525
shape_hibernate_after: Electric.Config.default(:shape_hibernate_after),
26-
chunk_bytes_threshold: Electric.ShapeCache.LogChunker.default_chunk_size_threshold()
26+
shape_enable_suspend?: Electric.Config.default(:shape_enable_suspend?),
27+
chunk_bytes_threshold: Electric.ShapeCache.LogChunker.default_chunk_size_threshold(),
28+
feature_flags: []
2729
]
2830
end
2931

‎packages/sync-service/lib/electric/stack_supervisor.ex‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ defmodule Electric.StackSupervisor do
367367
snapshot_timeout_to_first_data: config.tweaks[:snapshot_timeout_to_first_data],
368368
inspector: inspector,
369369
shape_hibernate_after: shape_hibernate_after,
370-
shape_enable_suspend?: shape_enable_suspend?
370+
shape_enable_suspend?: shape_enable_suspend?,
371+
feature_flags: Map.get(config, :feature_flags, [])
371372
]},
372373
{Electric.AsyncDeleter,
373374
stack_id: stack_id,

‎packages/sync-service/test/support/component_setup.ex‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ defmodule Support.ComponentSetup do
112112
inspector: Map.get(ctx, :inspector, nil),
113113
shape_changes_registry:
114114
Map.get(ctx, :registry, Electric.StackSupervisor.registry_name(stack_id)),
115-
shape_hibernate_after: Map.get(ctx, :shape_hibernate_after, 1_000)
115+
shape_hibernate_after: Map.get(ctx, :shape_hibernate_after, 1_000),
116+
feature_flags: Electric.Config.get_env(:feature_flags)
116117
],
117118
seed_config
118119
)}
@@ -456,7 +457,8 @@ defmodule Support.ComponentSetup do
456457
registry_partitions: 1,
457458
shape_cleaner_opts: shape_cleaner_opts(ctx)
458459
],
459-
manual_table_publishing?: Map.get(ctx, :manual_table_publishing?, false)},
460+
manual_table_publishing?: Map.get(ctx, :manual_table_publishing?, false),
461+
feature_flags: Electric.Config.get_env(:feature_flags)},
460462
restart: :temporary,
461463
significant: false
462464
)

0 commit comments

Comments
 (0)