Skip to content

Commit

Permalink
⚗️ Update experimental code
Browse files Browse the repository at this point in the history
  • Loading branch information
czy-29 committed Dec 11, 2024
1 parent 2b35bcb commit 067e896
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
8 changes: 4 additions & 4 deletions tracing-surreal/src/bin/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use surrealdb::{
opt::auth::Root,
Surreal,
};
use tokio::time::sleep;
use tokio::time::timeout;
use tracing_surreal::{stop::Stop, tmp::server::ServerBuilder};

async fn db() -> AnyRes<Surreal<Client>> {
Expand All @@ -26,12 +26,12 @@ async fn main() -> AnyRes {
let mut server = ServerBuilder::from_stop_default(&stop).start().await?;
println!("{}", server.get_local_addr());

let grace_type = tokio::select! {
_ = sleep(Duration::from_secs_f64(5.0)) => {
let grace_type = match timeout(Duration::from_secs_f64(5.0), &mut server).await {
Err(_) => {
println!("5s elapsed, initiating shutdown...");
server.graceful_shutdown().await??
}
res = server.get_routine_mut() => {
Ok(res) => {
println!("routine exited");
res??
}
Expand Down
24 changes: 12 additions & 12 deletions tracing-surreal/src/tmp/server.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::stop::Stop;
use std::{
future::IntoFuture,
future::Future,
io,
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
pin::Pin,
task::{Context, Poll},
time::Duration,
};
use surrealdb::Connection;
Expand Down Expand Up @@ -228,11 +230,14 @@ impl<C: Connection + Clone> ServerBuilder<C> {
}
}

type RoutineOutput = io::Result<GracefulType>;
type ServerOutput = Result<RoutineOutput, JoinError>;

#[derive(Debug)]
pub struct ServerHandle {
local_addr: SocketAddr,
shutdown_s: oneshot::Sender<()>,
routine: JoinHandle<io::Result<GracefulType>>,
routine: JoinHandle<RoutineOutput>,
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
Expand All @@ -246,21 +251,16 @@ impl ServerHandle {
self.local_addr
}

pub fn get_routine_mut(&mut self) -> &mut JoinHandle<io::Result<GracefulType>> {
&mut self.routine
}

pub async fn graceful_shutdown(self) -> Result<io::Result<GracefulType>, JoinError> {
pub async fn graceful_shutdown(self) -> ServerOutput {
self.shutdown_s.send(()).ok();
self.routine.await
}
}

impl IntoFuture for ServerHandle {
type Output = Result<io::Result<GracefulType>, JoinError>;
type IntoFuture = JoinHandle<io::Result<GracefulType>>;
impl Future for ServerHandle {
type Output = ServerOutput;

fn into_future(self) -> Self::IntoFuture {
self.routine
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.routine).poll(cx)
}
}

0 comments on commit 067e896

Please sign in to comment.