Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ For this experiment, we will use the https://api.starwars.run REST API. A simple
1. Currying
2. Tail Recursion
3. Function Compostion
4. and more!!!!
4. and more!!!!


without threads:
[2022-01-14T18:22:15Z INFO sw_scraper] START
[2022-01-14T18:22:58Z INFO sw_scraper] FINISH
43 seconds

with threads
3 changes: 3 additions & 0 deletions app_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BASE_URL = "https://api.starwars.run/api/"
OUTPUT_DIR = "./output/"
NUM_THREADS = "3"
33 changes: 29 additions & 4 deletions sw-scraper/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
[package]
build = "build.rs"
edition = "2018"
name = "sw-scraper"
version = "0.1.0"

[[bin]]
name = "sw-scraper"
path = "src/main.rs"

[profile.release]
lto = true
opt-level = 'z'

[build-dependencies]
anyhow = "1"
vergen = "6"

[dependencies]
#log = "0.4"
reqwest = {version = "0.11", features = [ "blocking", "json"]}
config = "0.11"
env_logger = "0.9"
log = "0.4"

actix = "0.12"
reqwest = {version = "0.11", features = ["blocking", "json"]}
serde = {version = "1", features = ["derive"]}
serde_json = "1.0"
serde_json = "1"
tokio = {version = "1", features = ["full"]}
url = {version = "2", features = ["serde"]}
#uuid = {version = "0.8", features = ["v4", "serde"]}

### ACTIX TEST STUFF
slog = "2"
slog-async = "2"
slog-json = "2"
slog-term = "2"

lazy_static = "1"
thread-id = "4"
7 changes: 7 additions & 0 deletions sw-scraper/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use anyhow::Result;
use vergen::{Config, vergen};

fn main() -> Result<()> {
// Generate the default 'cargo:' instruction output
vergen(Config::default())
}
3 changes: 2 additions & 1 deletion sw-scraper/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ pub enum AppErrorType {
_FetchError,
_NotFound,
_InvalidData,
WriteError,
ConfigError,
_WriteError,
}

#[derive(Debug)]
Expand Down
69 changes: 69 additions & 0 deletions sw-scraper/src/logger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use slog;
use thread_id;

use slog::*;
use std::result;

thread_local!(static TL_THREAD_ID: usize = thread_id::get() % 65536);

#[derive(Clone)]
pub struct ThreadLocalDrain<D>
where
D: Drain,
{
pub drain: D,
}

impl<D> Drain for ThreadLocalDrain<D>
where
D: Drain,
{
type Ok = ();
type Err = Never;

fn log(
&self,
record: &Record<'_>,
values: &OwnedKVList,
) -> result::Result<Self::Ok, Self::Err> {
let chained = OwnedKVList::from(OwnedKV((
SingleKV("thread", TL_THREAD_ID.with(|id| *id)),
values.clone(),
)));
let _ = self.drain.log(record, &chained);
Ok(())
}
}

pub struct FnGuard {
function_name: &'static str,
logger: Logger,
}

impl FnGuard {
// pub fn new<T>(logger: Logger, values: OwnedKV<T>, function_name: &'static str) -> FnGuard
// where
// T: SendSyncRefUnwindSafeKV + 'static,
// {
// let new_logger = logger.new(values);
// info!(new_logger, "[Enter]"; o!("function_name"=>function_name));
// FnGuard {
// function_name,
// logger: new_logger,
// }
// }

// pub fn sub_guard(&self, function_name: &'static str) -> FnGuard {
// FnGuard::new(self.logger.clone(), o!(), function_name)
// }

// pub fn log(&self, record: &Record<'_>) {
// self.logger.log(record)
// }
}

impl Drop for FnGuard {
fn drop(&mut self) {
info!(self.logger, "[Exit]"; o!("function_name"=>self.function_name))
}
}
Loading