|
40 | 40 | _OAUTH_MAX_RESPONSE_BYTES = 1 << 20 |
41 | 41 | _PAGER_MAX_CONSUMED_TOKENS = 10_000 |
42 | 42 | _PAGER_MAX_CONSUMED_TOKEN_BYTES = 1 << 20 |
| 43 | +# Replica routing headers and retry rule: docs/kubernetes/ingress.mdx. |
43 | 44 | _OWNER_REPLICA_HEADER = "x-openshell-owner" |
44 | 45 | _OWNER_REQUEST_HEADER = "x-openshell-want-owner" |
45 | 46 | _ROUTE_REPLICA_HEADER = "x-openshell-replica" |
@@ -152,6 +153,12 @@ def _workspace_scope(workspace: str) -> datamodel_pb2.WorkspaceSelector: |
152 | 153 | return datamodel_pb2.WorkspaceSelector(workspace=workspace) |
153 | 154 |
|
154 | 155 |
|
| 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 | + |
155 | 162 | def _all_workspaces_scope() -> datamodel_pb2.WorkspaceSelector: |
156 | 163 | return datamodel_pb2.WorkspaceSelector(all_workspaces=datamodel_pb2.AllWorkspaces()) |
157 | 164 |
|
@@ -1015,10 +1022,7 @@ def providers(self) -> ProviderClient: |
1015 | 1022 |
|
1016 | 1023 | def get(self, name: str, *, workspace: str) -> SandboxRef: |
1017 | 1024 | response = self._stub.GetSandbox( |
1018 | | - openshell_pb2.GetSandboxRequest( |
1019 | | - workspace_scope=_workspace_scope(workspace), |
1020 | | - name=name, |
1021 | | - ), |
| 1025 | + _get_sandbox_request(name, workspace), |
1022 | 1026 | timeout=self._timeout, |
1023 | 1027 | ) |
1024 | 1028 | return _sandbox_ref(response.sandbox) |
@@ -1363,16 +1367,14 @@ def _owner_of(self, workspace: str, sandbox: str) -> str: |
1363 | 1367 | return owner |
1364 | 1368 | try: |
1365 | 1369 | _, 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), |
1370 | 1371 | timeout=self._timeout, |
1371 | 1372 | metadata=((_OWNER_REQUEST_HEADER, "1"),), |
1372 | 1373 | ) |
1373 | 1374 | except grpc.RpcError: |
1374 | 1375 | 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 "" |
1376 | 1378 | self._owners.put(workspace, sandbox, owner) |
1377 | 1379 | return owner |
1378 | 1380 |
|
|
0 commit comments