Skip to content

Commit a33b712

Browse files
committed
refactor(sdk): share the Python GetSandbox request for owner lookup
Reuse get's request for the owner lookup, point the routing headers at their documented contract, and accept only string owner metadata. Part of #4223 Signed-off-by: divesh <[email protected]>
1 parent e57548c commit a33b712

2 files changed

Lines changed: 13 additions & 11 deletions

File tree

‎python/openshell/sandbox.py‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
_OAUTH_MAX_RESPONSE_BYTES = 1 << 20
4141
_PAGER_MAX_CONSUMED_TOKENS = 10_000
4242
_PAGER_MAX_CONSUMED_TOKEN_BYTES = 1 << 20
43+
# Replica routing headers and retry rule: docs/kubernetes/ingress.mdx.
4344
_OWNER_REPLICA_HEADER = "x-openshell-owner"
4445
_OWNER_REQUEST_HEADER = "x-openshell-want-owner"
4546
_ROUTE_REPLICA_HEADER = "x-openshell-replica"
@@ -152,6 +153,12 @@ def _workspace_scope(workspace: str) -> datamodel_pb2.WorkspaceSelector:
152153
return datamodel_pb2.WorkspaceSelector(workspace=workspace)
153154

154155

156+
def _get_sandbox_request(name: str, workspace: str) -> openshell_pb2.GetSandboxRequest:
157+
return openshell_pb2.GetSandboxRequest(
158+
workspace_scope=_workspace_scope(workspace), name=name
159+
)
160+
161+
155162
def _all_workspaces_scope() -> datamodel_pb2.WorkspaceSelector:
156163
return datamodel_pb2.WorkspaceSelector(all_workspaces=datamodel_pb2.AllWorkspaces())
157164

@@ -1015,10 +1022,7 @@ def providers(self) -> ProviderClient:
10151022

10161023
def get(self, name: str, *, workspace: str) -> SandboxRef:
10171024
response = self._stub.GetSandbox(
1018-
openshell_pb2.GetSandboxRequest(
1019-
workspace_scope=_workspace_scope(workspace),
1020-
name=name,
1021-
),
1025+
_get_sandbox_request(name, workspace),
10221026
timeout=self._timeout,
10231027
)
10241028
return _sandbox_ref(response.sandbox)
@@ -1363,16 +1367,14 @@ def _owner_of(self, workspace: str, sandbox: str) -> str:
13631367
return owner
13641368
try:
13651369
_, call = self._stub.GetSandbox.with_call(
1366-
openshell_pb2.GetSandboxRequest(
1367-
workspace_scope=_workspace_scope(workspace),
1368-
name=sandbox,
1369-
),
1370+
_get_sandbox_request(sandbox, workspace),
13701371
timeout=self._timeout,
13711372
metadata=((_OWNER_REQUEST_HEADER, "1"),),
13721373
)
13731374
except grpc.RpcError:
13741375
return ""
1375-
owner = dict(call.initial_metadata() or ()).get(_OWNER_REPLICA_HEADER, "")
1376+
value = dict(call.initial_metadata() or ()).get(_OWNER_REPLICA_HEADER)
1377+
owner = value if isinstance(value, str) else ""
13761378
self._owners.put(workspace, sandbox, owner)
13771379
return owner
13781380

‎python/openshell/sandbox_test.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
)
5454

5555
if TYPE_CHECKING:
56-
from collections.abc import Iterator
56+
from collections.abc import Generator
5757

5858

5959
def _request_workspace(request: Any) -> str | None:
@@ -485,7 +485,7 @@ def ExecSandbox(self, request, context):
485485

486486

487487
@contextlib.contextmanager
488-
def _routing_client(gateway: _RoutingGateway) -> Iterator[SandboxClient]:
488+
def _routing_client(gateway: _RoutingGateway) -> Generator[SandboxClient]:
489489
server = grpc.server(futures.ThreadPoolExecutor(max_workers=2))
490490
openshell_pb2_grpc.add_OpenShellServicer_to_server(gateway, server)
491491
port = server.add_insecure_port("127.0.0.1:0")

0 commit comments

Comments
 (0)