broker
This module provides utility functions and constants to manage broker configurations and connection strings for various messaging systems, including RabbitMQ and Redis. It supports multiple connection protocols and configurations, such as SSL, Unix sockets, and password inclusion.
The module defines constants for supported brokers and connection string templates, along with functions
to construct and retrieve connection strings and SSL configurations based on settings defined in the
app.yaml configuration file.
get_connection_string(include_password=True)
Constructs and returns a connection string based on the broker configuration.
This function retrieves the connection string from the CONFIG.broker.url if available.
Otherwise, it determines the connection string based on the broker name specified in the
configuration file (app.yaml). If the broker name is not supported, a ValueError is raised.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_password
|
bool
|
Whether to include the password in the connection string. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
A formatted connection string based on the broker configuration. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the broker name is not supported. |
Source code in merlin/config/broker.py
get_rabbit_connection(include_password, conn='amqps')
Constructs and returns a RabbitMQ connection string based on broker configurations.
This function reads broker configurations (such as server, port, username, password, and vhost)
and formats them into a RabbitMQ connection string. Optionally, the password can be included
in the connection string if include_password is set to True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_password
|
bool
|
Whether to include the password in the connection string. |
required |
conn
|
str
|
The connection protocol to use. Defaults to "amqps". Supported values are "amqp" and "amqps". |
'amqps'
|
Returns:
| Type | Description |
|---|---|
str
|
A formatted RabbitMQ connection string. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the password file path is not provided in the broker configuration, or if the password file does not exist or cannot be read. |
Source code in merlin/config/broker.py
get_redis_connection(include_password, use_ssl=False)
Constructs and returns a Redis connection string, optionally using SSL and including a password.
This function retrieves broker configurations (such as server, port, username, password, and database number)
and formats them into a Redis connection string. The connection can be configured to use SSL (rediss protocol)
and optionally include the password in the connection string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_password
|
bool
|
Whether to include the password in the connection string. |
required |
use_ssl
|
bool
|
Whether to use the |
False
|
Returns:
| Type | Description |
|---|---|
str
|
A formatted Redis connection string. |
Source code in merlin/config/broker.py
get_redissock_connection()
Constructs and returns a Redis connection string using a Unix socket.
This function retrieves broker configurations, such as the database number (db_num) and
the Unix socket file path (path), and formats them into a Redis connection string.
If the database number is not specified in the configuration, it defaults to 0.
Returns:
| Type | Description |
|---|---|
str
|
A formatted Redis connection string using a Unix socket. |
Source code in merlin/config/broker.py
get_ssl_config()
Retrieves the SSL configuration for the broker based on the settings in the app.yaml configuration file.
This function determines whether SSL should be used for the broker connection and, if applicable,
returns the SSL configuration details. If the broker does not require SSL or is unsupported,
the function returns False.
Returns:
| Type | Description |
|---|---|
Union[bool, Dict[str, Union[str, VerifyMode]]]
|
This returns either:
|