2023-04-24 20:37:35 +00:00
|
|
|
use crate::{frame::Frame, Connection, Shutdown};
|
2023-04-23 21:28:45 +00:00
|
|
|
|
2023-04-24 20:37:35 +00:00
|
|
|
use std::collections::BTreeMap;
|
2023-04-23 21:28:45 +00:00
|
|
|
use std::future::Future;
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
use tokio::net::{TcpListener, TcpStream};
|
|
|
|
|
use tokio::sync::{broadcast, mpsc, Semaphore};
|
|
|
|
|
use tokio::time::{self, Duration};
|
|
|
|
|
use tracing::{debug, error, info};
|
|
|
|
|
|
|
|
|
|
struct Listener {
|
|
|
|
|
listener: TcpListener,
|
|
|
|
|
limit_connections: Arc<Semaphore>,
|
|
|
|
|
notify_shutdown: broadcast::Sender<()>,
|
|
|
|
|
shutdown_complete_rx: mpsc::Receiver<()>,
|
|
|
|
|
shutdown_complete_tx: mpsc::Sender<()>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Handler {
|
|
|
|
|
connection: Connection,
|
|
|
|
|
shutdown: Shutdown,
|
2023-04-24 20:37:35 +00:00
|
|
|
local_db: BTreeMap<Timestamp, Price>,
|
2023-04-23 21:28:45 +00:00
|
|
|
_shutdown_complete: mpsc::Sender<()>,
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-24 20:37:35 +00:00
|
|
|
type Timestamp = i32;
|
|
|
|
|
type Price = i32;
|
|
|
|
|
|
2023-04-23 21:28:45 +00:00
|
|
|
const MAX_CONNECTIONS: usize = 5;
|
|
|
|
|
|
|
|
|
|
pub async fn run(listener: TcpListener, shutdown: impl Future) -> crate::Result<()> {
|
|
|
|
|
let (notify_shutdown, _) = broadcast::channel(1);
|
|
|
|
|
let (shutdown_complete_tx, shutdown_complete_rx) = mpsc::channel(1);
|
|
|
|
|
|
|
|
|
|
let mut server = Listener {
|
|
|
|
|
listener,
|
|
|
|
|
limit_connections: Arc::new(Semaphore::new(MAX_CONNECTIONS)),
|
|
|
|
|
notify_shutdown,
|
|
|
|
|
shutdown_complete_tx,
|
|
|
|
|
shutdown_complete_rx,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
tokio::select! {
|
|
|
|
|
res = server.run() => {
|
|
|
|
|
if let Err(err) = res {
|
|
|
|
|
error!(cause = %err, "failed to accept");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ = shutdown => {
|
|
|
|
|
info!("shutting down");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let Listener {
|
|
|
|
|
mut shutdown_complete_rx,
|
|
|
|
|
shutdown_complete_tx,
|
|
|
|
|
notify_shutdown,
|
|
|
|
|
..
|
|
|
|
|
} = server;
|
|
|
|
|
|
|
|
|
|
drop(notify_shutdown);
|
|
|
|
|
drop(shutdown_complete_tx);
|
|
|
|
|
|
|
|
|
|
let _ = shutdown_complete_rx.recv().await;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Listener {
|
|
|
|
|
async fn run(&mut self) -> crate::Result<()> {
|
|
|
|
|
info!("accepting inbound connections");
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
let permit = self
|
|
|
|
|
.limit_connections
|
|
|
|
|
.clone()
|
|
|
|
|
.acquire_owned()
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let socket = self.accept().await?;
|
|
|
|
|
|
|
|
|
|
let mut handler = Handler {
|
|
|
|
|
connection: Connection::new(socket),
|
|
|
|
|
shutdown: Shutdown::new(self.notify_shutdown.subscribe()),
|
2023-04-24 20:37:35 +00:00
|
|
|
local_db: BTreeMap::new(),
|
2023-04-23 21:28:45 +00:00
|
|
|
_shutdown_complete: self.shutdown_complete_tx.clone(),
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-24 20:37:35 +00:00
|
|
|
info!("Created new handler");
|
|
|
|
|
|
2023-04-23 21:28:45 +00:00
|
|
|
tokio::spawn(async move {
|
|
|
|
|
if let Err(err) = handler.run().await {
|
|
|
|
|
error!(cause = ?err, "connection error");
|
|
|
|
|
}
|
|
|
|
|
drop(permit);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn accept(&mut self) -> crate::Result<TcpStream> {
|
|
|
|
|
let mut backoff = 1;
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
match self.listener.accept().await {
|
|
|
|
|
Ok((socket, _)) => return Ok(socket),
|
|
|
|
|
Err(err) => {
|
|
|
|
|
if backoff > 64 {
|
|
|
|
|
return Err(err.into());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
time::sleep(Duration::from_secs(backoff)).await;
|
|
|
|
|
|
|
|
|
|
backoff *= 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Handler {
|
|
|
|
|
async fn run(&mut self) -> crate::Result<()> {
|
|
|
|
|
while !self.shutdown.is_shutdown() {
|
|
|
|
|
let maybe_frame = tokio::select! {
|
|
|
|
|
res = self.connection.read_frame() => res?,
|
|
|
|
|
_ = self.shutdown.recv() => {
|
2023-04-24 20:37:35 +00:00
|
|
|
debug!("Shutdown");
|
2023-04-23 21:28:45 +00:00
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
debug!(?maybe_frame);
|
|
|
|
|
|
|
|
|
|
let frame = match maybe_frame {
|
|
|
|
|
Some(frame) => frame,
|
|
|
|
|
None => return Ok(()),
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-24 20:37:35 +00:00
|
|
|
match frame {
|
|
|
|
|
Frame::Insert { timestamp, price } => {
|
|
|
|
|
self.local_db.insert(timestamp, price);
|
|
|
|
|
}
|
|
|
|
|
Frame::Query { mintime, maxtime } => {
|
|
|
|
|
debug!(?mintime, ?maxtime);
|
|
|
|
|
|
|
|
|
|
if mintime <= maxtime {
|
|
|
|
|
let mut count = 0;
|
|
|
|
|
let mut sum = 0i64;
|
|
|
|
|
|
|
|
|
|
for (_, price) in self.local_db.range(mintime..=maxtime) {
|
|
|
|
|
sum += *price as i64;
|
|
|
|
|
count += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mean = if count > 0 { sum / count } else { 0 };
|
|
|
|
|
debug!(?mean);
|
|
|
|
|
self.connection.write_frame(&Frame::Response(mean)).await?;
|
2023-04-24 21:58:30 +00:00
|
|
|
} else {
|
|
|
|
|
self.connection.write_frame(&Frame::Response(0)).await?;
|
2023-04-24 20:37:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => unimplemented!(),
|
|
|
|
|
}
|
2023-04-23 21:28:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|