Skip to main content

RabbitMQ plugin for Spakky framework

Project description

Spakky RabbitMQ

Spakky Framework를 위한 RabbitMQ 플러그인입니다.

설치

pip install spakky-rabbitmq

또는 Spakky extra로 설치합니다:

pip install spakky[rabbitmq]

IAsyncEventPublisher와 함께 쓰는 RabbitMQ 이벤트 서비스라면 event core까지 포함하는 spakky[events-rabbitmq]를 권장합니다.

설정

SPAKKY_RABBITMQ__ prefix를 가진 환경변수를 설정합니다:

export SPAKKY_RABBITMQ__USE_SSL="false"
export SPAKKY_RABBITMQ__HOST="localhost"
export SPAKKY_RABBITMQ__PORT="5672"
export SPAKKY_RABBITMQ__USER="guest"
export SPAKKY_RABBITMQ__PASSWORD="guest"
export SPAKKY_RABBITMQ__EXCHANGE_NAME="my-exchange"  # Optional
export SPAKKY_RABBITMQ__AUTH_CHALLENGE_ACTION="ack"
export SPAKKY_RABBITMQ__AUTH_DENY_ACTION="ack"
export SPAKKY_RABBITMQ__AUTH_ERROR_ACTION="nack_requeue"

사용법

이벤트 발행

from spakky.core.common.mutability import immutable
from spakky.domain.models.event import AbstractIntegrationEvent
from spakky.event.event_publisher import IEventPublisher
from spakky.core.pod.annotations.pod import Pod

@immutable
class UserCreatedEvent(AbstractIntegrationEvent):
    user_id: int
    email: str

@Pod()
class UserService:
    def __init__(self, publisher: IEventPublisher) -> None:
        self.publisher = publisher

    def create_user(self, email: str) -> User:
        user = User(email=email)
        self.publisher.publish(UserCreatedEvent(user_id=user.id, email=email))
        return user

이벤트 수신

from spakky.event.stereotype.event_handler import EventHandler, on_event

@EventHandler()
class UserEventHandler:
    def __init__(self, notification_service: NotificationService) -> None:
        self.notification_service = notification_service

    @on_event(UserCreatedEvent)
    async def on_user_created(self, event: UserCreatedEvent) -> None:
        await self.notification_service.send_welcome_email(event.email)

비동기 변형

비동기 애플리케이션에서는 IAsyncEventPublisher를 사용합니다:

from spakky.event.event_publisher import IAsyncEventPublisher

@Pod()
class AsyncUserService:
    def __init__(self, publisher: IAsyncEventPublisher) -> None:
        self.publisher = publisher

    async def create_user(self, email: str) -> User:
        user = User(email=email)
        await self.publisher.publish(UserCreatedEvent(user_id=user.id, email=email))
        return user

분산 트레이싱

spakky-tracing은 필수 의존성으로 자동 설치됩니다. ITracePropagator가 컨테이너에 등록되어 있으면 이벤트 발행/소비 시 TraceContext가 자동으로 전파됩니다.

  • 발행 측: IEventTransport.send() 시 현재 TraceContext를 메시지 헤더에 주입합니다
  • 소비 측: 수신 메시지에서 TraceContext를 추출하여 자식 스팬을 생성합니다
  • 헤더가 없으면 새로운 루트 트레이스를 시작합니다

인증/인가 snapshot 수신

spakky-auth의 보호 decorator가 붙은 RabbitMQ event handler는 메시지 header의 signed AuthContextSnapshot을 검증한 뒤 ApplicationContextAuthContext를 seed합니다. seed 시점은 handler wrapper가 integration context를 clear_context()로 정리한 직후이며, 사용자 handler 호출 전입니다.

  • 지원 header: spakky.auth.context_snapshot, x-spakky-auth-context-snapshot
  • missing, invalid, expired snapshot: CHALLENGE decision으로 fail-closed
  • protected handler의 DENY: configured ack/nack policy로 fail-closed
  • verifier provider unavailable: ERROR decision이며 기본 retryable policy(nack_requeue) 적용
  • event payload와 기존 trace header 의미는 변경하지 않습니다

인증 실패 시 message 처리 정책은 다음 환경변수로 조정합니다.

필드 환경변수 기본값 설명
auth_challenge_action SPAKKY_RABBITMQ__AUTH_CHALLENGE_ACTION ack missing/invalid/expired snapshot 처리
auth_deny_action SPAKKY_RABBITMQ__AUTH_DENY_ACTION ack protected handler DENY 처리
auth_error_action SPAKKY_RABBITMQ__AUTH_ERROR_ACTION nack_requeue verifier/provider ERROR 처리

가능한 값은 ack, nack_requeue, nack_drop입니다. 기본값은 CHALLENGE/DENY를 ack하여 poison-loop를 피하고, ERROR만 requeue하여 일시적 provider 장애를 재시도합니다.

주요 기능

  • 자동 queue 선언: 이벤트 타입 이름을 기준으로 durable queue 생성
  • 동기/비동기 지원: 동기 및 비동기 publisher/consumer 모두 지원
  • Background service 패턴: consumer polling을 background service로 실행
  • Pydantic 직렬화: 이벤트를 Pydantic으로 직렬화/역직렬화
  • Exchange 라우팅: pub/sub 메시지 패턴을 위한 선택적 exchange
  • SSL 지원: AMQPS protocol 기반 보안 연결
  • 분산 트레이싱: 서비스 간 trace 전파를 위한 spakky-tracing 통합
  • 인증/인가 보호 경계: signed AuthContextSnapshot 검증 및 protected handler fail-closed 처리

구성 요소

컴포넌트 설명
RabbitMQEventTransport 동기 event transport(IEventTransport)
AsyncRabbitMQEventTransport 비동기 event transport(IAsyncEventTransport)
RabbitMQEventConsumer 동기 event consumer(background service)
AsyncRabbitMQEventConsumer 비동기 event consumer(background service)
RabbitMQConnectionConfig 환경변수 기반 설정
RabbitMQAuthBoundary signed AuthContextSnapshot 검증 및 AuthContext seed helper

에러 처리

  • InvalidMessageError: 필수 metadata(consumer_tag 또는 delivery_tag)가 없는 message에서 발생
  • 보호된 handler 인증/인가 실패는 handler를 호출하지 않고 ack/nack 정책으로 fail-closed 처리됩니다

라이선스

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

spakky_rabbitmq-6.6.1.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spakky_rabbitmq-6.6.1-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file spakky_rabbitmq-6.6.1.tar.gz.

File metadata

  • Download URL: spakky_rabbitmq-6.6.1.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for spakky_rabbitmq-6.6.1.tar.gz
Algorithm Hash digest
SHA256 b78a5a6d5c5f7da98cedc7bae0a7bd6ff0b59a4c7362e0ccceeec1481433663b
MD5 4754ed964ede3b15205c5c0fddf670d9
BLAKE2b-256 1149a0d1f43ff721bb5ee27060968193001604a5ed93c73315ae83e8f6db809e

See more details on using hashes here.

Provenance

The following attestation bundles were made for spakky_rabbitmq-6.6.1.tar.gz:

Publisher: release.yml on E5presso/spakky-framework

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spakky_rabbitmq-6.6.1-py3-none-any.whl.

File metadata

  • Download URL: spakky_rabbitmq-6.6.1-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for spakky_rabbitmq-6.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 652907ab0732fd6e21cb48a49a40da1e83187bcaddf923dedb54c5529f9e8387
MD5 2bb99b8792b127edd5dd79c345742fe5
BLAKE2b-256 4034c1f3b8a436aa49074ea618c2ec4184633a69c3981e2e4383951e3fd2f79a

See more details on using hashes here.

Provenance

The following attestation bundles were made for spakky_rabbitmq-6.6.1-py3-none-any.whl:

Publisher: release.yml on E5presso/spakky-framework

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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