Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit17c73cb

Browse files
PostgresNodePortManager(+company) was moved in own file - port_manager.py
1 parent4fbf51d commit17c73cb

File tree

2 files changed

+99
-91
lines changed

2 files changed

+99
-91
lines changed

‎testgres/node.py

Lines changed: 8 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,16 @@
8383
BackupException, \
8484
InvalidOperationException
8585

86+
from .port_managerimportPostgresNodePortManager
87+
from .port_managerimportPostgresNodePortManager__ThisHost
88+
from .port_managerimportPostgresNodePortManager__Generic
89+
8690
from .loggerimportTestgresLogger
8791

8892
from .pubsubimportPublication,Subscription
8993

9094
from .standbyimportFirst
9195

92-
from .importutils
93-
9496
from .utilsimport \
9597
PgVer, \
9698
eprint, \
@@ -100,8 +102,6 @@
100102
options_string, \
101103
clean_on_error
102104

103-
from .helpers.port_managerimportPortForException
104-
105105
from .backupimportNodeBackup
106106

107107
from .operations.os_opsimportConnectionParams
@@ -131,93 +131,10 @@ def __getattr__(self, name):
131131
returngetattr(self.process,name)
132132

133133
def__repr__(self):
134-
return'{}(ptype={}, process={})'.format(self.__class__.__name__,
135-
str(self.ptype),
136-
repr(self.process))
137-
138-
139-
classPostgresNodePortManager:
140-
def__init__(self):
141-
super().__init__()
142-
143-
defreserve_port(self)->int:
144-
raiseNotImplementedError("PostgresNodePortManager::reserve_port is not implemented.")
145-
146-
defrelease_port(self,number:int)->None:
147-
asserttype(number)==int# noqa: E721
148-
raiseNotImplementedError("PostgresNodePortManager::release_port is not implemented.")
149-
150-
151-
classPostgresNodePortManager__ThisHost(PostgresNodePortManager):
152-
sm_single_instance:PostgresNodePortManager=None
153-
sm_single_instance_guard=threading.Lock()
154-
155-
def__init__(self):
156-
pass
157-
158-
def__new__(cls)->PostgresNodePortManager:
159-
assert__class__==PostgresNodePortManager__ThisHost
160-
assert__class__.sm_single_instance_guardisnotNone
161-
162-
if__class__.sm_single_instanceisNone:
163-
with__class__.sm_single_instance_guard:
164-
__class__.sm_single_instance=super().__new__(cls)
165-
assert__class__.sm_single_instance
166-
asserttype(__class__.sm_single_instance)==__class__# noqa: E721
167-
return__class__.sm_single_instance
168-
169-
defreserve_port(self)->int:
170-
returnutils.reserve_port()
171-
172-
defrelease_port(self,number:int)->None:
173-
asserttype(number)==int# noqa: E721
174-
returnutils.release_port(number)
175-
176-
177-
classPostgresNodePortManager__Generic(PostgresNodePortManager):
178-
_os_ops:OsOperations
179-
_allocated_ports_guard:object
180-
_allocated_ports:set[int]
181-
182-
def__init__(self,os_ops:OsOperations):
183-
assertos_opsisnotNone
184-
assertisinstance(os_ops,OsOperations)
185-
self._os_ops=os_ops
186-
self._allocated_ports_guard=threading.Lock()
187-
self._allocated_ports=set[int]()
188-
189-
defreserve_port(self)->int:
190-
ports=set(range(1024,65535))
191-
asserttype(ports)==set# noqa: E721
192-
193-
assertself._allocated_ports_guardisnotNone
194-
asserttype(self._allocated_ports)==set# noqa: E721
195-
196-
withself._allocated_ports_guard:
197-
ports.difference_update(self._allocated_ports)
198-
199-
sampled_ports=random.sample(tuple(ports),min(len(ports),100))
200-
201-
forportinsampled_ports:
202-
assertnot (portinself._allocated_ports)
203-
204-
ifnotself._os_ops.is_port_free(port):
205-
continue
206-
207-
self._allocated_ports.add(port)
208-
returnport
209-
210-
raisePortForException("Can't select a port")
211-
212-
defrelease_port(self,number:int)->None:
213-
asserttype(number)==int# noqa: E721
214-
215-
assertself._allocated_ports_guardisnotNone
216-
asserttype(self._allocated_ports)==set# noqa: E721
217-
218-
withself._allocated_ports_guard:
219-
assertnumberinself._allocated_ports
220-
self._allocated_ports.discard(number)
134+
return'{}(ptype={}, process={})'.format(
135+
self.__class__.__name__,
136+
str(self.ptype),
137+
repr(self.process))
221138

222139

223140
classPostgresNode(object):

‎testgres/port_manager.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from .operations.os_opsimportOsOperations
2+
3+
from .helpers.port_managerimportPortForException
4+
5+
from .importutils
6+
7+
importthreading
8+
importrandom
9+
10+
classPostgresNodePortManager:
11+
def__init__(self):
12+
super().__init__()
13+
14+
defreserve_port(self)->int:
15+
raiseNotImplementedError("PostgresNodePortManager::reserve_port is not implemented.")
16+
17+
defrelease_port(self,number:int)->None:
18+
asserttype(number)==int# noqa: E721
19+
raiseNotImplementedError("PostgresNodePortManager::release_port is not implemented.")
20+
21+
22+
classPostgresNodePortManager__ThisHost(PostgresNodePortManager):
23+
sm_single_instance:PostgresNodePortManager=None
24+
sm_single_instance_guard=threading.Lock()
25+
26+
def__init__(self):
27+
pass
28+
29+
def__new__(cls)->PostgresNodePortManager:
30+
assert__class__==PostgresNodePortManager__ThisHost
31+
assert__class__.sm_single_instance_guardisnotNone
32+
33+
if__class__.sm_single_instanceisNone:
34+
with__class__.sm_single_instance_guard:
35+
__class__.sm_single_instance=super().__new__(cls)
36+
assert__class__.sm_single_instance
37+
asserttype(__class__.sm_single_instance)==__class__# noqa: E721
38+
return__class__.sm_single_instance
39+
40+
defreserve_port(self)->int:
41+
returnutils.reserve_port()
42+
43+
defrelease_port(self,number:int)->None:
44+
asserttype(number)==int# noqa: E721
45+
returnutils.release_port(number)
46+
47+
48+
classPostgresNodePortManager__Generic(PostgresNodePortManager):
49+
_os_ops:OsOperations
50+
_allocated_ports_guard:object
51+
_allocated_ports:set[int]
52+
53+
def__init__(self,os_ops:OsOperations):
54+
assertos_opsisnotNone
55+
assertisinstance(os_ops,OsOperations)
56+
self._os_ops=os_ops
57+
self._allocated_ports_guard=threading.Lock()
58+
self._allocated_ports=set[int]()
59+
60+
defreserve_port(self)->int:
61+
ports=set(range(1024,65535))
62+
asserttype(ports)==set# noqa: E721
63+
64+
assertself._allocated_ports_guardisnotNone
65+
asserttype(self._allocated_ports)==set# noqa: E721
66+
67+
withself._allocated_ports_guard:
68+
ports.difference_update(self._allocated_ports)
69+
70+
sampled_ports=random.sample(tuple(ports),min(len(ports),100))
71+
72+
forportinsampled_ports:
73+
assertnot (portinself._allocated_ports)
74+
75+
ifnotself._os_ops.is_port_free(port):
76+
continue
77+
78+
self._allocated_ports.add(port)
79+
returnport
80+
81+
raisePortForException("Can't select a port")
82+
83+
defrelease_port(self,number:int)->None:
84+
asserttype(number)==int# noqa: E721
85+
86+
assertself._allocated_ports_guardisnotNone
87+
asserttype(self._allocated_ports)==set# noqa: E721
88+
89+
withself._allocated_ports_guard:
90+
assertnumberinself._allocated_ports
91+
self._allocated_ports.discard(number)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp