8000 PostgresNodePortManager(+company) was moved in own file - port_manage… · postgrespro/testgres@17c73cb · GitHub
[go: up one dir, main page]

Skip to content

Commit 17c73cb

Browse files
PostgresNodePortManager(+company) was moved in own file - port_manager.py
1 parent 4fbf51d commit 17c73cb

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_manager import PostgresNodePortManager
87+
from .port_manager import PostgresNodePortManager__ThisHost
88+
from .port_manager import PostgresNodePortManager__Generic
89+
8690
from .logger import TestgresLogger
8791

8892
from .pubsub import Publication, Subscription
8993

9094
from .standby import First
9195

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

103-
from .helpers.port_manager import PortForException
104-
105105
from .backup import NodeBackup
106106

107107
from .operations.os_ops import ConnectionParams
@@ -131,93 +131,10 @@ def __getattr__(self, name):
131131
return getattr(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-
class PostgresNodePortManager:
140-
def __init__(self):
141-
super().__init__()
142-
143-
def reserve_port(self) -> int:
144-
raise NotImplementedError("PostgresNodePortManager::reserve_port is not implemented.")
145-
146-
def release_port(self, number: int) -> None:
147-
assert type(number) == int # noqa: E721
148-
raise NotImplementedError("PostgresNodePortManager::release_port is not implemented.")
149-
150-
151-
class PostgresNodePortManager__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_guard is not None
161-
162-
if __class__.sm_single_instance is None:
163-
with __class__.sm_single_instance_guard:
164-
__class__.sm_single_instance = super().__new__(cls)
165-
assert __class__.sm_single_instance
166-
assert type(__class__.sm_single_instance) == __class__ # noqa: E721
167-
return __class__.sm_single_instance
168-
169-
def reserve_port(self) -> int:
170-
return utils.reserve_port()
171-
172-
def release_port(self, number: int) -> None:
173-
assert type(number) == int # noqa: E721
174-
return utils.release_port(number)
175-
176-
177-
class PostgresNodePortManager__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-
assert os_ops is not None
184-
assert isinstance(os_ops, OsOperations)
185-
self._os_ops = os_ops
186-
self._allocated_ports_guard = threading.Lock()
187-
self._allocated_ports = set[int]()
188-
189-
def reserve_port(self) -> int:
190-
ports = set(range(1024, 65535))
191-
assert type(ports) == set # noqa: E721
192-
193-
assert self._allocated_ports_guard is not None
194-
assert type(self._allocated_ports) == set # noqa: E721
195-
196-
with self._allocated_ports_guard:
197-
ports.difference_update(self._allocated_ports)
198-
199-
sampled_ports = random.sample(tuple(ports), min(len(ports), 100))
200-
201-
for port in sampled_ports:
202-
assert not (port in self._allocated_ports)
203-
204-
if not self._os_ops.is_port_free(port):
205-
continue
206-
207-
self._allocated_ports.add(port)
208-
return port
209-
210-
raise PortForException("Can't select a port")
211-
212-
def release_port(self, number: int) -> None:
213-
assert type(number) == int # noqa: E721
214-
215-
assert self._allocated_ports_guard is not None
216-
assert type(self._allocated_ports) == set # noqa: E721
217-
218-
with self._allocated_ports_guard:
219-
assert number in self._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
class PostgresNode(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_ops import OsOperations
2+
3+
from .helpers.port_manager import PortForException
4+
5+
from . import utils
6+
7+
import threading
8+
import random
9+
10+
class PostgresNodePortManager:
11+
def __init__(self):
12+
super().__init__()
13+
14+
def reserve_port(self) -> int:
15+
raise NotImplementedError("PostgresNodePortManager::reserve_port is not implemented.")
16+
17+
def release_port(self, number: int) -> None:
18+
assert type(number) == int # noqa: E721
19+
raise NotImplementedError("PostgresNodePortManager::release_port is not implemented.")
20+
21+
22+
class PostgresNodePortManager__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_guard is not None
32+
33+
if __class__.sm_single_instance is None:
34+
with __class__.sm_single_instance_guard:
35+
__class__.sm_single_instance = super().__new__(cls)
36+
assert __class__.sm_single_instance
37+
assert type(__class__.sm_single_instance) == __class__ # noqa: E721
38+
return __class__.sm_single_instance
39+
40+
def reserve_port(self) -> int:
41+
return utils.reserve_port()
42+
43+
def release_port(self, number: int) -> None:
44+
assert type(number) == int # noqa: E721
45+
return utils.release_port(number)
46+
47+
48+
class PostgresNodePortManager__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+
assert os_ops is not None
55+
assert isinstance(os_ops, OsOperations)
56+
self._os_ops = os_ops
57+
self._allocated_ports_guard = threading.Lock()
58+
self._allocated_ports = set[int]()
59+
60+
def reserve_port(self) -> int:
61+
ports = set(range(1024, 65535))
62+
assert type(ports) == set # noqa: E721
63+
64+
assert self._allocated_ports_guard is not None
65+
assert type(self._allocated_ports) == set # noqa: E721
66+
67+
with self._allocated_ports_guard:
68+
ports.difference_update(self._allocated_ports)
69+
70+
sampled_ports = random.sample(tuple(ports), min(len(ports), 100))
71+
72+
for port in sampled_ports:
73+
assert not (port in self._allocated_ports)
74+
75+
if not self._os_ops.is_port_free(port):
76+
continue
77+
78+
self._allocated_ports.add(port)
79+
return port
80+
81+
raise PortForException("Can't select a port")
82+
83+
def release_port(self, number: int) -> None:
84+
assert type(number) == int # noqa: E721
85+
86+
assert self._allocated_ports_guard is not None
87+
assert type(self._allocated_ports) == set # noqa: E721
88+
89+
with self._allocated_ports_guard:
90+
assert number in self._allocated_ports
91+
self._allocated_ports.discard(number)

0 commit comments

Comments
 (0)
0