|
1 | 1 | """Routing manager classes for tracking and inspecting routing records.""" |
2 | 2 |
|
| 3 | +import asyncio |
| 4 | +import logging |
3 | 5 | from typing import Coroutine, Sequence |
4 | 6 |
|
5 | 7 | from ....core.error import BaseError |
|
16 | 18 | from .models.route_updated import RouteUpdated |
17 | 19 |
|
18 | 20 |
|
| 21 | +LOGGER = logging.getLogger(__name__) |
| 22 | + |
| 23 | +RECIP_ROUTE_PAUSE = 0.1 |
| 24 | +RECIP_ROUTE_RETRY = 10 |
| 25 | + |
| 26 | + |
19 | 27 | class RoutingManagerError(BaseError): |
20 | 28 | """Generic routing error.""" |
21 | 29 |
|
@@ -54,21 +62,30 @@ async def get_recipient(self, recip_verkey: str) -> RouteRecord: |
54 | 62 | if not recip_verkey: |
55 | 63 | raise RoutingManagerError("Must pass non-empty recip_verkey") |
56 | 64 |
|
57 | | - try: |
58 | | - async with self._profile.session() as session: |
59 | | - record = await RouteRecord.retrieve_by_recipient_key( |
60 | | - session, recip_verkey |
| 65 | + i = 0 |
| 66 | + record = None |
| 67 | + while not record: |
| 68 | + try: |
| 69 | + LOGGER.info(">>> fetching routing record for verkey: " + recip_verkey) |
| 70 | + async with self._profile.session() as session: |
| 71 | + record = await RouteRecord.retrieve_by_recipient_key( |
| 72 | + session, recip_verkey |
| 73 | + ) |
| 74 | + LOGGER.info(">>> FOUND routing record for verkey: " + recip_verkey) |
| 75 | + return record |
| 76 | + except StorageDuplicateError: |
| 77 | + LOGGER.info(">>> DUPLICATE routing record for verkey: " + recip_verkey) |
| 78 | + raise RouteNotFoundError( |
| 79 | + f"More than one route record found with recipient key: {recip_verkey}" |
61 | 80 | ) |
62 | | - except StorageDuplicateError: |
63 | | - raise RouteNotFoundError( |
64 | | - f"More than one route record found with recipient key: {recip_verkey}" |
65 | | - ) |
66 | | - except StorageNotFoundError: |
67 | | - raise RouteNotFoundError( |
68 | | - f"No route found with recipient key: {recip_verkey}" |
69 | | - ) |
70 | | - |
71 | | - return record |
| 81 | + except StorageNotFoundError: |
| 82 | + LOGGER.info(">>> NOT FOUND routing record for verkey: " + recip_verkey) |
| 83 | + i += 1 |
| 84 | + if i > RECIP_ROUTE_RETRY: |
| 85 | + raise RouteNotFoundError( |
| 86 | + f"No route found with recipient key: {recip_verkey}" |
| 87 | + ) |
| 88 | + await asyncio.sleep(RECIP_ROUTE_PAUSE) |
72 | 89 |
|
73 | 90 | async def get_routes( |
74 | 91 | self, client_connection_id: str = None, tag_filter: dict = None |
@@ -136,13 +153,15 @@ async def create_route_record( |
136 | 153 | ) |
137 | 154 | if not recipient_key: |
138 | 155 | raise RoutingManagerError("Missing recipient_key") |
| 156 | + LOGGER.info(">>> creating routing record for verkey: " + recipient_key) |
139 | 157 | route = RouteRecord( |
140 | 158 | connection_id=client_connection_id, |
141 | 159 | wallet_id=internal_wallet_id, |
142 | 160 | recipient_key=recipient_key, |
143 | 161 | ) |
144 | 162 | async with self._profile.session() as session: |
145 | 163 | await route.save(session, reason="Created new route") |
| 164 | + LOGGER.info(">>> CREATED routing record for verkey: " + recipient_key) |
146 | 165 | return route |
147 | 166 |
|
148 | 167 | async def update_routes( |
|
0 commit comments