# Transports Deep Dive

Kurier supports multiple "Lines" (transports), each with its own implementation details. This guide drills into the specifics of our primary brokers.

## Table of Contents

- [1. RabbitMQ (`RabbitMQLine`)](#1-rabbitmq-rabbitmqline)
- [2. Apache Kafka (`KafkaLine`)](#2-apache-kafka-kafkaline)
- [3. Azure Service Bus (`AzureLine`)](#3-azure-service-bus-azureline)
- [4. In-Memory Transport (`InMemoryLine`)](#4-in-memory-transport-inmemoryline)
- [Best Practices](#best-practices)

---

## 1. RabbitMQ (`RabbitMQLine`)

Kurier's RabbitMQ implementation uses the **pika** library.

*   **Concurrency Model**: The consumer runs in a dedicated **daemon thread** using `pika.SelectConnection`. This ensures the long-lived AMQP connection doesn't block the main application's event loop.
*   **Acknowledge Strategy**: Messages are automatically acknowledged (`basic_ack`) *after* all handlers have been successfully invoked.
*   **Automatic Reconnect**: If the publisher or consumer connection is lost, Kurier automatically attempts to reconnect before the next operation.

### Configuration
```python
from kurier.configurations.models.rabbitmq import RabbitMQConfig
config = RabbitMQConfig(
    name="orders", 
    host="localhost",
    port=5672,
    username="guest",
    password="guest",
    virtual_host="/",
    exchange_name="order_exchange",
    queue_name="order_queue",
    routing_key="orders.created"
)
```

---

## 2. Apache Kafka (`KafkaLine`)

The Kafka transport uses the **kafka-python-ng** library.

*   **Partitioning**: Kurier currently relies on Kafka producer defaults for partition selection.
*   **Consumption**: Kafka consumers run in a separate thread and dispatch messages back to the `Station` loop.
*   **Batch Publishing**: `publish_batch_async()` is available through `BaseKurier` and publishes messages with a shared correlation ID.

### Configuration
```python
from kurier.configurations.models.kafka import KafkaConfig
config = KafkaConfig(
    name="events",
    bootstrap_servers=["localhost:9092"],
    topic_name="system_events",
    group_id="kurier_consumer_group"
)
```

---

## 3. Azure Service Bus (`AzureLine`)

Built on top of the **azure-servicebus** SDK.

*   **Support**: Supports both **Queues** and **Topics**.
*   **Security**: Uses standard Connection Strings.
*   **Lifecycle**: Gracefully handles closing of senders and receivers to prevent resource leaks.

### Configuration
```python
from kurier.configurations.models.azureservicebus import AzureServiceBusConfig
config = AzureServiceBusConfig(
    name="cloud_bus",
    connection_string="Endpoint=sb://...",
    queue_name="tasks" # Or topic_name="events"
)
```

---

## 4. In-Memory Transport (`InMemoryLine`)

A special transport designed for **local development and testing**.

*   **Mechanism**: Directly invokes `Station.dispatch` within the same process.
*   **Use Case**: Excellent for unit tests or applications where you want a "bus" architecture without the overhead of an external broker.
*   **No Configuration Class**: Can be instantiated directly with a `name`.

---

## Best Practices
1.  **Unique Names**: Ensure every Line in your system has a unique name (e.g., `orders_line`, `notifications_line`).
2.  **Graceful Shutdown**: Always call `await line.close()` during application shutdown to release broker connections.
3.  **Error Handling**: Wrap your `publish_async` calls in `try/except KurierPublishError` to handle broker downtime.
