Skip to content

Commit 9676535

Browse files
committed
add validation for new adaptive timing traits
1 parent 7e045b9 commit 9676535

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

jupyter_server_documents/rooms/yroom_file_api.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import logging
99
from tornado.web import HTTPError
1010
from traitlets.config import LoggingConfigurable
11-
from traitlets import Float
11+
from traitlets import Float, validate
1212

1313
if TYPE_CHECKING:
1414
from typing import Any, Coroutine, Literal
@@ -17,6 +17,8 @@
1717
from jupyter_server.services.contents.manager import ContentsManager
1818
from ..outputs.manager import OutputsManager
1919

20+
DEFAULT_MIN_POLL_INTERVAL = 0.5
21+
DEFAULT_POLL_INTERVAL_MULTIPLIER = 5.0
2022
class YRoomFileAPI(LoggingConfigurable):
2123
"""Provides an API to interact with a single file for a YRoom.
2224
@@ -46,14 +48,14 @@ class YRoomFileAPI(LoggingConfigurable):
4648
"""
4749

4850
min_poll_interval = Float(
49-
default_value=0.5,
51+
default_value=DEFAULT_MIN_POLL_INTERVAL,
5052
help="Minimum autosave interval in seconds. The adaptive timing will "
5153
"never go below this value. Defaults to 0.5 seconds.",
5254
config=True,
5355
)
5456

5557
poll_interval_multiplier = Float(
56-
default_value=5.0,
58+
default_value=DEFAULT_POLL_INTERVAL_MULTIPLIER,
5759
help="Multiplier applied to save duration to calculate the next poll "
5860
"interval. For example, if a save takes 1 second and the multiplier is "
5961
"5.0, the next poll interval will be 5 seconds (bounded by min/max). "
@@ -151,6 +153,25 @@ def __init__(self, *args, **kwargs):
151153

152154
# Initialize adaptive timing attributes
153155
self._adaptive_poll_interval = self.min_poll_interval
156+
157+
@validate("min_poll_interval", "poll_interval_multiplier")
158+
def _validate_adaptive_timing_traits(self, proposal):
159+
trait_name = proposal['trait'].name
160+
value = proposal['value']
161+
162+
if trait_name == "min_poll_interval":
163+
default_value = DEFAULT_MIN_POLL_INTERVAL
164+
else:
165+
default_value = DEFAULT_POLL_INTERVAL_MULTIPLIER
166+
167+
if value <= 0:
168+
self.log.warning(
169+
f"`YRoomFileAPI.{trait_name}` must be >0. Received: {value}. "
170+
f"Reverting to default value {default_value}."
171+
)
172+
return default_value
173+
174+
return proposal["value"]
154175

155176

156177
def get_path(self) -> str | None:

0 commit comments

Comments
 (0)