saverio-kantox

saverio-kantox

Plug_amqp: a plug adapter for amqp

https://hexdocs.pm/plug_amqp/Plug.AMQP.html

We have a complex RPC system, which uses rabbitmq as transport. So we have implemented the pattern described in https://www.rabbitmq.com/tutorials/tutorial-six-elixir.html

We have also found that having to manage the publishing of the response in each service was a bit too much to keep track of, so we have chosen to use plug as the framework to transform requests to responses. Then the adapter would handle consuming requests, publishing responses and keeping track of all the fizzbuzz.

So now we can expose an RPC with standard plug modules (router, parsers, other middleware) and standard plug.conn API (resp(conn, 200, "ok")).

The Conn struct

It is pretty much a standard plug conn, but some parts of it are kind of specific to amqp:

  • path_info is set to the routing key split on dots.
  • request_path is set to the routing key with slashes instead of dots
  • method is POST by default. It can be overridden using the x-method-override header. The common Plug.MethodOverride plug cannot be used because it needs to parse body first, and it won’t override to GET.
  • body is always available
  • query_params are always empty
  • chunks and streaming responses are not available

The request handler

The handler is run as a task, monitored by the main process. It is responsible of creating the Conn struct and doing telemetry.

The connection

The main process (that receives basic_deliver messages from rabbit) handles reconnections and outages, and to each message starts a task.

It stores a map task_ref => metadata so when a task asks for the response to be published back, it can retrieve the reply_to metadata item, and use it as routing key for the response.

Backpressure is handled by standard rabbit QoS, you can define the maximum number of unhandled messages per conection or per queue, and rabbit will enforce it for you.

Where Next?

Popular in Libraries Top

Qqwy
TypeCheck: Fast and flexible runtime type-checking for your Elixir projects. Core ideas Type- and function specifications are const...
336 13801 100
New
kip
Image is an image processing library for Elixir. It is based upon the fabulous vix library that provides a libvips wrapper for Elixir. I...
574 16576 179
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
jakub-zawislak
Hi everyone, I’m coming from the Symfony (PHP) framework. I like Phoenix, but it has a one thing that was build much better in the Symfo...
New
tmbb
I’ve published the first version of my Makeup library. It’s a syntax highlighter for Elixir in the spirit of Pygments, Currently it highl...
New
Qqwy
Hello everyone, I wrote a small library today called MapDiff. It returns a map listing the (smallest amount of) changes to get from map...
New
mbuhot
EctoJob A transactional job queue built with Ecto, PostgreSQL and GenStage Available on Hex.pm: ecto_job | Hex Docs: API Reference — ec...
New
mattludwigs
Grizzly is a library for working with Z-Wave devices. Z-Wave is a low-frequency radio protocol for controlling smart home devices on a me...
New
wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
mplatts
With HEEX released we decided to start a components library using Tailwind CSS - check it out here: Petal Components. We also have a boi...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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

Sub Categories:

We're in Beta

About us Mission Statement