Skip to main content

A lightweight framework for service decoupling and peak shaving in web services, leveraging RabbitMQ. - GoodManWEN/RabiBridge

Project description

RabiBridge

fury licence pyversions Publish Docs

This is a lightweight RPC framework based on RabbitMQ, designed to achieve network service decoupling and traffic peak shaving and protect your backend service. Applicable to FastAPI / aiohttp and other asynchronous frameworks

Catalogue

Feature

  • Based on message queues
  • Low Latency
  • Distributed services and horizontal scaling
  • Fully asynchronous framework
  • Connections ensured by tls
  • Stable under extensive stress testing

Dependency

  • Python 3.x
  • RabbitMQ

Installation

pip install RabiBridge

Quick Start

  1. First start your rabbitmq service
docker run -d \
  --name rabbitmq \
  -e RABBITMQ_DEFAULT_USER=admin \
  -e RABBITMQ_DEFAULT_PASS=123456 \
  -p 5672:5672 \
  -p 15672:15672 \
  rabbitmq:management
  1. Import RabiBridge, create a function to call, register and run serve.
# -*- coding: utf-8 -*-
# File name: service.py
import asyncio
from rabibridge import RMQServer, register_call

@register_call(timeout=10)
async def fibonacci(n: int):
    if n <= 0:
        return 0
    elif n == 1:
        return 1
    return await fibonacci(n-1) + await fibonacci(n-2)

async def main():
    bridge = RMQServer(host="localhost", port=5672, username="admin", password="123456")
    bridge.load_services(globals()) # Automatic capture procedure of the main namespace
    async with bridge:
        await bridge.run_serve()

asyncio.run(main())
  1. Call remotely.
# -*- coding: utf-8 -*-
# File name: client.py
import asyncio
from rabibridge import RMQClient

async def main():
    async with RMQClient(host="localhost", port=5672, username="admin", password="123456") as bridge:
        err_code, result = await bridge.remote_call('fibonacci', (10, ))
        print(f"Result is {result}")
        # >>> Result is 55

asyncio.run(main())

Documentation

For the detailed function description, please refer to the API reference.

Production deployment example

Encryption in configuration file

Configuration

For production and other needs, to achieve higher convenience and stronger security, it is recommended to use configuration files instead of passing parameters. The configuration file options are as follows:

[rabbitmq]      
RABBITMQ_HOST = 'localhost'                    # RabbitMQ configuration info, same below.
RABBITMQ_PORT = 5672
RABBITMQ_USERNAME = "admin"
RABBITMQ_PASSWORD = "fMgmG7+ooAYLjXdPnhInjQ==" # AES Encrypted, check "Encryption in configuration file"

[app]
DEBUG_MODE = false                             # Whether to run in Debug mode.
CID_MAX = 1073741824                           # The maximum value of the independent ID assigned by 
                                               # the client for each message, which should not be 
                                               # too small or too large.
COMPRESS_THRESHOLD = 1024                      # Stream compression algorithm will be enabled when 
                                               # the message size exceeds this byte threshold.
SERIALISER = 'msgpack'                         # Literal['msgpack', 'pickle', 'json']

[secret]
SECRET = "your secret that no one knows"       # Avoid being known by anyone.

Performance

Testing Platform:

CPU Model            : Intel(R) Xeon(R) Platinum 8369B CPU @ 2.70GHz
CPU Cores            : 16 Cores 2699.998 MHz x86_64
CPU Cache            : L2 1280K & L3 49152K
OS                   : Ubuntu 22.04.4 LTS (64 Bit) KVM
Kernel               : 5.15.0-106-generic
Total RAM            : 950 MB / 30663 MB (4646 MB Buff)
Location             : San Jose / US
Region               : California

Licence

The MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rabibridge-0.1.2.tar.gz (17.5 kB view hashes)

Uploaded Source

Built Distribution

RabiBridge-0.1.2-py3-none-any.whl (20.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page