-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optionally use genstage for consuming publish_mutation broadcast events
- Loading branch information
Showing
7 changed files
with
107 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule Absinthe.Subscription.LocalConsumer do | ||
@moduledoc """ | ||
Processes the publish_mutation request on the | ||
local node. | ||
""" | ||
|
||
def start_link({pubsub, mutation_result, subscribed_fields}) do | ||
Task.start_link(fn -> | ||
Absinthe.Subscription.Local.publish_mutation(pubsub, mutation_result, subscribed_fields) | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule Absinthe.Subscription.LocalConsumerSupervisor do | ||
@moduledoc """ | ||
Supervisor for consuming publish_mutation requests | ||
""" | ||
|
||
use ConsumerSupervisor | ||
|
||
alias Absinthe.Subscription.LocalProducer | ||
alias Absinthe.Subscription.LocalConsumer | ||
|
||
def start_link(args) do | ||
ConsumerSupervisor.start_link(__MODULE__, args) | ||
end | ||
|
||
def init([min_demand, max_demand]) do | ||
children = [%{id: LocalConsumer, start: {LocalConsumer, :start_link, []}, restart: :transient}] | ||
opts = [strategy: :one_for_one, subscribe_to: [{LocalProducer, min_demand: min_demand, max_demand: max_demand}]] | ||
ConsumerSupervisor.init(children, opts) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
defmodule Absinthe.Subscription.LocalProducer do | ||
@moduledoc """ | ||
GenStage producer that listens for `publish_mutation` broadcasts | ||
in order to process the subscriptions on this node. | ||
""" | ||
use GenStage | ||
|
||
def start_link(args) do | ||
GenStage.start_link(__MODULE__, args) | ||
end | ||
|
||
def topic(shard), do: "__absinthe__:proxy:#{shard}" | ||
|
||
def init([pubsub, shards, buffer_size]) do | ||
Enum.each(shards, fn shard -> | ||
:ok = pubsub.subscribe(topic(shard)) | ||
end) | ||
|
||
# default buffer_size | ||
{:producer, %{pubsub: pubsub, node: pubsub.node_name()}, buffer_size: buffer_size} | ||
end | ||
|
||
@doc """ | ||
Callback for the consumer to ask for more | ||
subscriptions to process. Since we will be sending | ||
them immediately when we get a message from pubsub, | ||
this just sends an empty list | ||
""" | ||
def handle_demand(_demand, state) do | ||
{:noreply, [], state} | ||
end | ||
|
||
def handle_info(payload, state) do | ||
if payload.node == state.node do | ||
{:noreply, [], state} | ||
else | ||
{:noreply, [{state.pubsub, payload.mutation_result, payload.subscribed_fields}], state} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule Absinthe.Subscription.StageSupervisor do | ||
@moduledoc false | ||
|
||
use Supervisor | ||
|
||
def start_link([pubsub, registry, pool_size]) do | ||
Supervisor.start_link(__MODULE__, {pubsub, registry, pool_size}) | ||
end | ||
|
||
def init({pubsub, _registry, pool_size}) do | ||
min_demand = 1 | ||
max_demand = pool_size | ||
shards = Enum.to_list(0..(pool_size - 1)) | ||
buffer_size = 10_000 | ||
|
||
children = [ | ||
{Absinthe.Subscription.LocalProducer, [pubsub, shards, buffer_size]}, | ||
{Absinthe.Subscription.LocalConsumerSupervisor, [min_demand, max_demand]} | ||
] | ||
|
||
Supervisor.init(children, strategy: :one_for_one) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters