A simple Python library to manage kinds of services connections
Project description
Service Linker Library
Overview
The Service Linker library provides a unified and extensible way to manage connections to various services like databases, message brokers, and search engines. It simplifies connection management by ensuring proper lifecycle handling, thread-safety, and flexibility for different service requirements.
The core functionality includes:
- Registering service adapters for various services.
- Managing both thread-safe and non-thread-safe connections.
- Providing context management for connections.
Key Features
- Extensibility: Easily register new services by creating adapters.
- Thread-Safety: Handle both thread-safe and non-thread-safe connections.
- Connection Lifecycle Management: Automatically close all connections at program exit.
- Context Management: Simplify connection handling using Python’s
withstatement.
Components
core.py
The core.py module provides the main ServiceManager class, which handles connection registration, retrieval, and cleanup.
Key Classes
-
ServiceManager- Manages connections and ensures proper lifecycle handling.
- Supports both global and thread-local connection storage.
- Provides methods to register adapters, retrieve connections, and close connections.
-
ConnectionWrapper- Wraps individual connections to provide additional management capabilities.
- Delegates attribute and method access to the underlying connection object.
adapter.py
The adapter.py module defines the abstract base class ServiceAdapter, which must be implemented for each new service. It ensures consistency and enforces the implementation of methods to establish and close connections.
Key Methods
do_get_connection: Establishes a connection.do_close: Closes a connection.
Adapters
The library includes several pre-built adapters for popular services:
-
Elasticsearch (
adapters/elasticsearch_adapter.py)- Adapter:
ElasticSearchAdapter - Thread-Safe: Yes
- Adapter:
-
Kafka Consumer (
adapters/kafka_consumer_adapter.py)- Adapter:
KafkaConsumerAdapter - Thread-Safe: No
- Adapter:
-
Kafka Producer (
adapters/kafka_producer_adapter.py)- Adapter:
KafkaProducerAdapter - Thread-Safe: Yes
- Adapter:
-
MongoDB (
adapters/mongo_adapter.py)- Adapter:
MongoAdapter - Thread-Safe: Yes
- Adapter:
-
MySQL (Raw) (
adapters/mysql_raw_adapter.py)- Adapter:
MySQLRawAdapter - Thread-Safe: No
- Adapter:
Usage
1. Registering an Adapter
Adapters are registered automatically using the @ServiceManager.register_service_adapter decorator.
2. Retrieving a Connection
from service_linker.core import ServiceManager
connection = ServiceManager.get_connection("elasticsearch", hosts=["http://localhost:9200"])
3. Using Context Management
from service_linker.core import ServiceManager
with ServiceManager.connection_context("mongo", host="localhost", port=27017) as conn:
db = conn.my_database
print(db.list_collection_names())
4. Closing Connections
from service_linker.core import ServiceManager
ServiceManager.close_connection("elasticsearch", hosts=["http://localhost:9200"])
5. Closing All Connections
All connections are automatically closed when the program exits, but you can manually invoke it:
from service_linker.core import ServiceManager
ServiceManager.close_all_connections()
Extending the Library
To add support for a new service, create a new adapter by subclassing ServiceAdapter:
from service_linker.adapter import ServiceAdapter
from service_linker.core import ServiceManager
@ServiceManager.register_service_adapter
class MyServiceAdapter(ServiceAdapter):
thread_safe = True
service_name = "my_service"
def do_get_connection(self, **params):
# Logic to establish a connection
return connection
def do_close(self, connection):
# Logic to close the connection
pass
Installation
Clone the repository and install dependencies:
pip install service_linker
License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file service_linker-0.0.2a0.tar.gz.
File metadata
- Download URL: service_linker-0.0.2a0.tar.gz
- Upload date:
- Size: 9.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c8c5716b11838285436ab5d24cdd5b29fa0f666a408cd7f412f3d475224952f
|
|
| MD5 |
1698d682c63c9d96b1122e4affebacb7
|
|
| BLAKE2b-256 |
664f91286e0b11fdad7dd147699d000d26a766193458f1c7463081ff76a30d92
|
File details
Details for the file service_linker-0.0.2a0-py3-none-any.whl.
File metadata
- Download URL: service_linker-0.0.2a0-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3f884d4daf600d1f0517a77d42432e16bc094c38a3d288e56b116447b0c2bbc1
|
|
| MD5 |
5ab9f9c804cfb8cbe0bfe80d2a12ee5a
|
|
| BLAKE2b-256 |
8dc698b27bbf2f3e5a30e567ce31fa8c276752056e2fc3044cbe77b70c0d3850
|