Skip to content

Commit a424cdc

Browse files
authored
feat: Use time.monotonic to avoid endless loop when using time machine (#330)
Use time.monotonic to avoid endless loop when using time machine Signed-off-by: christian.lutnik <christian.lutnik@dynatrace.com>
1 parent 9ad25a4 commit a424cdc

3 files changed

Lines changed: 5 additions & 11 deletions

File tree

providers/openfeature-provider-flagd/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ classifiers = [
1818
keywords = []
1919
dependencies = [
2020
"openfeature-sdk>=0.8.2",
21-
"grpcio>=1.68.1",
21+
"grpcio>=1.76.0",
2222
"protobuf>=6.30.0,<7.0.0",
2323
"mmh3>=5.0.0,<6.0.0",
2424
"panzi-json-logic>=1.0.1",

providers/openfeature-provider-flagd/src/openfeature/contrib/provider/flagd/resolvers/grpc.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ def __init__(
7272
self.thread: typing.Optional[threading.Thread] = None
7373
self.timer: typing.Optional[threading.Timer] = None
7474

75-
self.start_time = time.time()
76-
7775
def _generate_channel(self, config: Config) -> grpc.Channel:
7876
target = f"{config.host}:{config.port}"
7977
# Create the channel with the service config
@@ -163,8 +161,8 @@ def connect(self) -> None:
163161
)
164162
self.monitor_thread.start()
165163
## block until ready or deadline reached
166-
timeout = self.deadline + time.time()
167-
while not self.connected and time.time() < timeout:
164+
timeout = self.deadline + time.monotonic()
165+
while not self.connected and time.monotonic() < timeout:
168166
time.sleep(0.05)
169167
logger.debug("Finished blocking gRPC state initialization")
170168

@@ -201,7 +199,6 @@ def _state_change_callback(self, new_state: ChannelConnectivity) -> None:
201199
message="gRPC sync disconnected, reconnecting",
202200
)
203201
)
204-
self.start_time = time.time()
205202
# adding a timer, so we can emit the error event after time
206203
self.timer = threading.Timer(self.retry_grace_period, self.emit_error)
207204

providers/openfeature-provider-flagd/src/openfeature/contrib/provider/flagd/resolvers/process/connector/grpc_watcher.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ def __init__(
5353
self.thread: typing.Optional[threading.Thread] = None
5454
self.timer: typing.Optional[threading.Timer] = None
5555

56-
self.start_time = time.time()
57-
5856
def _generate_channel(self, config: Config) -> grpc.Channel:
5957
target = f"{config.host}:{config.port}"
6058
# Create the channel with the service config
@@ -144,8 +142,8 @@ def connect(self) -> None:
144142
)
145143
self.monitor_thread.start()
146144
## block until ready or deadline reached
147-
timeout = self.deadline + time.time()
148-
while not self.connected and time.time() < timeout:
145+
timeout = self.deadline + time.monotonic()
146+
while not self.connected and time.monotonic() < timeout:
149147
time.sleep(0.05)
150148
logger.debug("Finished blocking gRPC state initialization")
151149

@@ -182,7 +180,6 @@ def _state_change_callback(self, new_state: grpc.ChannelConnectivity) -> None:
182180
message="gRPC sync disconnected, reconnecting",
183181
)
184182
)
185-
self.start_time = time.time()
186183
# adding a timer, so we can emit the error event after time
187184
self.timer = threading.Timer(self.retry_grace_period, self.emit_error)
188185

0 commit comments

Comments
 (0)