-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
336 additions
and
40 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use crate::{middleware::MiddlewareStack, uow::UowChannel}; | ||
|
||
#[expect(dead_code)] | ||
pub struct Kvsd { | ||
channel: UowChannel, | ||
middlewares: MiddlewareStack, | ||
} | ||
|
||
impl Kvsd { | ||
pub(crate) fn new(channel: UowChannel, middlewares: MiddlewareStack) -> Self { | ||
Self { | ||
channel, | ||
middlewares, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,12 @@ | ||
mod dispatcher; | ||
pub(crate) use dispatcher::Dispatcher; | ||
mod stack; | ||
pub(crate) use stack::MiddlewareStack; | ||
mod telemetry; | ||
|
||
use crate::uow::UnitOfWork; | ||
|
||
pub(crate) trait Middleware { | ||
type Error; | ||
async fn handle(&mut self, uow: UnitOfWork) -> Result<(), Self::Error>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use crate::{ | ||
middleware::{telemetry::Telemetry, Dispatcher, Middleware}, | ||
uow::UnitOfWork, | ||
}; | ||
|
||
pub(crate) struct MiddlewareStack { | ||
root: Telemetry<Dispatcher>, | ||
} | ||
|
||
impl MiddlewareStack { | ||
pub(crate) fn new(dispatcher: Dispatcher) -> Self { | ||
let telemetry = Telemetry::new(dispatcher); | ||
|
||
Self { root: telemetry } | ||
} | ||
|
||
#[expect(dead_code)] | ||
pub(crate) async fn handle(&mut self, uow: UnitOfWork) { | ||
self.root.handle(uow).await.unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use std::fmt; | ||
|
||
use crate::{middleware::Middleware, uow::UnitOfWork}; | ||
|
||
use synd_stdx::prelude::*; | ||
|
||
pub(crate) struct Telemetry<MW> { | ||
next: MW, | ||
} | ||
|
||
impl<MW> Telemetry<MW> { | ||
pub(super) fn new(next: MW) -> Self { | ||
Self { next } | ||
} | ||
} | ||
|
||
impl<MW> Middleware for Telemetry<MW> | ||
where | ||
MW: Middleware + Send + 'static, | ||
<MW as Middleware>::Error: fmt::Display, | ||
{ | ||
type Error = MW::Error; | ||
|
||
async fn handle(&mut self, uow: UnitOfWork) -> Result<(), Self::Error> { | ||
// TODO: emit metrics | ||
let result = self.next.handle(uow).await; | ||
match result { | ||
Ok(()) => info!("Handle uow"), | ||
// Should handle in Error mw ? | ||
Err(ref err) => error!("{err}"), | ||
} | ||
|
||
result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use synd_kvsd_protocol::Connection; | ||
|
||
use crate::{authn::principal::Principal, server::IncommingConnection, uow::UowSender}; | ||
|
||
#[expect(dead_code)] | ||
pub(super) struct Handler { | ||
pub(super) principal: Principal, | ||
pub(super) connection: IncommingConnection<Connection>, | ||
pub(super) sender: UowSender, | ||
} | ||
|
||
impl Handler { | ||
pub(super) fn new(connection: IncommingConnection<Connection>, sender: UowSender) -> Self { | ||
Self { | ||
principal: Principal::AnonymousUser, | ||
connection, | ||
sender, | ||
} | ||
} | ||
|
||
#[expect(clippy::unused_async)] | ||
pub(super) async fn handle(self) { | ||
todo!() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use std::sync::Arc; | ||
|
||
use tokio::{ | ||
net::{TcpListener, TcpStream}, | ||
sync::Semaphore, | ||
}; | ||
|
||
use crate::server::{IncommingConnection, ServerError}; | ||
|
||
pub(super) struct ConcurrencyLimited<Listener> { | ||
listener: Listener, | ||
semaphore: Arc<Semaphore>, | ||
} | ||
|
||
impl<Listener> ConcurrencyLimited<Listener> { | ||
pub(super) fn new(listener: Listener, max_connections: usize) -> Self { | ||
Self { | ||
listener, | ||
semaphore: Arc::new(Semaphore::new(max_connections)), | ||
} | ||
} | ||
} | ||
|
||
impl ConcurrencyLimited<TcpListener> { | ||
pub(super) async fn accept(&mut self) -> Result<IncommingConnection<TcpStream>, ServerError> { | ||
let permit = self.semaphore.clone().acquire_owned().await?; | ||
let (stream, peer_addr) = self.listener.accept().await.map_err(ServerError::accept)?; | ||
|
||
Ok(IncommingConnection { | ||
connection: stream, | ||
peer_addr, | ||
permit, | ||
}) | ||
} | ||
} |
Oops, something went wrong.