Skip to content

Commit fb7f9a3

Browse files
connection_pool: introduce connection pool
Introduce ConnectionPool class to work with cluster of Tarantool instances. ConnectionPool support master discovery and ro/rw-based requests, so it is most useful while working with a single replicaset of instances. ConnectionPool is supported only for Python 3.7 or newer. Authenticated user must be able to call `box.info` on instances. ConnectionPool updates information about each server state (RO/RW) on initial connect and then asynchronously in separate threads. Application retries must be written considering the asynchronous nature of cluster state refresh. User does not need to use any synchronization mechanisms in requests, it's all handled with ConnectionPool methods. ConnectionPool API is the same as a plain Connection API. On each request, a connection is chosen to execute this request. A connection is chosen based on a request mode: * Mode.ANY chooses any instance. * Mode.RW chooses an RW instance. * Mode.RO chooses an RO instance. * Mode.PREFER_RW chooses an RW instance, if possible, RO instance otherwise. * Mode.PREFER_RO chooses an RO instance, if possible, RW instance otherwise. All requests that are guaranteed to write (insert, replace, delete, upsert, update) use RW mode by default. select uses ANY by default. You can set the mode explicitly. call, eval, execute and ping requests require to set the mode explicitly. Example: pool = tarantool.ConnectionPool( addrs=[ {'host': '108.177.16.0', 'port': 3301}, {'host': '108.177.16.0', 'port': 3302}, ], user='test', password='test',) pool.call('some_write_procedure', arg, mode=tarantool.Mode.RW) Closes #196
1 parent f1cf442 commit fb7f9a3

File tree

8 files changed

+1109
-8
lines changed

8 files changed

+1109
-8
lines changed

Diff for: CHANGELOG.md

+38
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,44 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
- Reusable testing workflow for integration with tarantool artifacts
1111
(PR #192).
12+
- Connection pool with master discovery (PR #211, #196).
13+
14+
ConnectionPool is supported only for Python 3.7 or newer.
15+
Authenticated user must be able to call `box.info` on instances.
16+
17+
ConnectionPool updates information about each server state (RO/RW)
18+
on initial connect and then asynchronously in separate threads.
19+
Application retries must be written considering the asynchronous nature
20+
of cluster state refresh. User does not need to use any synchronization
21+
mechanisms in requests, it's all handled with ConnectionPool methods.
22+
23+
ConnectionPool API is the same as a plain Connection API.
24+
On each request, a connection is chosen to execute this request.
25+
A connection is chosen based on a request mode:
26+
* Mode.ANY chooses any instance.
27+
* Mode.RW chooses an RW instance.
28+
* Mode.RO chooses an RO instance.
29+
* Mode.PREFER_RW chooses an RW instance, if possible, RO instance
30+
otherwise.
31+
* Mode.PREFER_RO chooses an RO instance, if possible, RW instance
32+
otherwise.
33+
All requests that are guaranteed to write (insert, replace, delete,
34+
upsert, update) use RW mode by default. select uses ANY by default. You
35+
can set the mode explicitly. call, eval, execute and ping requests
36+
require to set the mode explicitly.
37+
38+
Example:
39+
```python
40+
pool = tarantool.ConnectionPool(
41+
addrs=[
42+
{'host': '108.177.16.0', 'port': 3301},
43+
{'host': '108.177.16.0', 'port': 3302},
44+
],
45+
user='test',
46+
password='test',)
47+
48+
pool.call('some_write_procedure', arg, mode=tarantool.Mode.RW)
49+
```
1250

1351
### Changed
1452
- **Breaking**: drop Python 2 support (PR #207).

Diff for: tarantool/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# -*- coding: utf-8 -*-
22
# pylint: disable=C0301,W0105,W0401,W0614
33

4+
import sys
5+
46
from tarantool.connection import Connection
57
from tarantool.mesh_connection import MeshConnection
68
from tarantool.const import (
@@ -76,3 +78,8 @@ def connectmesh(addrs=({'host': 'localhost', 'port': 3301},), user=None,
7678
__all__ = ['connect', 'Connection', 'connectmesh', 'MeshConnection', 'Schema',
7779
'Error', 'DatabaseError', 'NetworkError', 'NetworkWarning',
7880
'SchemaError', 'dbapi']
81+
82+
# ConnectionPool is supported only for Python 3.7 or newer.
83+
if sys.version_info.major >= 3 and sys.version_info.minor >= 7:
84+
from tarantool.connection_pool import ConnectionPool, Mode
85+
__all__.extend(['ConnectionPool', 'Mode'])

0 commit comments

Comments
 (0)