From d7ca5f4725c610f7ae780b0194594507973053b1 Mon Sep 17 00:00:00 2001 From: dqy <1016751306@qq.com> Date: Sat, 21 Oct 2023 20:42:41 +0800 Subject: [PATCH] feat: change timeout --- src/server.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/server.py b/src/server.py index d030e2c..e6f8107 100644 --- a/src/server.py +++ b/src/server.py @@ -2,11 +2,9 @@ from fastapi import FastAPI from fastapi.encoders import jsonable_encoder from fastapi.responses import JSONResponse from contextlib import asynccontextmanager -from typing import Tuple, Callable import sqlite3 import asyncio import time -import random @asynccontextmanager async def lifespan(app: FastAPI): @@ -28,7 +26,7 @@ cursor.execute('''CREATE TABLE IF NOT EXISTS nodes ( )''') def init(): - task = asyncio.create_task(receive_heartbeat_internal()) + asyncio.create_task(receive_heartbeat_internal()) def clean_env(): # 关闭游标和连接 @@ -93,16 +91,13 @@ async def receive_heartbeat(ip: str): cursor.execute("UPDATE nodes SET last_heartbeat = ? WHERE ip = ?", (time.time(), ip)) return {"status": "received"} -async def receive_heartbeat_internal() -> int: +async def receive_heartbeat_internal(): while 1: - print('successful delete1') - timeout = 10 + timeout = 70 # 删除超时的节点 cursor.execute("DELETE FROM nodes WHERE last_heartbeat < ?", (time.time() - timeout,)) conn.commit() - print('successful delete') await asyncio.sleep(timeout) - return 1 @app.get("/server/send_nodes_list") async def send_nodes_list(count: int) -> JSONResponse: