Skip to content

Allows PHP clients to quickly publish over a unix domain socket and keep connections established by AMQGate.

License

Notifications You must be signed in to change notification settings

pipetky/AmqGate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AmqGate - Unix Socket Gateway for RabbitMQ

Go RabbitMQ License: MIT

AmqGate solves RabbitMQ performance bottlenecks in high-load systems by eliminating connection overhead. This lightweight gateway accepts messages through Unix sockets and forwards them to RabbitMQ using a persistent connection pool.

The Problem: RabbitMQ Connection Overhead

In architectures like Nginx + PHP-FPM, each request typically creates:

  • New TCP connection to RabbitMQ

  • AMQP authentication handshake

  • Channel creation

  • Queue declaration

This results in:

  • High latency (50-100ms per message)

  • RabbitMQ overload from ephemeral connections

  • Blocked PHP processes during connection setup

  • Increased CPU/memory consumption

The Solution

AmqGate acts as a high-performance proxy between your application and RabbitMQ:

[Your Application] → [Unix Socket] → [AmqGate] → [Persistent Connections] → [RabbitMQ]

Key Benefits

  • 10-100x faster message delivery (1-5ms vs 50-100ms)

  • Fixed connection pool to RabbitMQ instead of thousands of short-lived connections

  • Non-blocking asynchronous architecture in Go

  • Automatic reconnection to RabbitMQ

  • Simple integration via Unix sockets

Features

  • 🚀 Configurable AMQP connection pooling

  • 🔌 Automatic RabbitMQ reconnection with exponential backoff

  • 📈 Round-robin channel balancing

  • 📝 Full RabbitMQ queue parameter support (durable, exclusive, etc.)

  • 📊 JSON logging with detailed diagnostics

  • 🔒 Safe message handling with context timeouts

Installation & Usage

Docker

https://hub.docker.com/r/pipetky/amqgate

Running

docker run -it --rm -v /tmp/amqgate/:/tmp/amqgate/ -v $(pwd)/config.yaml:/app/config.yaml pipetky/amqgate:0.0.1

From source

Requirements

Go 1.18+

RabbitMQ server

Install Go

git clone https://github.com/yourusername/amqgate.git
cd amqgate
CGO_ENABLED=0 GOOS=linux go build -o ./amqgate && \
sudo mv amqgate /usr/local/bin/

Running

amqgate -c config.yaml

Configuration

Example config.yaml file:

- qname: "example_q"
  server: "192.168.10.3"
  port: "5672"
  cpq: 4 # Connections per queue
  login: "test"
  pass: "test"
  vhost: "test" # Optional, default ""
  durable: True # Optional, default False
  no_wait: True # Optional, default False
  auto_delete: True # Optional, default False
  exclusive: True # Optional, default False
  exchange: "test" # Optional, default False
- qname: "example_q2"
  server: "192.168.10.3"
  port: "5672"
  cpq: 2 # Connections per queue
  login: "test"
  pass: "test"

Running

./amqgate -c config.yaml

Example php code

<?php

$myfile = fopen("test_50k.txt", "r") or die("Unable to open file!");
$file = fread($myfile,filesize("test_50k.txt"));
fclose($myfile);
$socket_file = "/tmp/amqgate/example_q.sock";

for ($i = 0; $i <= 100000; $i++) {

$socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
socket_connect($socket, $socket_file);
socket_write($socket, $file, $msg_len = strlen($file));
socket_close($socket);
}
?>

Performance

Test results (AMD EPYC 7B12, 64GB RAM, RabbitMQ 3.10):

Metric Without AmqGate With AmqGate
Send latency (p99) 58 ms 1.2 ms
Max throughput 1,200 msg/s 24,000 msg/s
RabbitMQ connections 850 5
PHP CPU usage 75% 12%

Monitoring & Operations

{
  "level": "info",
  "msg": "Connected to rabbitmq.local:5672",
  "time": "2023-10-15T12:34:56Z"
}
{
  "level": "warning",
  "event": "reconnect",
  "attempt": 3,
  "queue": "web_events",
  "time": "2023-10-15T12:35:01Z"
}

Recommended Metrics

  • amqgate_messages_received_total (count)

  • amqgate_connection_errors_total (count)

  • amqgate_reconnect_attempts_total (count)

  • RabbitMQ queue depth

Graceful Shutdown

kill -SIGTERM $(pgrep amqgate)

Why Use AmqGate?

  • Eliminates AMQP connection overhead - Maintains persistent connections to RabbitMQ

  • Reduces application complexity - No need for connection pooling in application code

  • Improves resource utilization - 1 AmqGate instance can serve multiple applications

  • Prevents RabbitMQ overload - Limits connection storms during traffic spikes

  • Simplifies scaling - Add more AmqGate instances as needed

License

This project is licensed under the MIT License - see the LICENSE file for details.

Aleksandr Karabchevskiy - pipetky@gmail.com

Project Link: https://github.com/pipetky/AmqGate

About

Allows PHP clients to quickly publish over a unix domain socket and keep connections established by AMQGate.

Topics

Resources

License

Stars

Watchers

Forks