use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use serde::{Serialize, de::DeserializeOwned};
use serde_json::Value;
use tokio::sync::{Semaphore, mpsc, oneshot, watch};
use super::HeldConnection;
use super::lifecycle::{WorkerStatus, wait_for_shutdown};
use super::protocol::Command;
use crate::reentry::is_polling;
use crate::{Call, CallOutput, Error, Limits, Request, Response, Result};
#[cfg(any(feature = "boa", feature = "quickjs", feature = "v8"))]
use {
super::{PoolState, WorkerOptions, runtime},
crate::AppBuilder,
crate::reentry::new_key,
};
struct Shared {
sender: mpsc::Sender<Command>,
shutdown: Arc<watch::Sender<bool>>,
status: watch::Receiver<WorkerStatus>,
limits: Limits,
key: u64,
held: Arc<Semaphore>,
max_held: usize,
refused_connections: AtomicU64,
#[cfg(any(feature = "boa", feature = "quickjs", feature = "v8"))]
pool: Arc<runtime::Pool>,
}
#[derive(Clone)]
pub struct AppHandle {
shared: Arc<Shared>,
}
impl AppHandle {
#[cfg(any(feature = "boa", feature = "quickjs", feature = "v8"))]
pub(crate) async fn spawn(
builder: AppBuilder,
options: WorkerOptions,
limits: Limits,
) -> Result<Self> {
let (sender, receiver) = mpsc::channel(options.queue_capacity.get());
let shutdown = Arc::new(watch::channel(false).0);
let (report, status) = watch::channel(WorkerStatus::Running);
let key = new_key();
let max_held = options.instances.max_held_connections;
let held = Arc::new(Semaphore::new(max_held));
let builder = builder.keyed(key);
let pool = match runtime::Pool::start(builder, options, receiver, shutdown.clone(), report)
.await
{
Ok(pool) => pool,
Err(Error::WorkerClosed) => {
wait_for_shutdown(status).await?;
return Err(Error::WorkerClosed);
}
Err(error) => return Err(error),
};
Ok(Self {
shared: Arc::new(Shared {
sender,
shutdown,
status,
limits,
key,
held,
max_held,
refused_connections: AtomicU64::new(0),
pool,
}),
})
}
pub fn limits(&self) -> &Limits {
&self.shared.limits
}
pub fn reserve_connection(&self) -> Option<HeldConnection> {
let place = HeldConnection::try_reserve(&self.shared.held);
if place.is_none() {
self.shared
.refused_connections
.fetch_add(1, Ordering::Relaxed);
}
place
}
#[cfg(any(feature = "boa", feature = "quickjs", feature = "v8"))]
pub fn pool_state(&self) -> PoolState {
let shared = &self.shared;
PoolState {
queued: shared.sender.max_capacity() - shared.sender.capacity(),
held_connections: shared.max_held - shared.held.available_permits(),
refused_connections: shared.refused_connections.load(Ordering::Relaxed),
..shared.pool.state()
}
}
pub fn max_held_connections(&self) -> usize {
self.shared.max_held
}
pub async fn call_json(&self, export: &str, arguments: &[Value]) -> Result<Value> {
let call = Call::with_arguments(export.to_owned(), arguments.to_vec());
Ok(self.call_with(call).await?.into_value())
}
pub async fn call_with(&self, call: Call) -> Result<CallOutput> {
let (reply, receive) = oneshot::channel();
self.enqueue(Command::Call { call, reply }).await?;
self.receive(receive).await
}
pub async fn call<I: Serialize + Sync + ?Sized, O: DeserializeOwned>(
&self,
export: &str,
input: &I,
) -> Result<O> {
let input = serde_json::to_value(input)
.map_err(|error| Error::InvalidRequest(error.to_string()))?;
let call = Call::with_arguments(export.to_owned(), vec![input]);
self.call_with(call).await?.deserialize()
}
pub async fn fetch(&self, request: Request) -> Result<Response> {
self.fetch_inner(request, None).await
}
pub async fn fetch_with_context<C: Serialize + Sync + ?Sized>(
&self,
request: Request,
context: &C,
) -> Result<Response> {
let context = serde_json::to_value(context)
.map_err(|error| Error::InvalidRequest(error.to_string()))?;
self.fetch_inner(request, Some(context)).await
}
pub async fn shutdown(&self) -> Result<()> {
self.shared.shutdown.send_replace(true);
wait_for_shutdown(self.shared.status.clone()).await
}
async fn fetch_inner(&self, request: Request, context: Option<Value>) -> Result<Response> {
let (reply, receive) = oneshot::channel();
self.enqueue(Command::Fetch {
request: Box::new(request),
context,
reply,
})
.await?;
self.receive(receive).await
}
async fn enqueue(&self, command: Command) -> Result<()> {
if is_polling(self.shared.key) {
return Err(Error::Reentrant);
}
let mut stopping = self.shared.shutdown.subscribe();
if *stopping.borrow() {
return Err(Error::WorkerClosed);
}
let permit = tokio::select! {
permit = self.shared.sender.reserve() => permit.map_err(|_| Error::WorkerClosed)?,
_ = stopping.wait_for(|requested| *requested) => return Err(Error::WorkerClosed),
};
if *stopping.borrow() {
return Err(Error::WorkerClosed);
}
permit.send(command);
#[cfg(any(feature = "boa", feature = "quickjs", feature = "v8"))]
{
let sender = &self.shared.sender;
self.shared
.pool
.grow(sender.max_capacity() - sender.capacity());
}
Ok(())
}
async fn receive<T>(&self, reply: oneshot::Receiver<Result<T>>) -> Result<T> {
match reply.await {
Ok(result) => result,
Err(_) => {
wait_for_shutdown(self.shared.status.clone()).await?;
Err(Error::WorkerClosed)
}
}
}
}