Exemplo n.º 1
0
def setup(threads):
    pool = ThreadedConnectionPool(
        minconn=int(threads / 4),
        maxconn=int(threads / 4),
        database="hello_world",
        user="******",
        password="******",
        host="tfb-database",
        port=5432,
    )
    template = load_fortunes_template()
    read_row_sql = (
        'SELECT world."randomnumber", world."id" FROM "world" WHERE id = %(id)s'
    )
    prepared_read_row_sql = (
        'SELECT world."randomnumber", world."id" FROM "world" WHERE id = $1')
    write_row_sql = 'UPDATE "world" SET "randomnumber"=$1 WHERE id=$2'
    additional_row = (0, "Additional fortune added at request time.")
    db = pool.getconn()
    cursor = db.cursor()
    cursor.execute("PREPARE read_stmt (int) AS " + prepared_read_row_sql)
    cursor.execute("PREPARE write_stmt (int, int) AS " + write_row_sql)
    cursor.execute('PREPARE fortune AS SELECT * FROM "Fortune"')
    del cursor
    pool.putconn(db)
    del db
    return (
        pool,
        template,
        read_row_sql,
        prepared_read_row_sql,
        write_row_sql,
        additional_row,
    )
Exemplo n.º 2
0
class SAEService(Service):
    name = "sae"
    api = [SAEAPI]
    # Some activator pools may be unavailable
    # while SAE remains healthy
    require_dcs_health = False

    def __init__(self):
        super(SAEService, self).__init__()
        self.pool_cache = {}
        self.activators = {}
        self.pg_pool = None
        self.pg_pool_ready = threading.Event()

    def load_pools(self):
        self.logger.info("Loading pools")
        for p in Pool.objects.all():
            self.pool_cache[str(p.id)] = p.name

    def on_activate(self):
        self.load_pools()
        self.pg_pool = ThreadedConnectionPool(1, config.sae.db_threads,
                                              **config.pg_connection_args)
        self.pg_pool_ready.set()

    def get_pool_name(self, pool_id):
        """
        Returns pool name by id
        """
        return self.pool_cache.get(str(pool_id))

    @contextlib.contextmanager
    def get_pg_connect(self):
        self.pg_pool_ready.wait()
        connect = self.pg_pool.getconn()
        if not connect.autocommit:
            connect.autocommit = True
        try:
            yield connect
        finally:
            self.pg_pool.putconn(connect)