DrValera commited on
Commit
4d85bf3
·
verified ·
1 Parent(s): 24de318

Adjusting ping from datfid master to datfid api to avoid sleeping

Browse files
Files changed (1) hide show
  1. main.py +19 -11
main.py CHANGED
@@ -123,8 +123,7 @@ async def _forward(path: str, method: str = "GET", json_body=None, user_token: s
123
  Forward request to the PRIVATE Space:
124
  - 'Authorization: Bearer <HF_TOKEN>' to pass HF private gate
125
  - 'X-API-Key: <dt+...>' so your private app can validate the user token
126
- While waiting, pings upstream every PING_UPSTREAM_INTERVAL seconds so the backend Space does not sleep.
127
- Uses explicit wait (response OR ping timer) so pings run even when the event loop doesn't preempt the request.
128
  """
129
  url = f"{UPSTREAM_URL}{path}"
130
  headers = {
@@ -134,8 +133,12 @@ async def _forward(path: str, method: str = "GET", json_body=None, user_token: s
134
  if user_token:
135
  headers["X-API-Key"] = user_token
136
 
137
- ping_url = f"{UPSTREAM_URL}/"
 
138
  ping_headers = {"Authorization": f"Bearer {HF_TOKEN}"}
 
 
 
139
  timeout = httpx.Timeout(UPSTREAM_TIMEOUT)
140
  r = None
141
 
@@ -166,7 +169,6 @@ async def _forward(path: str, method: str = "GET", json_body=None, user_token: s
166
  pass
167
  if request_task in done:
168
  break
169
- # Ping timer fired: hit upstream so backend does not sleep
170
  try:
171
  async with httpx.AsyncClient(timeout=10.0) as client:
172
  await client.get(ping_url, headers=ping_headers)
@@ -211,7 +213,10 @@ async def _forward_stream(path: str, files=None, data=None, user_token: str | No
211
  if user_token:
212
  headers["X-API-Key"] = user_token
213
 
214
- ping_task = _start_ping_task(f"{UPSTREAM_URL}/", {"Authorization": f"Bearer {HF_TOKEN}"})
 
 
 
215
  timeout = httpx.Timeout(UPSTREAM_TIMEOUT)
216
  client = httpx.AsyncClient(timeout=timeout, follow_redirects=True)
217
 
@@ -275,7 +280,7 @@ async def _forward_stream(path: str, files=None, data=None, user_token: str | No
275
 
276
 
277
  async def _forward_multipart_json(path: str, files=None, data=None, user_token: str | None = None, method: str = "POST"):
278
- """POST multipart (files + data) to upstream and return JSON. Explicit ping-while-wait (same as _forward)."""
279
  url = f"{UPSTREAM_URL}{path}"
280
  headers = {
281
  "Authorization": f"Bearer {HF_TOKEN}",
@@ -283,8 +288,10 @@ async def _forward_multipart_json(path: str, files=None, data=None, user_token:
283
  }
284
  if user_token:
285
  headers["X-API-Key"] = user_token
286
- ping_url = f"{UPSTREAM_URL}/"
287
  ping_headers = {"Authorization": f"Bearer {HF_TOKEN}"}
 
 
288
  timeout = httpx.Timeout(UPSTREAM_TIMEOUT)
289
  r = None
290
 
@@ -432,10 +439,11 @@ async def root(req: Request):
432
  @app.get("/keep-alive")
433
  async def keep_alive():
434
  """
435
- Hit this from an external cron (e.g. every 4 min) to keep this Space and the backend awake.
436
- Sleep timeout is configured on Hugging Face (Space settings), not in code; pinging more
437
- often than that (e.g. every 4 min if HF sleep is 5 min) prevents both spaces from sleeping.
438
- Pings the upstream API so both see activity. No auth; use UptimeRobot, cron-job.org, etc.
 
439
  """
440
  try:
441
  url = f"{UPSTREAM_URL}/"
 
123
  Forward request to the PRIVATE Space:
124
  - 'Authorization: Bearer <HF_TOKEN>' to pass HF private gate
125
  - 'X-API-Key: <dt+...>' so your private app can validate the user token
126
+ While waiting for the result, sends secure_ping to upstream every PING_UPSTREAM_INTERVAL seconds.
 
127
  """
128
  url = f"{UPSTREAM_URL}{path}"
129
  headers = {
 
133
  if user_token:
134
  headers["X-API-Key"] = user_token
135
 
136
+ # While waiting for upstream, call backend secure_ping every PING_UPSTREAM_INTERVAL seconds
137
+ ping_url = f"{UPSTREAM_URL}/secure-ping/"
138
  ping_headers = {"Authorization": f"Bearer {HF_TOKEN}"}
139
+ if user_token:
140
+ ping_headers["X-API-Key"] = user_token
141
+
142
  timeout = httpx.Timeout(UPSTREAM_TIMEOUT)
143
  r = None
144
 
 
169
  pass
170
  if request_task in done:
171
  break
 
172
  try:
173
  async with httpx.AsyncClient(timeout=10.0) as client:
174
  await client.get(ping_url, headers=ping_headers)
 
213
  if user_token:
214
  headers["X-API-Key"] = user_token
215
 
216
+ ping_headers = {"Authorization": f"Bearer {HF_TOKEN}"}
217
+ if user_token:
218
+ ping_headers["X-API-Key"] = user_token
219
+ ping_task = _start_ping_task(f"{UPSTREAM_URL}/secure-ping/", ping_headers)
220
  timeout = httpx.Timeout(UPSTREAM_TIMEOUT)
221
  client = httpx.AsyncClient(timeout=timeout, follow_redirects=True)
222
 
 
280
 
281
 
282
  async def _forward_multipart_json(path: str, files=None, data=None, user_token: str | None = None, method: str = "POST"):
283
+ """POST multipart to upstream and return JSON. Sends secure_ping every PING_UPSTREAM_INTERVAL while waiting."""
284
  url = f"{UPSTREAM_URL}{path}"
285
  headers = {
286
  "Authorization": f"Bearer {HF_TOKEN}",
 
288
  }
289
  if user_token:
290
  headers["X-API-Key"] = user_token
291
+ ping_url = f"{UPSTREAM_URL}/secure-ping/"
292
  ping_headers = {"Authorization": f"Bearer {HF_TOKEN}"}
293
+ if user_token:
294
+ ping_headers["X-API-Key"] = user_token
295
  timeout = httpx.Timeout(UPSTREAM_TIMEOUT)
296
  r = None
297
 
 
439
  @app.get("/keep-alive")
440
  async def keep_alive():
441
  """
442
+ Hit this to keep this Space (datfid_master) and the backend awake.
443
+ - External cron (e.g. every 4 min): UptimeRobot, cron-job.org keeps both spaces awake when idle.
444
+ - During a long request (>5 min): the client can call this in a background thread every ~4 min
445
+ so HF does not put this Space to sleep while it is waiting for the backend (no response
446
+ sent to the client yet). This endpoint also pings the upstream (backend). No auth required.
447
  """
448
  try:
449
  url = f"{UPSTREAM_URL}/"