Skip to content

Commit e8603a5

Browse files
committed
fix(discovery): enforce reader admission across workspace changes
Signed-off-by: Taylor Mutch <taylormutch@gmail.com>
1 parent d04958a commit e8603a5

4 files changed

Lines changed: 88 additions & 17 deletions

File tree

‎crates/openshell-server/src/grpc/service_discovery.rs‎

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,6 @@ mod tests {
754754
let error = watch(
755755
&state,
756756
authed_request(WatchServiceRoutesRequest {
757-
workspaces: Vec::new(),
758757
workspace_scope: request().workspace_scope,
759758
workspaces: Vec::new(),
760759
}),
@@ -998,7 +997,7 @@ mod tests {
998997
})
999998
)
1000999
.await
1001-
.is_err()
1000+
.is_ok()
10021001
);
10031002
let mut outsider = authed_request(request());
10041003
if let Some(Principal::User(user)) = outsider.extensions_mut().get_mut::<Principal>() {
@@ -1780,7 +1779,7 @@ mod tests {
17801779
.unwrap();
17811780
}
17821781
assert_eq!(
1783-
selected_snapshot(&state, &principal, &Selection::All)
1782+
selected_snapshot(&state, &principal, &Selection::All, &[])
17841783
.await
17851784
.unwrap_err()
17861785
.code(),
@@ -1790,7 +1789,8 @@ mod tests {
17901789
selected_snapshot(
17911790
&state,
17921791
&principal,
1793-
&Selection::Named(vec!["default".into()])
1792+
&Selection::Named(vec!["default".into()]),
1793+
&["default".into()]
17941794
)
17951795
.await
17961796
.is_ok()
@@ -1865,9 +1865,14 @@ mod tests {
18651865
.await
18661866
.unwrap();
18671867
let selected = Selection::Named(vec!["default".into(), "team-b".into()]);
1868-
let both = selected_snapshot(&state, &principal, &selected)
1869-
.await
1870-
.unwrap();
1868+
let both = selected_snapshot(
1869+
&state,
1870+
&principal,
1871+
&selected,
1872+
&["default".into(), "team-b".into()],
1873+
)
1874+
.await
1875+
.unwrap();
18711876
assert_eq!(
18721877
both.routes
18731878
.iter()
@@ -1877,22 +1882,81 @@ mod tests {
18771882
);
18781883
assert_eq!(
18791884
both,
1880-
selected_snapshot(&state, &principal, &Selection::All)
1881-
.await
1882-
.unwrap()
1885+
selected_snapshot(
1886+
&state,
1887+
&principal,
1888+
&Selection::All,
1889+
&["default".into(), "team-b".into()]
1890+
)
1891+
.await
1892+
.unwrap()
18831893
);
18841894
state
18851895
.store
18861896
.delete("workspace", "workspace-b")
18871897
.await
18881898
.unwrap();
18891899
assert_eq!(
1890-
selected_snapshot(&state, &principal, &Selection::All)
1900+
selected_snapshot(&state, &principal, &Selection::All, &["default".into()])
18911901
.await
18921902
.unwrap()
18931903
.routes
18941904
.len(),
18951905
1
18961906
);
18971907
}
1908+
#[tokio::test]
1909+
async fn all_workspace_watch_admits_new_workspaces_before_publishing_their_routes() {
1910+
use openshell_core::proto::{ObjectMeta, Workspace};
1911+
let (state, principal, _dir) = reader_fixture(true).await;
1912+
let mut stream = watch(
1913+
&state,
1914+
reader_request(
1915+
WatchServiceRoutesRequest {
1916+
workspace_scope: Some(openshell_core::proto::all_workspaces_selector()),
1917+
workspaces: Vec::new(),
1918+
},
1919+
&principal,
1920+
),
1921+
)
1922+
.await
1923+
.unwrap()
1924+
.into_inner();
1925+
assert!(stream.next().await.unwrap().is_ok());
1926+
let blockers = (0..WATCHES_PER_WORKSPACE)
1927+
.map(|n| {
1928+
state
1929+
.discovery
1930+
.admit(&user(&format!("blocker-{n}")), &["later".into()], true)
1931+
.unwrap()
1932+
})
1933+
.collect::<Vec<_>>();
1934+
state
1935+
.store
1936+
.put_message(&Workspace {
1937+
metadata: Some(ObjectMeta {
1938+
id: "later".into(),
1939+
name: "later".into(),
1940+
..Default::default()
1941+
}),
1942+
..Default::default()
1943+
})
1944+
.await
1945+
.unwrap();
1946+
let error = tokio::time::timeout(Duration::from_secs(3), stream.next())
1947+
.await
1948+
.unwrap()
1949+
.unwrap()
1950+
.unwrap_err();
1951+
assert_eq!(error.code(), tonic::Code::ResourceExhausted);
1952+
assert!(stream.next().await.is_none());
1953+
assert_eq!(state.discovery_watches.available_permits(), 32);
1954+
drop(blockers);
1955+
assert!(
1956+
state
1957+
.discovery
1958+
.admit(&principal, &["default".into(), "later".into()], true)
1959+
.is_ok()
1960+
);
1961+
}
18981962
}

‎crates/openshell-server/src/multiplex.rs‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,11 @@ impl MultiplexService {
338338
self.state.gateway_interceptors.clone(),
339339
Some(self.state.clone()),
340340
);
341-
let discovery = ServiceDiscoveryServer::new(OpenShellService::new(self.state.clone()))
342-
.max_decoding_message_size(MAX_GRPC_DECODE_SIZE);
343-
let discovery = GatewayInterceptorGrpcService::new(
344-
discovery,
341+
let discovery_service =
342+
ServiceDiscoveryServer::new(OpenShellService::new(self.state.clone()))
343+
.max_decoding_message_size(MAX_GRPC_DECODE_SIZE);
344+
let discovery_service = GatewayInterceptorGrpcService::new(
345+
discovery_service,
345346
self.state.gateway_interceptors.clone(),
346347
Some(self.state.clone()),
347348
);
@@ -354,7 +355,7 @@ impl MultiplexService {
354355
let grpc_service = AuthGrpcRouter::with_peer_identity(
355356
GrpcRouter::new(openshell, reflection)
356357
.with_sandbox_updates(sandbox_updates)
357-
.with_discovery(discovery),
358+
.with_discovery(discovery_service),
358359
authenticator_chain,
359360
authz_policy,
360361
self.state

‎crates/openshell-server/src/storage_proto.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ mod tests {
179179
// updates use a separate capability/service and add one classified RPC.
180180
// Discovery RPCs move to ServiceDiscovery; message and durable closures are unchanged.
181181
const PUBLIC_RPC_SCHEMA_SHA256: &str =
182-
"9570c5a65c405284cecb1d14067f91be2ce5d5dc98f2ceb0d7504ded776a863c";
182+
"1c3bf59d4cd397c2b4428bdfeff951323de10bbda6c93622af842c3d025cd9be";
183183
const DURABLE_SCHEMA_SHA256: &str =
184184
"cb15f8f79a855064a43fe8c6cd908bf07eca6604e2bf8ab7d2de0ef33e97da4e";
185185
const PUBLIC_DURABLE_OVERLAP_SHA256: &str =

‎docs/how-it-works/gateways/configuration.mdx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,3 +2087,9 @@ For rotation, add the replacement fingerprint, project the new certificate/key
20872087
to the consumer, then remove the old fingerprint. The proxy reloads mounted
20882088
identity and trust files on reconnect. No certificate issuance or renewal is
20892089
performed by the gateway.
2090+
2091+
Finite and all-workspace watches reserve capacity for every selected active
2092+
workspace. When that set changes, the watch reacquires workspace capacity before
2093+
publishing the next view. A newly selected workspace at capacity returns
2094+
`RESOURCE_EXHAUSTED`; the client can reconnect. Installation-record limits apply
2095+
per workspace; route and response-size limits also apply to the combined view.

0 commit comments

Comments
 (0)