-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from hv0905/vector_db_retry
Add a retry mechanism to vector_db service
- Loading branch information
Showing
9 changed files
with
113 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import asyncio | ||
import functools | ||
from typing import Callable | ||
|
||
from loguru import logger | ||
|
||
|
||
def retry_async(exceptions=Exception, tries=3, delay=0) -> Callable[[Callable], Callable]: | ||
def deco_retry(f): | ||
@functools.wraps(f) | ||
async def f_retry(*args, **kwargs): | ||
m_tries, m_delay = tries, delay | ||
while m_tries > 1: | ||
try: | ||
return await f(*args, **kwargs) | ||
except exceptions as e: | ||
logger.warning(f"{e}, Retrying in {m_delay} seconds...") | ||
if m_delay > 0: | ||
await asyncio.sleep(m_delay) | ||
m_tries -= 1 | ||
return await f(*args, **kwargs) | ||
|
||
return f_retry | ||
|
||
return deco_retry | ||
|
||
|
||
def wrap_object(obj: object, deco: Callable[[Callable], Callable]): | ||
for attr in dir(obj): | ||
if not attr.startswith('_'): | ||
attr_val = getattr(obj, attr) | ||
if callable(attr_val) and asyncio.iscoroutinefunction(attr_val): | ||
setattr(obj, attr, deco(getattr(obj, attr))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Requirements for development and testing | ||
|
||
pytest | ||
pytest-asyncio | ||
pylint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,6 @@ | ||
from qdrant_client import qdrant_client, models | ||
from app.Services.vector_db_context import VectorDbContext | ||
|
||
|
||
def create_coll(host, port, name): | ||
client = qdrant_client.QdrantClient(host=host, port=port) | ||
# create or update | ||
print("Creating collection") | ||
vectors_config = { | ||
"image_vector": models.VectorParams(size=768, distance=models.Distance.COSINE), | ||
"text_contain_vector": models.VectorParams(size=768, distance=models.Distance.COSINE) | ||
} | ||
client.create_collection(collection_name=name, | ||
vectors_config=vectors_config) | ||
print("Collection created") | ||
async def main(): | ||
context = VectorDbContext() | ||
await context.initialize_collection() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import asyncio | ||
|
||
import pytest | ||
|
||
from app.util.retry_deco_async import retry_async, wrap_object | ||
|
||
|
||
class TestRetryDeco: | ||
class ExampleClass: | ||
def __init__(self): | ||
self.counter = 0 | ||
self.counter2 = 0 | ||
|
||
async def example_method(self): | ||
await asyncio.sleep(0) | ||
self.counter += 1 | ||
if self.counter < 3: | ||
raise ValueError("Counter is less than 3") | ||
return self.counter | ||
|
||
async def example_method_must_raise(self): | ||
await asyncio.sleep(0) | ||
self.counter2 += 1 | ||
raise NotImplementedError("This method must raise an exception.") | ||
|
||
@pytest.mark.asyncio | ||
async def test_decorator(self): | ||
obj = self.ExampleClass() | ||
|
||
@retry_async(tries=3) | ||
def caller(): | ||
return obj.example_method() | ||
|
||
assert await caller() == 3 | ||
|
||
@pytest.mark.asyncio | ||
async def test_object_wrapper(self): | ||
obj = self.ExampleClass() | ||
wrap_object(obj, retry_async(ValueError, tries=2)) | ||
with pytest.raises(ValueError): | ||
await obj.example_method() | ||
assert await obj.example_method() == 3 | ||
with pytest.raises(NotImplementedError): | ||
await obj.example_method_must_raise() | ||
assert obj.counter2 == 1 |