8000 PostgresNodePortManager__ThisHost is defined (was: PostgresNodePortMa… · postgrespro/testgres@0f842fd · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f842fd

Browse files
PostgresNodePortManager__ThisHost is defined (was: PostgresNodePortManager__Global)
It is a singleton.
1 parent ef095d3 commit 0f842fd

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

testgres/node.py

Lines changed: 21 additions & 2 deletions< 10000 div class="d-flex">
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# coding: utf-8
2+
from __future__ import annotations
3+
24
import logging
35
import os
46
import random
@@ -133,6 +135,9 @@ def __repr__(self):
133135

134136

135137
class PostgresNodePortManager:
138+
def __init__(self):
139+
super().__init__()
140+
136141
def reserve_port(self) -> int:
137142
raise NotImplementedError("PostManager::reserve_port is not implemented.")
138143

@@ -141,10 +146,24 @@ def release_port(self, number: int) -> None:
141146
raise NotImplementedError("PostManager::release_port is not implemented.")
142147

143148

144-
class PostgresNodePortManager__Global(PostgresNodePortManager):
149+
class PostgresNodePortManager__ThisHost(PostgresNodePortManager):
150+
sm_single_instance: PostgresNodePortManager = None
151+
sm_single_instance_guard = threading.Lock()
152+
145153
def __init__(self):
146154
pass
147155

156+
def __new__(cls) -> PostgresNodePortManager:
157+
assert __class__ == PostgresNodePortManager__ThisHost
158+
assert __class__.sm_single_instance_guard is not None
159+
160+
if __class__.sm_single_instance is None:
161+
with __class__.sm_single_instance_guard:
162+
__class__.sm_single_instance = super().__new__(cls)
163+
assert __class__.sm_single_instance
164+
assert type(__class__.sm_single_instance) == __class__ # noqa: E721
165+
return __class__.sm_single_instance
166+
148167
def reserve_port(self) -> int:
149168
return utils.reserve_port()
150169

@@ -290,7 +309,7 @@ def _get_port_manager(os_ops: OsOperations) -> PostgresNodePortManager:
290309
assert isinstance(os_ops, OsOperations)
291310

292311
# [2025-04-03] It is our old, incorrected behaviour
293-
return PostgresNodePortManager__Global()
312+
return PostgresNodePortManager__ThisHost()
294313

295314
def clone_with_new_name_and_base_dir(self, name: str, base_dir: str):
296315
assert name is None or type(name) == str # noqa: E721

0 commit comments

Comments
 (0)
0