Skip to content

Commit

Permalink
connection_pool: introduce connection pool
Browse files Browse the repository at this point in the history
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
  • Loading branch information
DifferentialOrange committed Apr 20, 2022
1 parent 70b1448 commit 150751a
Show file tree
Hide file tree
Showing 8 changed files with 1,101 additions and 8 deletions.
38 changes: 38 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,44 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Reusable testing workflow for integration with tarantool artifacts
(PR #192).
- Connection pool with master discovery (PR #211, #196).

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:
```python
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)
```

### Changed
- **Breaking**: drop Python 2 support (PR #207).
Expand Down
5 changes: 5 additions & 0 deletions tarantool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,8 @@ def connectmesh(addrs=({'host': 'localhost', 'port': 3301},), user=None,
__all__ = ['connect', 'Connection', 'connectmesh', 'MeshConnection', 'Schema',
'Error', 'DatabaseError', 'NetworkError', 'NetworkWarning',
'SchemaError', 'dbapi']

# ConnectionPool is supported only for Python 3.7 or newer.
if sys.version_info.major >= 3 and sys.version_info.minor >= 7:
from tarantool.connection_pool import ConnectionPool, Mode
__all__.extend(['ConnectionPool', 'Mode'])
Loading

0 comments on commit 150751a

Please sign in to comment.