optimize api state extractor
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
ccef1a4c8b
commit
1f88866612
6 changed files with 81 additions and 23 deletions
|
@ -10,8 +10,7 @@ extern crate conduit_service as service;
|
|||
pub(crate) use conduit::{debug_info, pdu::PduEvent, utils, Error, Result};
|
||||
pub(crate) use service::services;
|
||||
|
||||
pub use self::router::State;
|
||||
pub(crate) use self::router::{Ruma, RumaResponse};
|
||||
pub(crate) use self::router::{Ruma, RumaResponse, State};
|
||||
|
||||
conduit::mod_ctor! {}
|
||||
conduit::mod_dtor! {}
|
||||
|
|
|
@ -3,7 +3,7 @@ mod auth;
|
|||
mod handler;
|
||||
mod request;
|
||||
mod response;
|
||||
mod state;
|
||||
pub mod state;
|
||||
|
||||
use axum::{
|
||||
response::IntoResponse,
|
||||
|
@ -14,8 +14,7 @@ use conduit::{err, Server};
|
|||
use http::Uri;
|
||||
|
||||
use self::handler::RouterExt;
|
||||
pub use self::state::State;
|
||||
pub(super) use self::{args::Args as Ruma, response::RumaResponse};
|
||||
pub(super) use self::{args::Args as Ruma, response::RumaResponse, state::State};
|
||||
use crate::{client, server};
|
||||
|
||||
pub fn build(router: Router<State>, server: &Server) -> Router<State> {
|
||||
|
|
|
@ -2,21 +2,78 @@ use std::{ops::Deref, sync::Arc};
|
|||
|
||||
use conduit_service::Services;
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct State {
|
||||
services: *const Services,
|
||||
}
|
||||
|
||||
pub struct Guard {
|
||||
services: Arc<Services>,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub fn new(services: Arc<Services>) -> Self {
|
||||
Self {
|
||||
services,
|
||||
}
|
||||
pub fn create(services: Arc<Services>) -> (State, Guard) {
|
||||
let state = State {
|
||||
services: Arc::into_raw(services.clone()),
|
||||
};
|
||||
|
||||
let guard = Guard {
|
||||
services,
|
||||
};
|
||||
|
||||
(state, guard)
|
||||
}
|
||||
|
||||
impl Drop for Guard {
|
||||
fn drop(&mut self) {
|
||||
let ptr = Arc::as_ptr(&self.services);
|
||||
// SAFETY: Parity with Arc::into_raw() called in create(). This revivifies the
|
||||
// Arc lost to State so it can be dropped, otherwise Services will leak.
|
||||
let arc = unsafe { Arc::from_raw(ptr) };
|
||||
debug_assert!(
|
||||
Arc::strong_count(&arc) > 1,
|
||||
"Services usually has more than one reference and is not dropped here"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for State {
|
||||
type Target = Arc<Services>;
|
||||
type Target = Services;
|
||||
|
||||
fn deref(&self) -> &Self::Target { &self.services }
|
||||
fn deref(&self) -> &Self::Target {
|
||||
deref(&self.services).expect("dereferenced Services pointer in State must not be null")
|
||||
}
|
||||
}
|
||||
|
||||
/// SAFETY: State is a thin wrapper containing a raw const pointer to Services
|
||||
/// in lieu of an Arc. Services is internally threadsafe. If State contains
|
||||
/// additional fields this notice should be reevaluated.
|
||||
unsafe impl Send for State {}
|
||||
|
||||
/// SAFETY: State is a thin wrapper containing a raw const pointer to Services
|
||||
/// in lieu of an Arc. Services is internally threadsafe. If State contains
|
||||
/// additional fields this notice should be reevaluated.
|
||||
unsafe impl Sync for State {}
|
||||
|
||||
fn deref(services: &*const Services) -> Option<&Services> {
|
||||
// SAFETY: We replaced Arc<Services> with *const Services in State. This is
|
||||
// worth about 10 clones (20 reference count updates) for each request handled.
|
||||
// Though this is not an incredibly large quantity, it's woefully unnecessary
|
||||
// given the context as explained below; though it is not currently known to be
|
||||
// a performance bottleneck, the front-line position justifies preempting it.
|
||||
//
|
||||
// Services is created prior to the axum/tower stack and Router, and prior
|
||||
// to serving any requests through the handlers in this crate. It is then
|
||||
// dropped only after all requests have completed, the listening sockets
|
||||
// have been closed, axum/tower has been dropped. Thus Services is
|
||||
// expected to live at least as long as any instance of State, making the
|
||||
// constant updates to the prior Arc unnecessary to keep Services alive.
|
||||
//
|
||||
// Nevertheless if it is possible to accomplish this by annotating State
|
||||
// with a lifetime to hold a reference (and be aware I have made a
|
||||
// significant effort trying to make this work) this unsafety may not be
|
||||
// necessary. It is either very difficult or impossible to get a
|
||||
// lifetime'ed reference through Router / RumaHandler; though it is
|
||||
// possible to pass a reference through axum's `with_state()` in trivial
|
||||
// configurations as the only requirement of a State is Clone.
|
||||
unsafe { services.as_ref() }
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue