Skip to content

add retry to http_client #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions nodes/dify_text_generator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import requests
import json
from ..utils import http_client

class DifyTextGenerator:
CATEGORY = "Browser"
Expand Down Expand Up @@ -39,7 +39,7 @@ def run(self, dify_api_endpoint, api_key, query, inputs_json_str=None):
# something weird, I have to add '{' and '}' manually
data["inputs"] = json.loads("{" + inputs_json_str + "}")

r = requests.post(
r = http_client().post(
dify_api_endpoint,
headers=header,
data=json.dumps(data),
Expand Down
4 changes: 2 additions & 2 deletions nodes/load_image_by_url.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import hashlib
import requests
import os
from PIL import Image, ImageSequence, ImageOps
import numpy as np
import torch
from ..utils import http_client

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我不太确定 relative import 是否在这里生效. 因为这个 python 的调用环境的 root 变了, 这个可能就报错了.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我其实也不太确定,我只能说本地和线上测试这样能跑通,原理没仔细研究…… 🙈



import folder_paths
Expand Down Expand Up @@ -36,7 +36,7 @@ def filename(self):

def download_by_url(self):
input_dir = folder_paths.get_input_directory()
res = requests.get(self.url)
res = http_client().get(self.url)
if res.status_code == 200:
download_path = os.path.join(input_dir, self.filename())
with open(download_path, 'wb') as file:
Expand Down
5 changes: 2 additions & 3 deletions nodes/upload_to_remote.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import requests
import threading
import asyncio
import json
Expand All @@ -7,7 +6,7 @@
import numpy as np
import io
import base64
from ..utils import log
from ..utils import http_client, log

class UploadToRemote:
CATEGORY = "Browser"
Expand Down Expand Up @@ -89,7 +88,7 @@ async def callback(images, extra, remote_url, extension='jpeg', quality=85, embe
}
data = json.dumps(data).encode('utf-8')
log(f"uploading {track_id} to {remote_url}")
res = requests.post(remote_url, data=data, headers=headers)
res = http_client().post(remote_url, data=data, headers=headers)
log(f"uploaded {track_id}: {res.status_code} {res.text}")
# TODO: check the response

Expand Down
5 changes: 2 additions & 3 deletions nodes/xyz_plot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import shutil
import time
import requests
import json
from PIL import Image
import numpy as np
Expand All @@ -9,7 +8,7 @@

import folder_paths

from ..utils import SERVER_BASE_URL
from ..utils import SERVER_BASE_URL, http_client

class XyzPlot:
CATEGORY = "Browser"
Expand Down Expand Up @@ -107,7 +106,7 @@ def queue_new_prompt(prompt):

# for some special network environments like AutoDL
proxies = {"http": "", "https": ""}
return requests.post(SERVER_BASE_URL + '/prompt', data=data, proxies=proxies)
return http_client().post(SERVER_BASE_URL + '/prompt', data=data, proxies=proxies)


batch_size = len(images)
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
tqdm
pandas
numpy
requests
urllib3
5 changes: 2 additions & 3 deletions routes/downloads.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from os import path
import os
import shutil
import requests
import time
import asyncio
import json
Expand All @@ -11,7 +10,7 @@

import folder_paths

from ..utils import download_logs_path, log
from ..utils import download_logs_path, log, http_client

def parse_options_header(content_disposition):
param, options = '', {}
Expand Down Expand Up @@ -53,7 +52,7 @@ async def download_by_requests(uuid:str, download_url:str, save_in:str, filename

HEADERS = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"}

with requests.get(download_url, headers=HEADERS, stream=True) as resp:
with http_client().get(download_url, headers=HEADERS, stream=True) as resp:
MISSING_FILENAME = f"unkwown_{uuid}"
# get file name
if filename == "":
Expand Down
11 changes: 11 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import subprocess
import time
from typing import TypedDict, List
import requests
from requests.adapters import HTTPAdapter, Retry

import folder_paths
from comfy.cli_args import args
Expand All @@ -24,6 +26,15 @@

git_remote_name = 'origin'

def http_client():
adapter = HTTPAdapter(max_retries=Retry(3, backoff_factor=0.1))
http = requests.session()
http.mount('http://', adapter)
http.mount('https://', adapter)

return http


@functools.cache
def get_config():
return {
Expand Down