Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit 596e54e

Browse files
committed
improved from_context
1 parent e94e143 commit 596e54e

3 files changed

Lines changed: 19 additions & 7 deletions

File tree

google/cloud/bigtable/data/_async/metrics_interceptor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def _with_active_operation(func):
4545

4646
@wraps(func)
4747
def wrapper(self, continuation, client_call_details, request):
48-
operation: "ActiveOperationMetric" | None = ActiveOperationMetric.from_context()
48+
operation: ActiveOperationMetric | None = ActiveOperationMetric.from_context()
4949

5050
if operation:
5151
# start a new attempt if not started

google/cloud/bigtable/data/_metrics/data_model.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,25 @@ class ActiveOperationMetric:
178178

179179
@classmethod
180180
def from_context(cls) -> ActiveOperationMetric | None:
181-
"""
182-
Reads the active operation saved to contextvars
181+
"""Retrieves the active operation from the current execution context.
182+
183+
Because execution within a context is sequential, this guarantees
184+
retrieval of the single, unique operation, isolated from other
185+
concurrent RPCs.
186+
187+
Note:
188+
This is intended to be called by gRPC interceptors at the start
189+
of an RPC.
183190
184-
This is meant to be called by grpc interceptor to grab a reference to
185-
the active attempt reference at the start of an rpc
191+
Returns:
192+
ActiveOperationMetric: The current active operation.
193+
None: If no operation is set, or if the current operation is
194+
already in the `COMPLETED` state.
186195
"""
187-
return cls._active_operation_context.get(None)
196+
op = cls._active_operation_context.get(None)
197+
if op and op.state == OperationState.COMPLETED:
198+
return None
199+
return op
188200

189201
@property
190202
def state(self) -> OperationState:

google/cloud/bigtable/data/_sync_autogen/metrics_interceptor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def _with_active_operation(func):
3232

3333
@wraps(func)
3434
def wrapper(self, continuation, client_call_details, request):
35-
operation: "ActiveOperationMetric" | None = ActiveOperationMetric.from_context()
35+
operation: ActiveOperationMetric | None = ActiveOperationMetric.from_context()
3636
if operation:
3737
if (
3838
operation.state == OperationState.CREATED

0 commit comments

Comments
 (0)