-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect_box.py
106 lines (79 loc) · 3.24 KB
/
connect_box.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import psycopg2
import os
import json
import threading
import time
lock = threading.Lock()
class ConnectionBase():
""""Class connection base"""
def __init__(self) -> None:
self.__load_config()
try:
self.connection = psycopg2.connect(
host="localhost",
database = self.__db_name,
user = self.__user_db,
password = self.__pass_db)
self.cursor = self.connection.cursor()
except psycopg2.OperationalError:
self.connection = None
self.cursor = None
def __del__(self) -> None:
self.cursor.close()
self.connection.close()
def __load_config(self) -> None:
"""Load config parameters"""
if os.path.isfile('config.ini'):
with open('config.ini', 'r') as outfile:
data = json.load(outfile)
self.__db_name = data['db_name']
self.__user_db = data['user_db']
self.__pass_db = data['pass_db']
else:
print('Configuration file not found')
raise NameError('No configuration file')
class ConnectionPool():
"""Class connection Pool"""
def __init__(self) -> None:
self._start_user_connect = 10
self.list_connection = {ConnectionBase(): False for _ in range(self._start_user_connect)}
self.__max_user_connect = 100
self.__max_free_connect = 5
def __add_new_connect(self) -> None:
""""Add new connect to data base"""
if self.__count_free_connect() < self.__max_free_connect and not len(self.list_connection) == self.__max_user_connect:
self.list_connection[ConnectionBase()] = False
def __del_connect(self) -> None:
"""Delete not use connect"""
while self.__count_free_connect() > self.__max_free_connect:
del self.list_connection[self.__check_no_busy_connect()]
def __check_no_busy_connect(self) -> int:
"""Check no busy connect - return index"""
for value in self.list_connection.items():
if value[1] == False and value[0].connection != None:
return value[0]
return None
def __count_free_connect(self) -> int:
"""Count free connect"""
return sum(value == False for value in self.list_connection.values())
def get_connect(self) -> ConnectionBase:
"""Get connect to data base"""
global lock
while True:
lock.acquire()
free_socket = self.__check_no_busy_connect()
if free_socket != -1:
self.list_connection[free_socket] = True
if self.__count_free_connect() < self.__max_free_connect:
self.__add_new_connect()
lock.release()
if free_socket == None:
print('asf')
time.sleep(1)
if free_socket != None:
return free_socket
def put_connect(self, connect:ConnectionBase) -> None:
"""Put connection"""
self.list_connection[connect] = False
#while self.__count_free_connect() > self.__max_free_connect:
self.__del_connect()