10
10
import logging
11
11
import peewee
12
12
13
+
13
14
# TODO: add ipv6 addresses, make domain checking better
14
15
_0_TO_255_REGEX = r"([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"
15
16
DOMAIN_LETTER_REGEX = r"[a-zA-Z0-9_\-]"
@@ -77,14 +78,14 @@ async def worker(self):
77
78
78
79
async def process_proxies (self ):
79
80
while True :
80
- await asyncio .sleep (0.00001 )
81
+ await asyncio .sleep (0.01 )
81
82
try :
82
83
# check good proxies
83
84
proxies = await db .execute (
84
85
Proxy .select ().where (
85
86
Proxy .number_of_bad_checks == 0 ,
86
87
Proxy .last_check_time < time .time () - Proxy .checking_period ,
87
- ).order_by (Proxy .last_check_time ).limit (settings .NUMBER_OF_CONCURRENT_TASKS )
88
+ ).order_by (Proxy .last_check_time ).limit (settings .NUMBER_OF_CONCURRENT_TASKS )
88
89
)
89
90
if proxies :
90
91
self .good_proxies_are_processed = False
@@ -131,9 +132,8 @@ async def process_proxies(self):
131
132
132
133
async def process_collectors (self ):
133
134
while True :
135
+ await asyncio .sleep (0.1 )
134
136
try :
135
- await asyncio .sleep (0.000001 )
136
-
137
137
# check collectors
138
138
collector_states = await db .execute (
139
139
CollectorState .select ().where (
@@ -155,16 +155,14 @@ async def process_collectors(self):
155
155
await asyncio .sleep (settings .SLEEP_AFTER_ERROR_PERIOD )
156
156
157
157
async def add_proxy_to_queue (self , proxy : Proxy , collector_id = None ):
158
- while self .proxies_semaphore .locked ():
159
- await asyncio .sleep (0.001 )
160
-
161
- asyncio .ensure_future (self .process_proxy (
162
- proxy .get_raw_protocol (),
163
- proxy .auth_data ,
164
- proxy .domain ,
165
- proxy .port ,
166
- collector_id ,
167
- ))
158
+ async with self .proxies_semaphore :
159
+ asyncio .ensure_future (self .process_proxy (
160
+ proxy .get_raw_protocol (),
161
+ proxy .auth_data ,
162
+ proxy .domain ,
163
+ proxy .port ,
164
+ collector_id ,
165
+ ))
168
166
169
167
async def add_proxies_to_queue (self , proxies : list ):
170
168
for proxy in proxies :
@@ -258,15 +256,16 @@ async def process_raw_proxy(self, proxy, collector_id):
258
256
259
257
for raw_protocol in range (len (Proxy .PROTOCOLS )):
260
258
while not self .good_proxies_are_processed :
261
- await asyncio .sleep (0.01 )
259
+ # TODO: find a better way
260
+ await asyncio .sleep (0.1 )
262
261
263
262
new_proxy = Proxy ()
264
263
new_proxy .raw_protocol = raw_protocol
265
264
new_proxy .auth_data = auth_data
266
265
new_proxy .domain = domain
267
266
new_proxy .port = port
268
267
269
- self .add_proxy_to_queue (new_proxy , collector_id )
268
+ await self .add_proxy_to_queue (new_proxy , collector_id )
270
269
271
270
async def process_proxy (self , raw_protocol : int , auth_data : str , domain : str , port : int , collector_id ):
272
271
async with self .proxies_semaphore :
0 commit comments