Exemple #1
0
def main_engine(pool_size=DB_POOL_SIZE,
                max_overflow=DB_POOL_MAX_OVERFLOW,
                echo=False):
    database_name = config.get_required('MYSQL_DATABASE')
    engine = create_engine(engine_uri(database_name),
                           listeners=[ForceStrictMode()],
                           isolation_level='READ COMMITTED',
                           echo=echo,
                           pool_size=pool_size,
                           pool_recycle=3600,
                           max_overflow=max_overflow,
                           connect_args={
                               'charset': 'utf8mb4',
                               'waiter': gevent_waiter
                           })

    @event.listens_for(engine, 'checkout')
    def receive_checkout(dbapi_connection, connection_record,
                         connection_proxy):
        '''Log checkedout and overflow when a connection is checked out'''
        hostname = gethostname().replace(".", "-")
        process_name = str(config.get("PROCESS_NAME", "unknown"))

        statsd_client.gauge(
            ".".join([
                "dbconn", database_name, hostname, process_name, "checkedout"
            ]), connection_proxy._pool.checkedout())

        statsd_client.gauge(
            ".".join(
                ["dbconn", database_name, hostname, process_name, "overflow"]),
            connection_proxy._pool.overflow())

    return engine
Exemple #2
0
def main_engine(pool_size=DB_POOL_SIZE, max_overflow=5):
    engine = create_engine(db_uri(),
                           listeners=[ForceStrictMode()],
                           isolation_level='READ COMMITTED',
                           echo=False,
                           pool_size=pool_size,
                           max_overflow=max_overflow,
                           connect_args={'charset': 'utf8mb4'})
    return engine
def engine(database_name,
           database_uri,
           pool_size=DB_POOL_SIZE,
           max_overflow=DB_POOL_MAX_OVERFLOW,
           pool_timeout=DB_POOL_TIMEOUT,
           echo=False):
    engine = create_engine(database_uri,
                           listeners=[ForceStrictMode()],
                           isolation_level='READ COMMITTED',
                           echo=False,
                           pool_size=pool_size,
                           pool_timeout=pool_timeout,
                           pool_recycle=3600,
                           max_overflow=max_overflow,
                           connect_args={
                               'charset': 'utf8mb4',
                               'waiter': gevent_waiter,
                               'connect_timeout': 60
                           })

    @event.listens_for(engine, 'checkout')
    def receive_checkout(dbapi_connection, connection_record,
                         connection_proxy):
        '''Log checkedout and overflow when a connection is checked out'''
        hostname = gethostname().replace(".", "-")
        process_name = str(config.get("PROCESS_NAME", "unknown"))

        statsd_client.gauge(
            ".".join([
                "dbconn", database_name, hostname, process_name, "checkedout"
            ]), connection_proxy._pool.checkedout())

        statsd_client.gauge(
            ".".join(
                ["dbconn", database_name, hostname, process_name, "overflow"]),
            connection_proxy._pool.overflow())

        # Keep track of where and why this connection was checked out.
        log = get_logger()
        context = log._context._dict.copy()
        f, name = find_first_app_frame_and_name(
            ignores=['sqlalchemy', 'inbox.ignition', 'nylas.logging'])
        source = '{}:{}'.format(name, f.f_lineno)

        pool_tracker[dbapi_connection] = {
            'source': source,
            'context': context,
            'checkedout_at': time.time()
        }

    @event.listens_for(engine, 'checkin')
    def receive_checkin(dbapi_connection, connection_record):
        if dbapi_connection in pool_tracker:
            del pool_tracker[dbapi_connection]

    return engine
Exemple #4
0
def engine(
    database_name,
    database_uri,
    pool_size=DB_POOL_SIZE,
    max_overflow=DB_POOL_MAX_OVERFLOW,
    pool_timeout=DB_POOL_TIMEOUT,
    echo=False,
):
    engine = create_engine(
        database_uri,
        listeners=[ForceStrictMode()],
        isolation_level="READ COMMITTED",
        echo=echo,
        pool_size=pool_size,
        pool_timeout=pool_timeout,
        pool_recycle=3600,
        max_overflow=max_overflow,
        connect_args={
            "binary_prefix": True,
            "charset": "utf8mb4",
            "waiter": gevent_waiter,
            "connect_timeout": 60,
        },
    )

    @event.listens_for(engine, "checkout")
    def receive_checkout(dbapi_connection, connection_record,
                         connection_proxy):
        """Log checkedout and overflow when a connection is checked out"""
        hostname = gethostname().replace(".", "-")
        process_name = str(config.get("PROCESS_NAME", "main_process"))

        if config.get("ENABLE_DB_TXN_METRICS", False):
            statsd_client.gauge(
                ".".join([
                    "dbconn", database_name, hostname, process_name,
                    "checkedout"
                ]),
                connection_proxy._pool.checkedout(),
            )

            statsd_client.gauge(
                ".".join([
                    "dbconn", database_name, hostname, process_name, "overflow"
                ]),
                connection_proxy._pool.overflow(),
            )

        # Keep track of where and why this connection was checked out.
        log = get_logger()
        context = log._context._dict.copy()
        f, name = find_first_app_frame_and_name(
            ignores=["sqlalchemy", "inbox.ignition", "nylas.logging"])
        source = "{}:{}".format(name, f.f_lineno)

        pool_tracker[dbapi_connection] = {
            "source": source,
            "context": context,
            "checkedout_at": time.time(),
        }

    @event.listens_for(engine, "checkin")
    def receive_checkin(dbapi_connection, connection_record):
        if dbapi_connection in pool_tracker:
            del pool_tracker[dbapi_connection]

    return engine