Skip to content

Commit

Permalink
use one clientsession and decrease timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
HarukaMa committed Jan 23, 2024
1 parent a2c26c2 commit 800317c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 28 deletions.
12 changes: 6 additions & 6 deletions webui/chain_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ async def block_route(request: Request):
case _:
raise HTTPException(status_code=550, detail="Unsupported transaction type")

sync_info = await out_of_sync_check(db)
sync_info = await out_of_sync_check(request.app.state.session, db)
ctx = {
"request": request,
"block": block,
Expand Down Expand Up @@ -231,7 +231,7 @@ async def transaction_route(request: Request):
block = await db.get_block_from_transaction_id(tx_id)
block_confirm_time = await db.get_block_confirm_time(block.height)

sync_info = await out_of_sync_check(db)
sync_info = await out_of_sync_check(request.app.state.session, db)
ctx: dict[str, Any] = {
"request": request,
"tx_id": tx_id,
Expand Down Expand Up @@ -566,7 +566,7 @@ async def transition_route(request: Request):
"value": f"{future.program_id}/{future.function_name}(...)",
})

sync_info = await out_of_sync_check(db)
sync_info = await out_of_sync_check(request.app.state.session, db)
ctx = {
"request": request,
"ts_id": ts_id,
Expand Down Expand Up @@ -733,7 +733,7 @@ async def blocks_route(request: Request):
start = total_blocks - 50 * (page - 1)
blocks = await db.get_blocks_range_fast(start, start - 50)

sync_info = await out_of_sync_check(db)
sync_info = await out_of_sync_check(request.app.state.session, db)
ctx = {
"request": request,
"blocks": blocks,
Expand Down Expand Up @@ -776,7 +776,7 @@ async def validators_route(request: Request):
})


sync_info = await out_of_sync_check(db)
sync_info = await out_of_sync_check(request.app.state.session, db)
ctx = {
"request": request,
"validators": validators,
Expand Down Expand Up @@ -815,7 +815,7 @@ async def unconfirmed_transactions_route(request: Request):
"first_seen": await db.get_transaction_first_seen(str(tx.id)),
})

sync_info = await out_of_sync_check(db)
sync_info = await out_of_sync_check(request.app.state.session, db)
ctx = {
"request": request,
"transactions": transactions,
Expand Down
6 changes: 3 additions & 3 deletions webui/error_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async def bad_request(request: Request, exc: Exception):
template = "htmx/400.jinja2"
else:
template = "400.jinja2"
sync_info = await out_of_sync_check(db)
sync_info = await out_of_sync_check(request.app.state.session, db)
return templates.TemplateResponse(template, {'request': request, "exc": exc, "sync_info": sync_info}, status_code=400) # type: ignore


Expand All @@ -22,7 +22,7 @@ async def not_found(request: Request, exc: Exception):
template = "htmx/404.jinja2"
else:
template = "404.jinja2"
sync_info = await out_of_sync_check(db)
sync_info = await out_of_sync_check(request.app.state.session, db)
return templates.TemplateResponse(template, {'request': request, "exc": exc, "sync_info": sync_info}, status_code=404) # type: ignore


Expand All @@ -33,7 +33,7 @@ async def internal_error(request: Request, exc: Exception):
template = "htmx/500.jinja2"
else:
template = "500.jinja2"
sync_info = await out_of_sync_check(db)
sync_info = await out_of_sync_check(request.app.state.session, db)
return templates.TemplateResponse(template, {'request': request, "exc": exc, "sync_info": sync_info}, status_code=500) # type: ignore


Expand Down
8 changes: 4 additions & 4 deletions webui/program_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async def programs_route(request: Request):
programs = await db.get_programs(start, start + 50, no_helloworld=no_helloworld)
builtin_programs = await db.get_builtin_programs()

sync_info = await out_of_sync_check(db)
sync_info = await out_of_sync_check(request.app.state.session, db)
ctx = {
"request": request,
"programs": programs + builtin_programs,
Expand Down Expand Up @@ -101,7 +101,7 @@ async def program_route(request: Request):
"key_type": str(mapping.key.plaintext_type),
"value_type": str(mapping.value.plaintext_type)
})
sync_info = await out_of_sync_check(db)
sync_info = await out_of_sync_check(request.app.state.session, db)
ctx: dict[str, Any] = {
"request": request,
"program_id": str(program.id),
Expand Down Expand Up @@ -161,7 +161,7 @@ async def similar_programs_route(request: Request):
start = 50 * (page - 1)
programs = await db.get_programs_with_feature_hash(feature_hash, start, start + 50)

sync_info = await out_of_sync_check(db)
sync_info = await out_of_sync_check(request.app.state.session, db)
ctx = {
"request": request,
"program_id": program_id,
Expand Down Expand Up @@ -206,7 +206,7 @@ async def upload_source_route(request: Request):
else:
import_programs.append(None)
message = request.query_params.get("message")
sync_info = await out_of_sync_check(db)
sync_info = await out_of_sync_check(request.app.state.session, db)
ctx = {
"request": request,
"program_id": program_id,
Expand Down
8 changes: 4 additions & 4 deletions webui/proving_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async def calc_route(request: Request):
else:
template = "calc.jinja2"
proof_target = (await db.get_latest_block()).header.metadata.proof_target
sync_info = await out_of_sync_check(db)
sync_info = await out_of_sync_check(request.app.state.session, db)
ctx = {
"request": request,
"proof_target": proof_target,
Expand Down Expand Up @@ -63,7 +63,7 @@ async def leaderboard_route(request: Request):
total_credit = await db.get_leaderboard_total()
target_credit = 37_500_000_000_000
ratio = total_credit / target_credit * 100
sync_info = await out_of_sync_check(db)
sync_info = await out_of_sync_check(request.app.state.session, db)
ctx = {
"request": request,
"leaderboard": data,
Expand Down Expand Up @@ -238,7 +238,7 @@ async def address_route(request: Request):
"function_name": transition.function_name,
})

sync_info = await out_of_sync_check(db)
sync_info = await out_of_sync_check(request.app.state.session, db)
ctx = {
"request": request,
"address": address,
Expand Down Expand Up @@ -301,7 +301,7 @@ async def address_solution_route(request: Request):
"target": solution["target"],
"target_sum": solution["target_sum"],
})
sync_info = await out_of_sync_check(db)
sync_info = await out_of_sync_check(request.app.state.session, db)
ctx = {
"request": request,
"address": address,
Expand Down
19 changes: 9 additions & 10 deletions webui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,19 @@ def get_relative_time(timestamp: int):
return f"{int(delta)} hours ago"


async def get_remote_height(rpc_root: str) -> str:
async def get_remote_height(session: aiohttp.ClientSession, rpc_root: str) -> str:
try:
async with aiohttp.ClientSession() as session:
async with session.get(f"{rpc_root}/testnet3/latest/height") as resp:
if resp.status == 200:
remote_height = await resp.text()
else:
remote_height = "?"
async with session.get(f"{rpc_root}/testnet3/latest/height") as resp:
if resp.status == 200:
remote_height = await resp.text()
else:
remote_height = "?"
except:
remote_height = "?"
return remote_height


async def out_of_sync_check(db: Database):
async def out_of_sync_check(session: aiohttp.ClientSession, db: Database):
last_timestamp, last_height = await asyncio.gather(
db.get_latest_block_timestamp(),
db.get_latest_height()
Expand All @@ -52,9 +51,9 @@ async def out_of_sync_check(db: Database):
reference_height = None
if out_of_sync:
if rpc_root := os.environ.get("RPC_URL_ROOT"):
node_height = await get_remote_height(rpc_root)
node_height = await get_remote_height(session, rpc_root)
if ref_rpc_root := os.environ.get("REF_RPC_URL_ROOT"):
reference_height = await get_remote_height(ref_rpc_root)
reference_height = await get_remote_height(session, ref_rpc_root)

return {
"out_of_sync": out_of_sync,
Expand Down
3 changes: 2 additions & 1 deletion webui/webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async def index_route(request: Request):
recent_blocks = await db.get_recent_blocks_fast()
network_speed = await db.get_network_speed()
participation_rate = await db.get_network_participation_rate()
sync_info = await out_of_sync_check(db)
sync_info = await out_of_sync_check(request.app.state.session, db)
ctx = {
"latest_block": await db.get_latest_block(),
"request": request,
Expand Down Expand Up @@ -227,6 +227,7 @@ async def run():
server = UvicornServer(config=config)
# noinspection PyUnresolvedReferences
app.state.lns = LightNodeState()
app.state.session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=1))

server.start()
while True:
Expand Down

0 comments on commit 800317c

Please sign in to comment.