Skip to content

Commit a839e29

Browse files
joamagclaude
andcommitted
feat: add diag_owner flag to Container and document base methods
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent b960bbb commit a839e29

3 files changed

Lines changed: 51 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12-
*
12+
* `diag_owner` flag in `Container` to restrict diagnostics to the owner base, avoiding port binding conflicts in multi-base setups
1313

1414
### Changed
1515

16-
*
16+
* Add docstrings to `Container.add_base()`, `remove_base()` and `start_base()`
17+
* Pass `diag_owner=True` in `ProxyServer` container to prevent child clients from binding to the diag port
1718

1819
### Fixed
1920

src/netius/base/container.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Container(Base):
3737

3838
def __init__(self, *args, **kwargs):
3939
Base.__init__(self, *args, **kwargs)
40+
self.diag_owner = kwargs.get("diag_owner", False)
4041
self.owner = None
4142
self.bases = []
4243

@@ -150,15 +151,61 @@ def on_stop(self):
150151
self.call_all("on_stop")
151152

152153
def add_base(self, base):
154+
"""
155+
Adds a base structure to the container, applying the
156+
container's shared poll, logger and thread identity to
157+
the base before appending it to the internal list of
158+
managed bases.
159+
160+
:type base: Base/Agent
161+
:param base: The base structure (server, client or agent)
162+
to be added to the container.
163+
"""
164+
153165
self.apply_base(base)
154166
self.bases.append(base)
155167

156168
def remove_base(self, base):
169+
"""
170+
Removes a previously added base structure from the
171+
container's internal list of managed bases. After this
172+
call the base will no longer receive poll events, tick
173+
callbacks or lifecycle notifications from the container.
174+
175+
:type base: Base/Agent
176+
:param base: The base structure to be removed from the
177+
container.
178+
"""
179+
157180
self.bases.remove(base)
158181

159182
def start_base(self, base):
183+
"""
184+
Starts a single base structure by propagating the
185+
container's logging level and logger and then calling
186+
the base's load operation. If the container is configured
187+
with `diag_owner` only the owner base will have its
188+
diagnostics enabled, all other bases have it disabled
189+
to avoid port binding conflicts.
190+
191+
:type base: Base/Agent
192+
:param base: The base structure to be started under
193+
the container.
194+
"""
195+
196+
# shares the container's logging level and logger with the base
197+
# this is required to ensure that the base is properly configured
198+
# to use the same logging configuration as the container (required for
199+
# the proper propagation of the logging configuration to the bases)
160200
base.level = self.level
161201
base.logger = self.logger
202+
203+
# if the container is configured with `diag_owner` only the
204+
# owner base will have its diagnostics enabled, all other bases
205+
# have it disabled to avoid port binding conflicts
206+
if self.diag_owner and not base == self.owner:
207+
base.diag = False
208+
162209
base.load()
163210

164211
def start_all(self):

src/netius/servers/proxy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def __init__(
184184
self.raw_client.bind("data", self._on_raw_data)
185185
self.raw_client.bind("close", self._on_raw_close)
186186

187-
self.container = netius.Container(*args, **kwargs)
187+
self.container = netius.Container(*args, diag_owner=True, **kwargs)
188188
self.container.add_base(self)
189189
self.container.add_base(self.http_client)
190190
self.container.add_base(self.raw_client)

0 commit comments

Comments
 (0)