Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Soberia committed Aug 10, 2022
0 parents commit e061bde
Show file tree
Hide file tree
Showing 7 changed files with 492 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Publish the Package to PyPI
on:
workflow_dispatch:
release:
types: [created]

jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Building the Package
run: |
python -m pip install build
python -m build
- name: Publishing the Package to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.venv
/dist
/*.egg-info
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.formatting.provider": "black"
}
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2022 Soberia

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
121 changes: 121 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# 💡 **About**

This is a dynamic connection pool and size of it grows as it requires. Extra connections will be terminated automatically if they're no longer needed.

The connection pool won't check the connectivity state of the connections before passing them to the user because in any time is still possible for the connection to drop in middle of the query. The user itself should watch for the disconnections.

The connection pool is thread-safe and can be shared on multithreaded context as long as the indivisual connection object not shared between the threads. However individual pool instances are required for different processes.

# 🔌 **Installation**

```bash
pip install mysqlclient-pool
```

# 📋 **How to Use**

Instantiating the connection pool. The pool also can be instantiated as a context manager using `with` statement.

```python
from mysqlclient_pool import ConnectionPool


try:
pool = ConnectionPool(
{
"unix_socket": "/var/run/mysqld/mysqld.sock",
"host": "localhost",
"port": 3306,
"user": "root",
"password": "...",
"database": "mysql"
},
size=20,
timeout=10
)
except TimeoutError:
# Couldn't connect to the database server.
# MySQL server service can be restarted in here if it's down.
pass
```

Acquiring a `cursor` object from the pool. `fetch()` method commits or rollbacks the changes by default.

```python

try:
with pool.fetch() as cursor:
cursor.execute("SELECT DATABASE()")
print(cursor.fetchone())
except (OperationalError, ProgrammingError):
# Handling MySQL errors
pass
except pool.OverflowError:
# The pool can't provide a connection anymore
# because maximum permitted number of simultaneous
# connections is exceeded.
# `max_connections` variable of MySQL server configuration
# can be tweaked to change the behaviour.
pass
except pool.DrainedError:
# The pool can't provide a connection anymore
# because it can't access the database server.
pass
```

`connection` object also can be accessed if needed. But any changes to connection should be reverted when returning the connection back to the pool.

```python
with pool.fetch() as cursor:
try:
cursor.connection.autocommit(True)
cursor.execute("INSERT INTO ...")
cursor.execute("UPDATE ...")
cursor.execute("DELETE FROM ...")
finally:
cursor.connection.autocommit(False)
```

# 🔧 **API**

- _class_ **`mysqlclient_pool.ConnectionPool`**

- _method_ **`__init__(config: dict, size: int = 10, timeout: int = 5) -> None`**

- _parameter_ **`config`**:
The keyword paramaters for creating the connection object.

- _parameter_ **`size`**:
The minimum number of the connections in the pool.

- _parameter_ **`timeout`**:
The time in seconds to wait for initiating the connection pool if the database server is unavailable.

- _exception_ **`TimeoutError`**:
When Unable to fill up the connection pool due to inability to connect to the database server.

- _method_ **`close() -> None`**
Closes the connection pool and disconnects all the connections.

- _method_ **`fetch(auto_manage: bool = True, cursor_type: MySQLdb.cursors.Cursor | MySQLdb.cursors.DictCursor = MySQLdb.cursors.Cursor) -> collections.abc.Generator[MySQLdb.cursors.Cursor | MySQLdb.cursors.DictCursor, None, None]`**
Returns a cursor object from a dedicated connection.

This is a context manager which pulls a connection from the pool and generates a cursor object from it and returns it to the user and at the end, if the connection hasn't disconnected in the way, closes the cursor and returns the connection back to the pool.

- _parameter_ **`auto_manage`**:
If `True` provided, if no unhandled exception raised in the enclosed block, commits the current transaction upon completion of the block or rollbacks the transaction on an unhandled exception.

- _parameter_ **`cursor_type`**:
Type of the cursor.

- _exception_ **`RuntimeError`**:
When called after closing the connection pool.

- _exception_ **`ConnectionPool.DrainedError`**:
When there's no connection available in the pool and unable to initiate new connections due to inability to connect to the database server.

- _exception_ **`ConnectionPool.OverflowError`**:
When unable to initiate new connections due to maximum permitted number of simultaneous connections is exceeded.

- _property_ **`capacity: int`**
Amount of idle connections present in the connection pool.
Loading

0 comments on commit e061bde

Please sign in to comment.