Skip to content

Commit dd3258c

Browse files
committed
optimized db
1 parent 31df4d9 commit dd3258c

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Meta:
1919
(('domain',), False),
2020
(('port',), False),
2121
(('number_of_bad_checks',), False),
22+
(('next_check_time',), False),
2223
(('last_check_time',), False),
2324
(('checking_period',), False),
2425
(('uptime',), False),
@@ -42,6 +43,7 @@ class Meta:
4243

4344
checking_period = peewee.IntegerField(default=settings.MIN_PROXY_CHECKING_PERIOD, null=False)
4445
last_check_time = peewee.IntegerField(default=0, null=False)
46+
next_check_time = peewee.IntegerField(default=0, null=False)
4547
number_of_bad_checks = peewee.IntegerField(default=0, null=False)
4648
uptime = peewee.IntegerField(default=None, null=True)
4749
bad_uptime = peewee.IntegerField(default=None, null=True)

processor.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ async def process_proxies(self):
7474
proxies = await db.execute(
7575
Proxy.select().where(
7676
Proxy.number_of_bad_checks == 0,
77-
Proxy.last_check_time < time.time() - Proxy.checking_period,
78-
).order_by(Proxy.last_check_time).limit(settings.NUMBER_OF_CONCURRENT_TASKS)
77+
Proxy.next_check_time < time.time(),
78+
).order_by(Proxy.next_check_time).limit(settings.NUMBER_OF_CONCURRENT_TASKS)
7979
)
8080
if proxies:
8181
self.good_proxies_are_processed = False
@@ -92,8 +92,8 @@ async def process_proxies(self):
9292
Proxy.select().where(
9393
Proxy.number_of_bad_checks > 0,
9494
Proxy.number_of_bad_checks < settings.DEAD_PROXY_THRESHOLD,
95-
Proxy.last_check_time < time.time() - settings.BAD_PROXY_CHECKING_PERIOD,
96-
).order_by(Proxy.last_check_time).limit(settings.NUMBER_OF_CONCURRENT_TASKS)
95+
Proxy.next_check_time < time.time(),
96+
).order_by(Proxy.next_check_time).limit(settings.NUMBER_OF_CONCURRENT_TASKS)
9797
)
9898

9999
await self.add_proxies_to_queue(proxies)
@@ -106,8 +106,8 @@ async def process_proxies(self):
106106
Proxy.select().where(
107107
Proxy.number_of_bad_checks >= settings.DEAD_PROXY_THRESHOLD,
108108
Proxy.number_of_bad_checks < settings.DO_NOT_CHECK_ON_N_BAD_CHECKS,
109-
Proxy.last_check_time < time.time() - settings.DEAD_PROXY_CHECKING_PERIOD,
110-
).order_by(Proxy.last_check_time).limit(settings.NUMBER_OF_CONCURRENT_TASKS)
109+
Proxy.next_check_time < time.time(),
110+
).order_by(Proxy.next_check_time).limit(settings.NUMBER_OF_CONCURRENT_TASKS)
111111
)
112112

113113
await self.add_proxies_to_queue(proxies)
@@ -293,6 +293,7 @@ async def process_proxy(self, raw_protocol: int, auth_data: str, domain: str, po
293293
)
294294

295295
proxy.last_check_time = int(time.time())
296+
proxy.next_check_time = proxy.last_check_time + proxy.checking_period
296297
proxy.number_of_bad_checks += 1
297298
proxy.uptime = int(time.time())
298299

@@ -374,4 +375,8 @@ async def create_or_update_proxy(
374375
+ (checking_time / settings.PROXY_CHECKING_TIMEOUT) \
375376
* (settings.MAX_PROXY_CHECKING_PERIOD - settings.MIN_PROXY_CHECKING_PERIOD)
376377

378+
# TODO: bad and not working proxies period
379+
380+
proxy.next_check_time = proxy.last_check_time + proxy.checking_period
381+
377382
await db.update(proxy)

proxy_py/_settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383

8484
PROXY_PROVIDER_SERVER_API_CONFIG_FETCH_CONFIG = {
8585
'fields': [
86-
'address', 'protocol', 'auth_data', 'domain', 'port', 'last_check_time', 'number_of_bad_checks',
86+
'address', 'protocol', 'auth_data', 'domain', 'port', 'last_check_time', 'next_check_time', 'number_of_bad_checks',
8787
'bad_proxy', 'uptime', 'response_time', 'white_ipv4', 'white_ipv6', 'city', 'country_code', 'region'
8888
],
8989
'filter_fields': [

statistics/statistics.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,23 @@ async def number_of_proxies_to_process(timestamp):
5555
good_proxies_count = await db.count(
5656
Proxy.select().where(
5757
Proxy.number_of_bad_checks == 0,
58-
Proxy.last_check_time < timestamp - Proxy.checking_period,
58+
Proxy.next_check_time < timestamp - Proxy.checking_period,
5959
)
6060
)
6161

6262
bad_proxies_count = await db.count(
6363
Proxy.select().where(
6464
Proxy.number_of_bad_checks > 0,
6565
Proxy.number_of_bad_checks < settings.DEAD_PROXY_THRESHOLD,
66-
Proxy.last_check_time < timestamp - settings.BAD_PROXY_CHECKING_PERIOD,
66+
Proxy.next_check_time < timestamp - settings.BAD_PROXY_CHECKING_PERIOD,
6767
)
6868
)
6969

7070
dead_proxies_count = await db.count(
7171
Proxy.select().where(
7272
Proxy.number_of_bad_checks >= settings.DEAD_PROXY_THRESHOLD,
7373
Proxy.number_of_bad_checks < settings.DO_NOT_CHECK_ON_N_BAD_CHECKS,
74-
Proxy.last_check_time < timestamp - settings.DEAD_PROXY_CHECKING_PERIOD,
74+
Proxy.next_check_time < timestamp - settings.DEAD_PROXY_CHECKING_PERIOD,
7575
)
7676
)
7777

0 commit comments

Comments
 (0)