tompave

tompave

Plug: forward options to nested child plugs

UPDATE:
I originally forgot to mention that my MainPlug is a Plug.Router. I only realized it while responding to Overmind, below. I’ve now updated the initial message to make it clear.

Hello,

I’m working on a project where I have a main Plug that acts as the public API of the library. This module is a Plug.Router and it includes its own plug pipeline. Users are supposed to use it in their routers and forward traffic to it. Let’s call it MyPackage.MainPlug.

This main plug router then uses a number of child plugs to get some work done. These must be separate plugs, because the idea is that users can still use these building blocks to create their own router, if they need some custom logic.

My problem is that I can’t find a way to forward options to the nested “child” plugs.

For example, this is my Phoenix router, where I forward to my plug and pass some options to it:

defmodule MyPhoenixApp.Web.Router do
  use MyPhoenixApp.Web, :router

  pipeline :api do
    plug :accepts, ["json"]
  end

  scope path: "/stuff" do
    pipe_through :api
    forward "/", MyPackage.MainPlug, my_options: [foo: "foo", bar: "bar"]
  end
end

In the next snippet are my main plug (first) and the child plug (second):

defmodule MyPackage.MainPlug do
  use Plug.Router
  plug MyPackage.ChildPlug

  def init(opts) do
    opts # == [my_options: [foo: "foo", bar: "bar"]]
  end

  def call(conn, opts) do
    # opts == [my_options: [foo: "foo", bar: "bar"]]
    conn = Plug.Conn.assign(conn, :my_options, opts[:my_options])

    # do more stuff with conn
  end
end


defmodule MyPackage.ChildPlug do
  def init(opts) do
    opts # == []
  end

  def call(conn, opts) do
    # I need the extra options here!
    conn
  end
end

My problem is that I want to be able to customize ChildPlug from the outside, but I can’t find a clear way to do it.

As you can see, in MainPlug.call/2 I’m assigning the options to the conn just as an example, but it doesn’t really help me because, by then, ChildPlug has already been run.

I guess I could avoid the plug macro and invoke the child plugs directly in MainPlug.call/2, for example:

defmodule MyPackage.MainPlug do
  use Plug.Router
  alias MyPackage.PrivatePlug

  def init(opts) do
    opts # == [my_options: [foo: "foo", bar: "bar"]]
  end

  def call(conn, opts) do
    conn = PrivatePlug.call(conn, PrivatePlug.init(opts))

    # do more stuff with conn
  end
end

That would work, but I’d lose the nice performance gain of being able to manipulate the opts in the init function ahead of time.

Also, this seems a common enough requirement that hopefully there is already a clean API to do what I need.

Thanks!

Most Liked

tompave

tompave

Ok, I’ve finally had the time to work on this.

I think I’ve managed to implement what you described @OvermindDL1. It seems to work, thanks!

I’ll share it here in case it’s helpful to anyone else. I would also appreciate suggestions on how to improve it.

My plugs:

# A pair of simple plugs

defmodule DynamicPlugs.Foo do
  require Logger

  def init(opts) do
    Logger.debug "Foo.init(opts)        -> #{inspect opts}"
    Keyword.get(opts, :foo)
  end

  def call(conn, opts) do
    Logger.debug "Foo.call(conn, opts)  -> #{inspect opts}"
    conn
  end
end

defmodule DynamicPlugs.Bar do
  require Logger

  def init(opts) do
    Logger.debug "Bar.init(opts)        -> #{inspect opts}"
    Keyword.get(opts, :bar)
  end

  def call(conn, opts) do
    Logger.debug "Bar.call(conn, opts)  -> #{inspect opts}"
    conn
  end
end

# --------------------------------------------------------
# Main "public" plug

defmodule DynamicPlugs.Main do
  require Logger

  defmacrop do_build_dynamic_proxy(args) do
    quote do
      defmodule Proxy do
        use Plug.Builder

        plug DynamicPlugs.Foo, unquote(args)
        plug DynamicPlugs.Bar, unquote(args)
      end
    end
  end

  defp build_proxy(args) do
    {:module, proxy, _, _} = do_build_dynamic_proxy(args)
    proxy
  end

  def init(opts) do
    Logger.debug "Main.init(opts)       -> #{inspect opts}"
    [proxy: build_proxy(opts)]
  end

  def call(conn, opts) do
    Logger.debug "Main.call(conn, opts) -> #{inspect opts}"
    opts[:proxy].call(conn, [])
  end
end

With this setup, I can plug the main module in a Plug router:

plug DynamicPlugs.Main, foo: %{a: 1}, bar: %{b: 2}, baz: "ignored"

And then on the Plug server I get:

Compiling 19 files (.ex)

01:33:06.071 [debug] Main.init(opts)       -> [foo: %{a: 1}, bar: %{b: 2}, baz: "ignored"]

01:33:06.101 [debug] Bar.init(opts)        -> [foo: %{a: 1}, bar: %{b: 2}, baz: "ignored"]

01:33:06.102 [debug] Foo.init(opts)        -> [foo: %{a: 1}, bar: %{b: 2}, baz: "ignored"]
Generated XXXXX app

01:33:08.168 [debug] GET /

01:33:08.194 [debug] Main.call(conn, opts) -> [proxy: DynamicPlugs.Main.Proxy]

01:33:08.194 [debug] Foo.call(conn, opts)  -> %{a: 1}

01:33:08.194 [debug] Bar.call(conn, opts)  -> %{b: 2}

01:33:08.195 [debug] Sent 204 in 26ms

bvobart

bvobart

Thanks a lot for sharing your implementation! I was running into a similar problem while writing a small wrapper for a public plug and I was able to fix it with due to this topic and your solution!

I realize it’s been 7 years since this topic opened, but there is one important detail to add that can save future readers some problems and headaches: init/1 (and therefore the module-generating macro inside it) is evaluated at compile-time, meaning that in order to be able to use your wrapping plug in multiple places in your code, you must also incorporate a unique ID in your generated module’s name, otherwise you’ll just end up redefining the same module, overwriting its previous implementations, meaning only the latest compiled module will remain. That likely causes the wrong set of options to be used in most places where you’re using this plug. For me, it also caused some flakiness in my tests due to compilation race conditions.

Here’s what my solution incorporating such a unique ID looks like:

defmodule WrapperPlug do
  @behaviour Plug

  defmacrop build(opts, id) do
    # note: do not define `id` here, this line will only be executed once during compilation.
    # id = ...

    quote location: :keep do
      defmodule "Impl#{unquote(id)}" |> String.to_atom() do
        use Plug.Builder

        plug ChildPlug, unquote(opts)

        # ...
      end
    end
  end

  @impl Plug
  def init(opts) do
    # init is evaluated at compile-time, so a unique integer ID is used to ensure
    # that multiple instances of this plug can co-exist in one codebase.
    id = :erlang.unique_integer([:positive])
    {:module, mod, _, _} = build(opts, id)
    [mod: mod]
  end

  @impl Plug
  def call(conn, opts) do
    opts[:mod].call(conn, opts)
  end
end

tompave

tompave

Yes, I get that, but so is the output of MainPlug.init/1, and I was hoping that there was some macro to store it in a module attribute and pass it down to the other plugs.
Looking into the code of Plug.Builder, however, I can see that the plug pipeline is “resolved and frozen” in __before_compile__, and I guess that the MainPlug module (or any module plug, really) is compiled before it gets referenced in the forward "/" ... call.

I can, but my child plugs are executed before MainPlug.call/2 is called, so it doesn’t really help me unless I manually invoke the plugs instead of relying on the plug macro.

OvermindDL1

OvermindDL1

Basically plug MyModule generates essentially (not really, but this gets the point across more simply) def do_plug(state), do: MyModule.call(state, unquote(MyModule.init([])). What you could do is generate a new location-sensitive module name dynamically generated, generate a proxy module that sets up the pipeline then pass the name of it back in the options, then call it inside your call to do the actual work.

Hard to give a good example currently, maybe if I get time later to actually write it out, poke me if so?

bvobart

bvobart

I can’t edit my original response anymore, but I’ve found out that :erlang.unique_integer([:positive]) may return the same integer twice between two separate runs of the Erlang VM, which could already happen with a console command as simple as mix compile && mix test.

It’s better to use a :crypto.hash of the module name plus options object:

    id =
      :sha256
      |> :crypto.hash(Macro.to_string(__MODULE__) <> Macro.to_string(opts))
      |> binary_part(0, 4)
      |> :crypto.bytes_to_integer()

Where Next?

Popular in Questions Top

itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement