Public release release-e7c2b3ac175d
Source commit: e7c2b3ac175db426791c02779841c5df1d0ffdff Public tree identity: sha256:4f0b7cd828932c06d56f2406788f89b2050ef56c1e1a4f76f55341d387d78e46
This commit is contained in:
parent
9f43c6276a
commit
cfd4f19da2
16 changed files with 1386 additions and 313 deletions
|
|
@ -1,6 +1,7 @@
|
|||
use std::collections::{BTreeMap, VecDeque};
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
|
||||
|
|
@ -33,13 +34,20 @@ pub trait ClientTransport: Send + Sync + 'static {
|
|||
fn send(&self, request: TransportRequest) -> TransportFuture;
|
||||
}
|
||||
|
||||
type ControlSessionPool = Arc<Vec<Mutex<Option<ControlSession>>>>;
|
||||
|
||||
pub struct ControlTransport {
|
||||
endpoint: String,
|
||||
connect_timeout: Duration,
|
||||
io_timeout: Duration,
|
||||
sessions: Arc<Mutex<BTreeMap<String, ControlSession>>>,
|
||||
sessions: Arc<Mutex<BTreeMap<String, ControlSessionPool>>>,
|
||||
next_session: Arc<AtomicU64>,
|
||||
}
|
||||
|
||||
// A small pool allows independent HTMX reads to progress concurrently while
|
||||
// keeping connection and blocking-worker use strictly bounded.
|
||||
const SESSIONS_PER_API_PATH: usize = 4;
|
||||
|
||||
impl ControlTransport {
|
||||
pub fn new(endpoint: impl Into<String>) -> Result<Self, ClientTransportError> {
|
||||
Self::with_timeouts(endpoint, Duration::from_secs(10), Duration::from_secs(30))
|
||||
|
|
@ -58,6 +66,7 @@ impl ControlTransport {
|
|||
connect_timeout,
|
||||
io_timeout,
|
||||
sessions: Arc::new(Mutex::new(BTreeMap::new())),
|
||||
next_session: Arc::new(AtomicU64::new(0)),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -68,35 +77,89 @@ impl ClientTransport for ControlTransport {
|
|||
let connect_timeout = self.connect_timeout;
|
||||
let io_timeout = self.io_timeout;
|
||||
let sessions = Arc::clone(&self.sessions);
|
||||
let next_session = Arc::clone(&self.next_session);
|
||||
Box::pin(async move {
|
||||
let pool = {
|
||||
let mut sessions = sessions.lock().map_err(|_| {
|
||||
ClientTransportError::Failed(
|
||||
"client transport pool lock was poisoned".to_owned(),
|
||||
)
|
||||
})?;
|
||||
Arc::clone(sessions.entry(request.api_path.clone()).or_insert_with(|| {
|
||||
Arc::new(
|
||||
(0..SESSIONS_PER_API_PATH)
|
||||
.map(|_| Mutex::new(None))
|
||||
.collect(),
|
||||
)
|
||||
}))
|
||||
};
|
||||
let slot_index =
|
||||
next_session.fetch_add(1, Ordering::Relaxed) as usize % SESSIONS_PER_API_PATH;
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let value = serde_json::from_slice(&request.body)
|
||||
.map_err(|error| ClientTransportError::Failed(error.to_string()))?;
|
||||
let mut sessions = sessions.lock().map_err(|_| {
|
||||
ClientTransportError::Failed(
|
||||
"client transport session lock was poisoned".to_owned(),
|
||||
)
|
||||
})?;
|
||||
if !sessions.contains_key(&request.api_path) {
|
||||
let session = ControlSession::connect_to_api_path_with_timeouts(
|
||||
&endpoint,
|
||||
&request.api_path,
|
||||
connect_timeout,
|
||||
io_timeout,
|
||||
)
|
||||
.map_err(|error| ClientTransportError::Failed(error.to_string()))?;
|
||||
sessions.insert(request.api_path.clone(), session);
|
||||
let mut selected = None;
|
||||
for offset in 0..SESSIONS_PER_API_PATH {
|
||||
let index = (slot_index + offset) % SESSIONS_PER_API_PATH;
|
||||
match pool[index].try_lock() {
|
||||
Ok(session) if session.is_some() => {
|
||||
selected = Some(session);
|
||||
break;
|
||||
}
|
||||
Ok(_) | Err(std::sync::TryLockError::WouldBlock) => {}
|
||||
Err(std::sync::TryLockError::Poisoned(_)) => {
|
||||
return Err(ClientTransportError::Failed(
|
||||
"client transport session lock was poisoned".to_owned(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
let response = sessions
|
||||
.get_mut(&request.api_path)
|
||||
.expect("session was inserted for the requested API path")
|
||||
if selected.is_none() {
|
||||
for offset in 0..SESSIONS_PER_API_PATH {
|
||||
let index = (slot_index + offset) % SESSIONS_PER_API_PATH;
|
||||
match pool[index].try_lock() {
|
||||
Ok(session) => {
|
||||
selected = Some(session);
|
||||
break;
|
||||
}
|
||||
Err(std::sync::TryLockError::WouldBlock) => {}
|
||||
Err(std::sync::TryLockError::Poisoned(_)) => {
|
||||
return Err(ClientTransportError::Failed(
|
||||
"client transport session lock was poisoned".to_owned(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut session = match selected {
|
||||
Some(session) => session,
|
||||
None => pool[slot_index].lock().map_err(|_| {
|
||||
ClientTransportError::Failed(
|
||||
"client transport session lock was poisoned".to_owned(),
|
||||
)
|
||||
})?,
|
||||
};
|
||||
if session.is_none() {
|
||||
*session = Some(
|
||||
ControlSession::connect_to_api_path_with_timeouts(
|
||||
&endpoint,
|
||||
&request.api_path,
|
||||
connect_timeout,
|
||||
io_timeout,
|
||||
)
|
||||
.map_err(|error| ClientTransportError::Failed(error.to_string()))?,
|
||||
);
|
||||
}
|
||||
let response = session
|
||||
.as_mut()
|
||||
.expect("session was initialized for the requested API path")
|
||||
.request(&value);
|
||||
match response {
|
||||
Ok(response) => serde_json::to_vec(&response)
|
||||
.map(|body| TransportResponse { body })
|
||||
.map_err(|error| ClientTransportError::Failed(error.to_string())),
|
||||
Err(error) => {
|
||||
sessions.remove(&request.api_path);
|
||||
*session = None;
|
||||
Err(ClientTransportError::Failed(error.to_string()))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
use std::net::TcpListener;
|
||||
use std::time::Duration;
|
||||
|
||||
use clusterflux_client::{
|
||||
ApiErrorCategory, ApiErrorCode, ArtifactDownloadPoll, ArtifactId, ClientError,
|
||||
ClusterfluxClient, MockTransport, ProjectId, SessionCredential, TenantId, UserId,
|
||||
CLIENT_API_VERSION,
|
||||
ClusterfluxClient, ControlTransport, MockTransport, ProjectId, SessionCredential, TenantId,
|
||||
UserId, CLIENT_API_VERSION,
|
||||
};
|
||||
use clusterflux_control::CONTROL_API_PATH;
|
||||
use clusterflux_coordinator::service::CoordinatorService;
|
||||
|
|
@ -181,3 +182,47 @@ async fn typed_client_runs_against_the_real_strict_control_endpoint() {
|
|||
drop(client);
|
||||
server.join().unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn concurrent_requests_expand_the_bounded_pool_without_serializing() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let server = std::thread::spawn(move || {
|
||||
let handlers = (0..2)
|
||||
.map(|_| {
|
||||
let (stream, _) = listener.accept().unwrap();
|
||||
std::thread::spawn(move || {
|
||||
let mut service = CoordinatorService::new(7);
|
||||
service
|
||||
.issue_cli_session(
|
||||
TenantId::from("tenant-one"),
|
||||
ProjectId::from("project-one"),
|
||||
UserId::from("user-one"),
|
||||
"concurrent-session",
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
service.handle_stream(stream).unwrap();
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
for handler in handlers {
|
||||
handler.join().unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
let transport = ControlTransport::with_timeouts(
|
||||
format!("clusterflux+tcp://{address}"),
|
||||
Duration::from_secs(2),
|
||||
Duration::from_secs(5),
|
||||
)
|
||||
.unwrap();
|
||||
let client = ClusterfluxClient::with_transport(transport)
|
||||
.with_session_credential(&SessionCredential::from_secret("concurrent-session"));
|
||||
let (first, second) = tokio::join!(client.account_status(), client.account_status());
|
||||
assert!(first.unwrap().authenticated);
|
||||
assert!(second.unwrap().authenticated);
|
||||
|
||||
drop(client);
|
||||
server.join().unwrap();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue