Skip to content

Commit 8ff9a64

Browse files
committed
add retry to http_client
1 parent 23f77cd commit 8ff9a64

File tree

7 files changed

+23
-13
lines changed

7 files changed

+23
-13
lines changed

nodes/dify_text_generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import requests
21
import json
2+
from ..utils import http_client
33

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

42-
r = requests.post(
42+
r = http_client().post(
4343
dify_api_endpoint,
4444
headers=header,
4545
data=json.dumps(data),

nodes/load_image_by_url.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import hashlib
2-
import requests
32
import os
43
from PIL import Image, ImageSequence, ImageOps
54
import numpy as np
65
import torch
6+
from ..utils import http_client
77

88

99
import folder_paths
@@ -36,7 +36,7 @@ def filename(self):
3636

3737
def download_by_url(self):
3838
input_dir = folder_paths.get_input_directory()
39-
res = requests.get(self.url)
39+
res = http_client().get(self.url)
4040
if res.status_code == 200:
4141
download_path = os.path.join(input_dir, self.filename())
4242
with open(download_path, 'wb') as file:

nodes/upload_to_remote.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import requests
21
import threading
32
import asyncio
43
import json
@@ -7,7 +6,7 @@
76
import numpy as np
87
import io
98
import base64
10-
from ..utils import log
9+
from ..utils import http_client, log
1110

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

nodes/xyz_plot.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import shutil
22
import time
3-
import requests
43
import json
54
from PIL import Image
65
import numpy as np
@@ -9,7 +8,7 @@
98

109
import folder_paths
1110

12-
from ..utils import SERVER_BASE_URL
11+
from ..utils import SERVER_BASE_URL, http_client
1312

1413
class XyzPlot:
1514
CATEGORY = "Browser"
@@ -107,7 +106,7 @@ def queue_new_prompt(prompt):
107106

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

112111

113112
batch_size = len(images)

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
tqdm
22
pandas
33
numpy
4+
requests
5+
urllib3

routes/downloads.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from os import path
22
import os
33
import shutil
4-
import requests
54
import time
65
import asyncio
76
import json
@@ -11,7 +10,7 @@
1110

1211
import folder_paths
1312

14-
from ..utils import download_logs_path, log
13+
from ..utils import download_logs_path, log, http_client
1514

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

5453
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"}
5554

56-
with requests.get(download_url, headers=HEADERS, stream=True) as resp:
55+
with http_client().get(download_url, headers=HEADERS, stream=True) as resp:
5756
MISSING_FILENAME = f"unkwown_{uuid}"
5857
# get file name
5958
if filename == "":

utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import subprocess
55
import time
66
from typing import TypedDict, List
7+
import requests
8+
from requests.adapters import HTTPAdapter, Retry
79

810
import folder_paths
911
from comfy.cli_args import args
@@ -24,6 +26,15 @@
2426

2527
git_remote_name = 'origin'
2628

29+
def http_client():
30+
adapter = HTTPAdapter(max_retries=Retry(3, backoff_factor=0.1))
31+
http = requests.session()
32+
http.mount('http://', adapter)
33+
http.mount('https://', adapter)
34+
35+
return http
36+
37+
2738
@functools.cache
2839
def get_config():
2940
return {

0 commit comments

Comments
 (0)