Skip to content
This repository has been archived by the owner on Sep 25, 2019. It is now read-only.

Commit

Permalink
simplify dns binary for easier use in npm package
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegn committed Oct 23, 2018
1 parent 1382ca3 commit d78baab
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 94 deletions.
9 changes: 0 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ cache:
directories:
- node_modules
- v8env/node_modules
# before_install:
# - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get install -y clang-3.9 libc++-dev libc++abi-dev; fi
addons:
apt:
sources:
Expand All @@ -40,13 +38,6 @@ before_install:
fi
install:
# - |
# if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
# sudo update-alternatives --install /usr/bin/cc cc /usr/bin/clang-3.9 100
# sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/clang++-3.9 100
# export CC=/usr/bin/clang-3.9
# export CXX=/usr/bin/clang++-3.9
# fi;
- |
cd third_party/flatbuffers
cmake -G "Unix Makefiles"
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ tokio-fs = "0.1"
tokio-codec = "0.1"
num_cpus = "1"
glob = "0.2"
clap = "2.32"

[workspace]
members = [
Expand Down
129 changes: 44 additions & 85 deletions src/bin/dns/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,8 @@ extern crate log;
extern crate env_logger;
extern crate fly;
extern crate libfly;
extern crate toml;

use tokio::prelude::*;
use tokio::timer::Interval;

use std::time::Duration;

use std::fs::File;

use fly::config::Config;

use fly::msg;

Expand All @@ -53,96 +45,63 @@ use fly::utils::*;

use env_logger::Env;

#[macro_use]
extern crate lazy_static;
extern crate num_cpus;
use std::sync::atomic::Ordering;

use std::collections::HashMap;
use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
use std::sync::RwLock;

lazy_static! {
static ref NCPUS: usize = num_cpus::get();
static ref REQ_PER_APP: RwLock<HashMap<String, AtomicUsize>> = RwLock::new(HashMap::new());
pub static ref RUNTIMES: RwLock<HashMap<String, Vec<Box<Runtime>>>> = RwLock::new(HashMap::new());
}
extern crate clap;

fn main() {
let env = Env::default().filter_or("LOG_LEVEL", "info");
env_logger::init_from_env(env);

let handler = DnsHandler {};
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8053);
let matches = clap::App::new("fly-dns")
.version("0.0.1-alpha")
.about("Fly DNS server")
.arg(
clap::Arg::with_name("port")
.short("p")
.long("port")
.takes_value(true),
).arg(
clap::Arg::with_name("INPUT")
.help("Sets the input file to use")
.required(true)
.index(1),
).get_matches();

let runtime = {
let rt = Runtime::new(None);
rt.eval_file(matches.value_of("INPUT").unwrap());
rt
};

let handler = DnsHandler { runtime };

let port: u16 = match matches.value_of("port") {
Some(pstr) => pstr.parse::<u16>().unwrap(),
None => 8053,
};

let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), port);
let server = ServerFuture::new(handler);

let udp_socket = UdpSocket::bind(&addr).expect(&format!("udp bind failed: {}", addr));
info!("listening for udp on {:?}", udp_socket);

info!("V8 version: {}", libfly::version());

env_logger::init_from_env(env);

let mut main_el = tokio::runtime::Runtime::new().unwrap();
let main_el = tokio::runtime::Runtime::new().unwrap();
unsafe {
EVENT_LOOP_HANDLE = Some(main_el.executor());
};

let mut file = File::open("fly.toml").unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
let conf: Config = toml::from_str(&contents).unwrap();

debug!("toml: {:?}", conf);

for (name, app) in conf.apps.unwrap().iter() {
{
let mut rts = RUNTIMES.write().unwrap();
let mut rtsv: Vec<Box<Runtime>> = vec![];
let filename = app.filename.as_str();
for _i in 0..*NCPUS {
let rt = Runtime::new(Some(name.to_string()));
rt.eval_file(filename);
rtsv.push(rt);
}
rts.insert(name.to_string(), rtsv);
REQ_PER_APP
.write()
.unwrap()
.insert(name.to_string(), ATOMIC_USIZE_INIT);
};
}

let task = Interval::new_interval(Duration::from_secs(5))
.for_each(move |_| {
match RUNTIMES.read() {
Ok(_rts) => {
// for (key, rt) in rts.iter() {
// let stats = rt.heap_statistics();
// info!(
// "[heap stats for {0}] used: {1:.2}MB | total: {2:.2}MB | alloc: {3:.2}MB | malloc: {4:.2}MB | peak malloc: {5:.2}MB",
// key,
// stats.used_heap_size as f64 / (1024_f64 * 1024_f64),
// stats.total_heap_size as f64 / (1024_f64 * 1024_f64),
// stats.externally_allocated as f64 / (1024_f64 * 1024_f64),
// stats.malloced_memory as f64 / (1024_f64 * 1024_f64),
// stats.peak_malloced_memory as f64 / (1024_f64 * 1024_f64),
// );
// }
}
Err(e) => error!("error locking runtimes: {}", e),
};
Ok(())
}).map_err(|e| panic!("interval errored; err={:?}", e));

main_el.spawn(task);
let _ = main_el.block_on_all(future::lazy(move || -> Result<(), ()> {
server.register_socket(udp_socket);
Ok(())
}));

// main_el.shutdown_on_idle();
}

pub struct DnsHandler;
pub struct DnsHandler {
runtime: Box<Runtime>,
}

impl RequestHandler for DnsHandler {
fn handle_request<'q, 'a, R: ResponseHandler + 'static>(
Expand Down Expand Up @@ -219,16 +178,16 @@ impl RequestHandler for DnsHandler {
},
);

let guard = RUNTIMES.read().unwrap();
let rtsv = guard.values().next().unwrap();
// let guard = RUNTIMES.read().unwrap();
// let rtsv = guard.values().next().unwrap();

let idx = {
let map = REQ_PER_APP.read().unwrap();
let counter = map.values().next().unwrap();
counter.fetch_add(1, Ordering::Relaxed) % rtsv.len()
};
// let idx = {
// let map = REQ_PER_APP.read().unwrap();
// let counter = map.values().next().unwrap();
// counter.fetch_add(1, Ordering::Relaxed) % rtsv.len()
// };

let rt = &rtsv[idx];
let rt = &self.runtime;
let rtptr = rt.ptr;

let to_send = fly_buf_from(
Expand Down

0 comments on commit d78baab

Please sign in to comment.