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

Single process bottleneck #17

@thiamsantos

Description

@thiamsantos

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions