Skip to content

Commit

Permalink
feat: add config entries for cache and rate limiting
Browse files Browse the repository at this point in the history
  • Loading branch information
0xf4lc0n committed Dec 29, 2022
1 parent f67d6f3 commit 42cccfc
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
23 changes: 13 additions & 10 deletions infrastructure/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,42 @@ use skytable::{

use application::prelude::Cache;

pub struct SkyTableCache<'a> {
host: &'a str,
pub struct SkyTableCache {
host: String,
port: u16,
}

impl<'a> SkyTableCache<'a> {
pub fn new(host: &'a str, port: u16) -> Self {
SkyTableCache { host, port }
impl SkyTableCache {
pub fn new(host: &str, port: u16) -> Self {
SkyTableCache {
host: host.into(),
port,
}
}
}

impl<'a> Cache for SkyTableCache<'a> {
impl Cache for SkyTableCache {
fn get<T: FromSkyhashBytes>(&self, key: &str) -> Result<T, skytable::error::Error> {
Connection::new(self.host, self.port)?.get(key)
Connection::new(&self.host, self.port)?.get(key)
}

fn set<T: IntoSkyhashBytes>(
&self,
key: &str,
value: T,
) -> Result<bool, skytable::error::Error> {
Connection::new(self.host, self.port)?.set(key, value)
Connection::new(&self.host, self.port)?.set(key, value)
}

fn update<T: IntoSkyhashBytes>(
&self,
key: &str,
value: T,
) -> Result<(), skytable::error::Error> {
Connection::new(self.host, self.port)?.update(key, value)
Connection::new(&self.host, self.port)?.update(key, value)
}

fn del(&self, key: &str) -> Result<u64, skytable::error::Error> {
Connection::new(self.host, self.port)?.del(key)
Connection::new(&self.host, self.port)?.del(key)
}
}
6 changes: 6 additions & 0 deletions server/configuration/config.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[application]
folder_to_watch = "/var/log/remote"
host = "localhost"
one_request_replenishment_time = 5
port = 8443
request_pool = 10

[certificates]
ca_cert_path = "certs/ca/rootCA.pem"
Expand All @@ -15,3 +17,7 @@ password = "postgres"
port = 5432
require_ssl = false
username = "postgres"

[cache]
host = "localhost"
port = 2003
9 changes: 9 additions & 0 deletions server/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ pub struct Settings {
pub application: ApplicationSettings,
pub database: DatabaseSettings,
pub certificates: CertificateSettings,
pub cache: CacheSettings,
}

#[derive(serde::Deserialize)]
pub struct ApplicationSettings {
pub folder_to_watch: String,
pub host: String,
pub port: u16,
pub request_pool: u32,
pub one_request_replenishment_time: u64,
}

#[derive(serde::Deserialize)]
Expand Down Expand Up @@ -78,3 +81,9 @@ pub struct CertificateSettings {
pub server_cert_path: String,
pub server_key_path: String,
}

#[derive(serde::Deserialize)]
pub struct CacheSettings {
pub host: String,
pub port: u16,
}
2 changes: 1 addition & 1 deletion server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async fn main() -> Result<()> {

let connection_pool = PgPoolOptions::new().connect_lazy_with(config.database.with_db());

let cache = SkyTableCache::new("127.0.0.1", 2003);
let cache = SkyTableCache::new(&config.cache.host, config.cache.port);
let log_repo = PgLogRepo::new(connection_pool.clone());
let file_system = Arc::new(LinuxFS::new(cache, log_repo));

Expand Down
4 changes: 2 additions & 2 deletions server/src/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub fn run(address: String, db_pool: PgPool, settings: &Settings) -> Result<Serv
let ssl_builder = setup_certificate_auth(settings)?;

let governor_conf = GovernorConfigBuilder::default()
.per_second(10)
.burst_size(10)
.per_second(settings.application.one_request_replenishment_time)
.burst_size(settings.application.request_pool)
.finish()
.unwrap();

Expand Down

0 comments on commit 42cccfc

Please sign in to comment.