Skip to content

Commit 5d63f29

Browse files
committed
few fix
1 parent 89968a7 commit 5d63f29

3 files changed

Lines changed: 44 additions & 9 deletions

File tree

subvortex/core/country/country.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
# Per-API rate limits (seconds between calls)
3030
API_RATE_LIMITS = {
3131
"subvortex": 60, # SubVortex API - conservative
32-
"ipinfo": 86.4, # Free tier: 1000 req/day = 86400s/1000 = 86.4s between calls - Highest accuracy (~99%)
33-
"ip_api": 1.5, # Free tier: 45 req/min = 1.33s between calls, use 1.5s for safety (https://ip-api.com/docs/unban) - Very high accuracy (~98%)
32+
"ipinfo": 90, # Free tier: 1000 req/day = 86400s/1000 = 86.4s between calls - Highest accuracy (~99%)
33+
"ip_api": 5, # Free tier: 45 req/min = 1.33s between calls, use 1.5s for safety (https://ip-api.com/docs/unban) - Very high accuracy (~98%)
3434
"country_is": 10, # Free tier: 1 req/10 seconds per IP (https://country.is/) - Good accuracy (~95%)
3535
"my_api": 0, # Custom API - Down!
3636
}
@@ -80,21 +80,28 @@ def get_country(ip: str):
8080

8181
try:
8282
_last_call_time[api_name] = now
83+
8384
country, reason = api_func(ip_ipv4)
8485
if country:
8586
return country
87+
8688
if reason:
8789
errors.append(f"{api_name}: {reason}")
8890

91+
except requests.HTTPError as e:
92+
status_code = getattr(e.response, 'status_code', 'unknown')
93+
errors.append(f"{api_name} ({status_code}): {e}")
94+
8995
except Exception as e:
9096
errors.append(f"{api_name}: {e}")
9197

9298
# Build error message with rate limit info
9399
all_errors = errors + [
94100
f"{api}: {remaining:.0f}s cooldown" for api, remaining in rate_limited.items()
95101
]
102+
96103
raise CountryApiException(
97-
f"All APIs failed for {ip_ipv4}: {'; '.join(all_errors)}", rate_limited
104+
f"All APIs failed for {ip_ipv4} - {'; '.join(all_errors)}", rate_limited
98105
)
99106

100107

subvortex/core/metagraph/database.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,32 @@ async def notify_state(self):
248248
prefix=self.settings.logging_name,
249249
)
250250

251+
async def get_state(self):
252+
"""
253+
Get the block of the last time the metagraph has been updated
254+
"""
255+
# Ensure Redis connection is established before any operation.
256+
await self.ensure_connection()
257+
258+
# Get a connected Redis client, configured with the correct DB index and prefix.
259+
client = await self.get_client()
260+
261+
try:
262+
raw = await client.get(self._key("state:metagraph"))
263+
return raw
264+
265+
except Exception as ex:
266+
btul.logging.error(
267+
f"[get_last_updated] Failed to read last updated block: {ex}",
268+
prefix=self.settings.logging_name,
269+
)
270+
btul.logging.debug(
271+
f"[get_last_updated] Exception type: {type(ex).__name__}, Traceback:\n{traceback.format_exc()}",
272+
prefix=self.settings.logging_name,
273+
)
274+
275+
return 0
276+
251277
def _key(self, key: str):
252278
# Prefixes keys to namespace them under this service
253279
return f"{self.settings.key_prefix}:{key}"

subvortex/core/metagraph/metagraph.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ async def start(self):
4141
"""
4242
registration_count = 0
4343
axons = {}
44-
ready = False
4544
last_synced_block = 0
4645
sync_interval = self.settings.sync_interval
4746
has_missing_country = False
@@ -112,11 +111,13 @@ async def start(self):
112111
# Get the last udpate
113112
last_update = await self.database.get_neuron_last_updated()
114113

114+
# Get the current state of the metagraph
115+
state = await self.database.get_state()
116+
115117
# Determine whether a resync is needed
116118
time_to_resync = block - last_synced_block >= sync_interval
117119
must_resync = (
118-
not ready
119-
or has_new_registration
120+
has_new_registration
120121
or has_axons_changed
121122
or has_missing_country
122123
or time_to_resync
@@ -128,12 +129,12 @@ async def start(self):
128129
"No changes detected; skipping sync.",
129130
prefix=self.settings.logging_name,
130131
)
131-
132+
132133
# Notify listener the metagraph is ready
133-
ready = await self._notify_if_needed(ready)
134+
ready = await self._notify_if_needed(state == "ready")
134135

135136
continue
136-
137+
137138
if has_axons_changed:
138139
reason = "hotkey/IP changes detected"
139140
elif has_missing_country:
@@ -271,6 +272,7 @@ async def _resync(self, last_update: bool) -> dict[str, str]:
271272
f"⏰ Waiting {max_wait:.0f}s for all APIs to recover..."
272273
)
273274
await asyncio.sleep(max_wait)
275+
274276
else:
275277
await asyncio.sleep(60) # Default wait if no rate limit info
276278

0 commit comments

Comments
 (0)