Skip to content

Commit

Permalink
Merge pull request #84 from minos-framework/0.1.1
Browse files Browse the repository at this point in the history
0.1.1
  • Loading branch information
vladyslav-fenchak authored Feb 11, 2022
2 parents 87f8cf8 + eabb9ec commit 31250e6
Show file tree
Hide file tree
Showing 9 changed files with 319 additions and 320 deletions.
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,9 @@ History
------------------

* New view /endpoints. Return all endpoints stored.

0.1.1 (2022-02-11)
------------------

* Increment redis pool.
* Additional logs on `MinosRedisClient`.
2 changes: 1 addition & 1 deletion minos/api_gateway/discovery/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.1.0"
__version__ = "0.1.1"

from .cli import (
app,
Expand Down
19 changes: 10 additions & 9 deletions minos/api_gateway/discovery/database/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class OrdersMinosApiRouter(MinosApiRouter):
"""

import json
import logging
from typing import (
Any,
)
Expand All @@ -26,6 +27,8 @@ class OrdersMinosApiRouter(MinosApiRouter):
MICROSERVICE_KEY_PREFIX,
)

log = logging.getLogger(__name__)


class MinosRedisClient:
"""Class that connects to Redis and returns the configuration values according to domain name.
Expand All @@ -38,14 +41,16 @@ class MinosRedisClient:

__slots__ = "address", "port", "password", "redis"

def __init__(self, config: MinosConfig):
def __init__(self, config: MinosConfig, pool_size: int = 50):
"""Perform initial configuration and connection to Redis"""

address = config.discovery.database.host
port = config.discovery.database.port
password = config.discovery.database.password

pool = aioredis.ConnectionPool.from_url(f"redis://{address}:{port}", password=password, max_connections=10)
pool = aioredis.ConnectionPool.from_url(
f"redis://{address}:{port}", password=password, max_connections=pool_size
)
self.redis = aioredis.Redis(connection_pool=pool)

async def get_data(self, key: str) -> str:
Expand Down Expand Up @@ -73,13 +78,9 @@ async def get_all(self) -> list:
return data

async def set_data(self, key: str, data: dict):
flag = True
try:
await self.redis.set(key, json.dumps(data))
except Exception: # pragma: no cover
flag = False

return flag
async with self.redis as r:
await r.set(key, json.dumps(data))
await r.save()

async def update_data(self): # pragma: no cover
"""Update specific value"""
Expand Down
15 changes: 15 additions & 0 deletions minos/api_gateway/discovery/domain/microservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
annotations,
)

import logging

from ..exceptions import (
NotFoundException,
)
Expand All @@ -13,6 +15,8 @@
MICROSERVICE_KEY_PREFIX = "microservice"
ENDPOINT_KEY_PREFIX = "endpoint"

log = logging.getLogger(__name__)


class Microservice:
"""Microservice class."""
Expand All @@ -36,6 +40,7 @@ async def find_by_endpoint(cls, concrete_endpoint: ConcreteEndpoint, db_client)
"""
async for key_bytes in db_client.redis.scan_iter(match=f"{ENDPOINT_KEY_PREFIX}:{concrete_endpoint.verb}:*"):
endpoint = GenericEndpoint.load_by_key(key_bytes)
log.info(endpoint)
if endpoint.matches(concrete_endpoint):
return await cls.load_by_endpoint(key_bytes, db_client)

Expand All @@ -50,6 +55,7 @@ async def load_by_endpoint(cls, endpoint_key: bytes, db_client) -> Microservice:
:return: A ``Microservice`` instance.
"""
microservice_key = await db_client.get_data(endpoint_key)
log.info(microservice_key)
return await cls.load(microservice_key, db_client)

@classmethod
Expand All @@ -65,6 +71,7 @@ async def load(cls, microservice_key: bytes, db_client) -> Microservice:
microservice_dict["endpoints"] = [
endpoint_key.split(":", 2)[1:] for endpoint_key in microservice_dict["endpoints"]
]
log.info(microservice_dict)
return cls(**microservice_dict)

@classmethod
Expand Down Expand Up @@ -96,10 +103,18 @@ async def save(self, db_client) -> None:
}

microservice_key = f"{MICROSERVICE_KEY_PREFIX}:{self.name}"
log.info("--------DATA TO SAVE----------")
log.info(microservice_key)
log.info(microservice_value)
await db_client.set_data(microservice_key, microservice_value)
for endpoint_key in microservice_value["endpoints"]:
await db_client.set_data(endpoint_key, microservice_key)

log.info("---SAVED DATA---")
log.info(await db_client.get_data(microservice_key))
log.info("----------------")
log.info("--------END SAVE----------")

@classmethod
async def delete(cls, microservice_name, redis_client):
microservice_label = f"{MICROSERVICE_KEY_PREFIX}:{microservice_name}"
Expand Down
Loading

0 comments on commit 31250e6

Please sign in to comment.