module tarantool.connection_pool
¶
This module provides API for interaction with Tarantool servers cluster.
- class tarantool.connection_pool.ConnectionPool(addrs, user=None, password=None, socket_timeout=None, reconnect_max_attempts=0, reconnect_delay=0, connect_now=True, encoding='utf-8', call_16=False, connection_timeout=None, strategy_class=<class 'tarantool.connection_pool.RoundRobinStrategy'>, refresh_delay=1)¶
Represents the pool of connections to a cluster of Tarantool servers.
To work with
ConnectionPool
, box.info must be callable for the user on each server.ConnectionPool
is best suited to work with a single replicaset. Its API is the same as a single serverConnection
, but requests supportmode
parameter (atarantool.Mode
value) to choose between read-write and read-only pool instances:>>> resp = conn.select('demo', 'AAAA', mode=tarantool.Mode.PREFER_RO) >>> resp - ['AAAA', 'Alpha']
- Parameters
List of dictionaries describing server addresses:
{ "host': "str" or None, # mandatory "port": int or "str", # mandatory "transport": "str", # optional "ssl_key_file": "str", # optional "ssl_cert_file": "str", # optional "ssl_ca_file": "str", # optional "ssl_ciphers": "str" # optional }
Refer to corresponding
Connection
parameters.user¶ – Refer to
user
. The value is used for each connection in the pool.password¶ – Refer to
password
. The value is used for each connection in the pool.socket_timeout¶ – Refer to
socket_timeout
. The value is used for each connection in the pool.reconnect_max_attempts¶ – Refer to
reconnect_max_attempts
. The value is used for each connection in the pool. Be careful: it is internalConnection
reconnect unrelated to pool reconnect mechanisms.reconnect_delay¶ – Refer to
reconnect_delay
. The value is used for each connection in the pool. Be careful: it is internalConnection
reconnect unrelated to pool reconnect mechanisms.connect_now¶ (
bool
, optional) – IfTrue
, connect to all pool servers on initialization. Otherwise, you have to callconnect()
manually after initialization.encoding¶ – Refer to
encoding
. The value is used for each connection in the pool.call_16¶ – Refer to
call_16
. The value is used for each connection in the pool.connection_timeout¶ (
float
, optional) – Refer toconnection_timeout
. The value is used for each connection in the pool.strategy_class¶ (
StrategyInterface
, optional) – Strategy for choosing a server based on a request mode. Defaults to the round-robin strategy.refresh_delay¶ – Minimal time between pool server box.info.ro status background refreshes, in seconds.
- Raise
ConfigurationError
,Connection
exceptions
- call(func_name, *args, mode=None, on_push=None, on_push_ctx=None)¶
Execute a CALL request on the pool server: call a stored Lua function. Refer to
call()
.
- close()¶
Stop request processing, close each connection in the pool.
- connect()¶
Create a connection to each address specified on initialization and start background process threads for them. There is no need to call this method explicitly until you have set
connect_now=False
on initialization.If some connections have failed to connect successfully or provide box.info status (including the case when all of them have failed), no exceptions are raised. Attempts to reconnect and refresh the info would be processed in the background.
- delete(space_name, key, *, index=0, mode=Mode.RW, on_push=None, on_push_ctx=None)¶
Execute an DELETE request on the pool server: delete a tuple in the space. Refer to
delete()
.
- eval(expr, *args, mode=None, on_push=None, on_push_ctx=None)¶
Execute an EVAL request on the pool server: evaluate a Lua expression. Refer to
eval()
.
- execute(query, params=None, *, mode=None)¶
Execute an SQL request on the pool server. Refer to
execute()
.
- insert(space_name, values, *, mode=Mode.RW, on_push=None, on_push_ctx=None)¶
Execute an INSERT request on the pool server: insert a tuple to the space. Refer to
insert()
.
- is_closed()¶
Returns
False
if at least one connection is not closed and is ready to process requests. Otherwise, returnsTrue
.- Return type
- ping(notime=False, *, mode=None)¶
Execute a PING request on the pool server: send an empty request and receive an empty response from the server. Refer to
ping()
.
- replace(space_name, values, *, mode=Mode.RW, on_push=None, on_push_ctx=None)¶
Execute a REPLACE request on the pool server: replace a tuple in the space. Refer to
replace()
.
- select(space_name, key, *, offset=0, limit=4294967295, index=0, iterator=None, mode=Mode.ANY, on_push=None, on_push_ctx=None)¶
Execute a SELECT request on the pool server: update a tuple from the space. Refer to
select()
.- Parameters
- Return type
- Raise
select()
exceptions
- update(space_name, key, op_list, *, index=0, mode=Mode.RW, on_push=None, on_push_ctx=None)¶
Execute an UPDATE request on the pool server: update a tuple in the space. Refer to
update()
.
- class tarantool.connection_pool.InstanceState(status: Status = Status.UNHEALTHY, ro: Optional[bool] = None)¶
Cluster single server state.
- class tarantool.connection_pool.Mode(value)¶
Request mode.
- ANY = 1¶
Send a request to any server.
- PREFER_RO = 5¶
Send a request to RO server, if possible, RW server otherwise.
- PREFER_RW = 4¶
Send a request to RW server, if possible, RO server otherwise.
- RO = 3¶
Send a request to RO server.
- RW = 2¶
Send a request to RW server.
- class tarantool.connection_pool.PoolTask(method_name: str, args: tuple, kwargs: dict)¶
Store request type and arguments to pass them to some server thread.
- args: tuple¶
Connection
method args.- Type
- kwargs: dict¶
Connection
method kwargs.- Type
- method_name: str¶
Connection
method name.- Type
- class tarantool.connection_pool.PoolUnit(addr: dict, conn: ~tarantool.connection.Connection, input_queue: ~queue.Queue = <factory>, output_queue: ~queue.Queue = <factory>, thread: ~typing.Optional[~threading.Thread] = None, state: ~tarantool.connection_pool.InstanceState = <factory>, request_processing_enabled: bool = False)¶
Class to store a Tarantool server metainfo and to work with it as a part of connection pool.
- conn: Connection¶
- Type
- request_processing_enabled: bool = False¶
Flag used to stop requests processing requests in the background thread on connection close or destruction.
- Type
- state: InstanceState¶
Current server state.
- Type
- tarantool.connection_pool.QueueFactory()¶
Build a queue-based channel.
- class tarantool.connection_pool.RoundRobinStrategy(pool)¶
Simple round-robin pool servers rotation.
- build()¶
Initialize (or re-initialize) internal pools to rotate servers based on box.info.ro state.
- getnext(mode)¶
Get server based on the request mode.
- Parameters
- Return type
- Raise
- class tarantool.connection_pool.Status(value)¶
Cluster single server status.
- HEALTHY = 1¶
Server is healthy: connection is successful, box.info.ro could be extracted, box.info.status is “running”.
- UNHEALTHY = 2¶
Server is unhealthy: either connection is failed, box.info cannot be extracted, box.info.status is not “running”.