Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Single process bottleneck #17

Open
thiamsantos opened this issue Jun 29, 2021 · 0 comments
Open

Single process bottleneck #17

thiamsantos opened this issue Jun 29, 2021 · 0 comments

Comments

@thiamsantos
Copy link
Member

thiamsantos commented Jun 29, 2021

Motivation

cc: @adrianolisboa @dmarasquin

Proposed solution

Principles behind the proposal:

Steps:

  1. Provide a way for the user start its own instance of pluggy in its own supervisor tree
{Pluggy, name: MyPluggy, client_id: "your-app-client-id", client_secret: "your-app-client-secret"}

This "Pluggy" process could be a named supervisor that starts the token cache and store the client_id and client_secret.

  1. All operations receive the name of the instance of pluggy.
Pluggy.Webhooks.create(MyPluggy, params)
  1. Start a token cache that uses an ets table an make all reads go directly to the ets table.
# example
defmodule Pluggy.CoolNameForTokenCache do
  use GenServer

  @token_key :token

  def start_link(opts) do
    name = Keyword.fetch!(opts, :name)

    GenServer.start_link(__MODULE__, opts, name: name)
  end

  def init(opts) do
    name = Keyword.fetch!(opts, :name)

    :ets.new(table_name(name), [:set, :named_table, :protected, read_concurrency: true])

    {:ok, %{name: name}}
  end

  def set(name, value) do
    GenServer.call(name, {:set, value})
  end

 # note that the lookup goes directly to the table without calling the genserver
 # removing the single process bottleneck that currently exists with the agent
  def get(name) do
    case :ets.lookup(table_name(name), @token_key) do
      [{_key, value}] -> {:ok, value}
      _ -> {:error, :not_found}
    end
  end

  def handle_call({:set, value}, _from, state) do
    :ets.insert(table_name(state.name), {@token_key, value})

    {:reply, :ok, state}
  end

  defp table_name(name), do: Module.concat(name, "TokeCache.Table")
end
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant