Skip to content

Commit 5f7f754

Browse files
committed
populate balance metric more frequently
1 parent d5d12a3 commit 5f7f754

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

apps/hip-3-pusher/src/pusher/user_limit_listener.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import time
23

34
from hyperliquid.utils.types import SpotMeta, Meta
45
from loguru import logger
@@ -22,16 +23,27 @@ def __init__(self, config: Config, metrics: Metrics, address: str):
2223

2324
async def run(self):
2425
logger.info("Starting user limit listener url: {} address: {} interval: {}", self.info.base_url, self.address, self.interval)
26+
most_recent_timestamp = None
27+
most_recent_balance = None
28+
2529
while True:
2630
try:
27-
response = await asyncio.to_thread(self._request)
28-
logger.debug("userRateLimit response: {}", response)
29-
balance = response["nRequestsSurplus"] - response["nRequestsCap"] - response["nRequestsUsed"]
30-
logger.debug("userRateLimit user: {} balance: {}", self.address, balance)
31-
self.metrics.user_request_balance.set(balance, {"dex": self.dex, "user": self.address})
31+
now = time.time()
32+
if not most_recent_timestamp or now - most_recent_timestamp > self.interval:
33+
response = await asyncio.to_thread(self._request)
34+
logger.debug("userRateLimit response: {}", response)
35+
new_balance = response["nRequestsSurplus"] + response["nRequestsCap"] - response["nRequestsUsed"]
36+
logger.debug("userRateLimit user: {} balance: {}", self.address, new_balance)
37+
38+
most_recent_timestamp = now
39+
most_recent_balance = new_balance
40+
41+
self.metrics.user_request_balance.set(most_recent_balance, {"dex": self.dex, "user": self.address})
3242
except Exception as e:
3343
logger.error("userRateLimit query failed: {}", e)
34-
await asyncio.sleep(self.interval)
44+
45+
# want to update every 60s to keep metric populated in Grafana
46+
await asyncio.sleep(60)
3547

3648
def _request(self):
3749
return self.info.user_rate_limit(self.address)

0 commit comments

Comments
 (0)