Skip to content

configurations

Configuration read from the environment, and the shared clients built from it.

The connection helpers live beside the settings they read rather than in a module of their own: there is one source of truth for where Redis, MongoDB and TimescaleDB are, and one place that opens them.

Functions:

Name Description
get_cache

Redis client shared by the broker API classes and scripts.

get_mongo_db

MongoDB database shared by the broker API classes and scripts.

get_logger

Named logger.

get_postgres

TimescaleDB connection used by the tick persistence layer.

get_postgres_engine

SQLAlchemy engine over the same TimescaleDB the persisters write to.

get_cache

get_cache()

Redis client shared by the broker API classes and scripts.

health_check_interval makes redis-py ping a connection that has been idle for longer than thirty seconds before handing it back, which matters for the long running scripts: one that holds a connection across a quiet spell, overnight say, would otherwise only discover a dropped connection on its next command.

Deliberately no socket_timeout: a blocking stream read (XREADGROUP with block) would then raise whenever it waited longer than the timeout. Stream readers bound their waiting with the read's own block instead.

Source code in utilities/configurations.py
def get_cache():
    """
    Redis client shared by the broker API classes and scripts.

    `health_check_interval` makes redis-py ping a connection that has been idle for longer than
    thirty seconds before handing it back, which matters for the long running scripts: one that
    holds a connection across a quiet spell, overnight say, would otherwise only discover a dropped
    connection on its next command.

    Deliberately no `socket_timeout`: a blocking stream read (`XREADGROUP` with `block`) would then
    raise whenever it waited longer than the timeout. Stream readers bound their waiting with the
    read's own `block` instead.
    """
    return redis.Redis(host=redis_configuration['host'], port=redis_configuration['port'], username=redis_configuration['username'], password=redis_configuration['password'], db=redis_configuration['db'], decode_responses=True, encoding='utf-8', health_check_interval=30, socket_keepalive=True)

get_mongo_db

get_mongo_db()

MongoDB database shared by the broker API classes and scripts.

Source code in utilities/configurations.py
def get_mongo_db():
    """
    MongoDB database shared by the broker API classes and scripts.
    """
    mongo_client = pymongo.MongoClient(mongodb_configuration['connection_string'])
    return mongo_client[mongodb_configuration['db']]

get_logger

get_logger(name)

Named logger.

  • name is the name of the logger, for example zerodha.websocket_quotes.
Source code in utilities/configurations.py
def get_logger(name):
    """
    Named logger.

    - `name` is the name of the logger, for example `zerodha.websocket_quotes`.
    """
    return logging.getLogger(name)

get_postgres

get_postgres()

TimescaleDB connection used by the tick persistence layer.

Source code in utilities/configurations.py
def get_postgres():
    """
    TimescaleDB connection used by the tick persistence layer.
    """
    return psycopg2.connect(host=postgres_configuration['host'], port=postgres_configuration['port'],
                            dbname=postgres_configuration['db'], user=postgres_configuration['username'],
                            password=postgres_configuration['password'])

get_postgres_engine

get_postgres_engine()

SQLAlchemy engine over the same TimescaleDB the persisters write to.

get_postgres returns a psycopg2 connection, which is what the tick, order and position persisters want because they COPY a fixed, known column list. The instrument loader instead writes whatever columns a broker published that day, straight off a pandas DataFrame, and DataFrame.to_sql needs an engine. The DDL runner uses it too.

Source code in utilities/configurations.py
def get_postgres_engine():
    """
    SQLAlchemy engine over the same TimescaleDB the persisters write to.

    `get_postgres` returns a psycopg2 connection, which is what the tick, order and position
    persisters want because they COPY a fixed, known column list. The instrument loader instead
    writes whatever columns a broker published that day, straight off a pandas DataFrame, and
    `DataFrame.to_sql` needs an engine. The DDL runner uses it too.
    """
    return create_engine(postgres_configuration['connection_string'])