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

Commit

Permalink
Performance optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
losfair committed Jul 26, 2017
1 parent 0d5d4b8 commit 5fb5f2c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 113 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ice_core"
version = "0.1.4"
version = "0.1.5"
authors = ["losfair <[email protected]>"]
links = "ice_glue ice_internal_prefix_tree"
build = "build.rs"
Expand Down
38 changes: 22 additions & 16 deletions src/delegates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use futures::future::{FutureResult, Future};
use futures::{Async, Poll};
use futures::sync::oneshot;
use futures::Stream;
use std::rc::Rc;
use std::cell::RefCell;

use hyper;
use hyper::server::{Request, Response};
Expand Down Expand Up @@ -45,7 +47,9 @@ pub fn fire_handlers(ctx: Arc<ice_server::Context>, req: Request) -> Box<Future<

let method = format!("{}", req.method());

logger.log(logging::Message::Info(format!("{} {} {}", remote_addr.as_str(), method.as_str(), uri.as_str())));
if(ctx.log_requests) {
logger.log(logging::Message::Info(format!("{} {} {}", remote_addr.as_str(), method.as_str(), uri.as_str())));
}

/*
target_req.set_context(Arc::into_raw(ctx.clone()));
Expand Down Expand Up @@ -140,7 +144,7 @@ pub fn fire_handlers(ctx: Arc<ice_server::Context>, req: Request) -> Box<Future<
let max_request_body_size = ctx.max_request_body_size as usize;

let (tx, rx) = oneshot::channel();
let mut body: Arc<Mutex<Vec<u8>>> = Arc::new(Mutex::new(Vec::new()));
let mut body: Rc<RefCell<Vec<u8>>> = Rc::new(RefCell::new(Vec::new()));
let mut body_cloned = body.clone();
let mut body_len = 0;

Expand All @@ -150,23 +154,25 @@ pub fn fire_handlers(ctx: Arc<ice_server::Context>, req: Request) -> Box<Future<

let start_micros = time::micros();

Box::new(req.body().for_each(move |chunk| {
let mut body = body_cloned.lock().unwrap();
let reader: Box<Future<Item = (), Error = hyper::Error>> = match read_body {
true => Box::new(req.body().for_each(move |chunk| {
let mut body = body_cloned.borrow_mut();

if body_len + chunk.len() > max_request_body_size {
body.clear();
return Err(hyper::Error::TooLarge);
}
if body_len + chunk.len() > max_request_body_size {
body.clear();
return Err(hyper::Error::TooLarge);
}

body_len += chunk.len();

if read_body {
body.extend_from_slice(&chunk);
}
body_len += chunk.len();
body.extend_from_slice(&chunk);

Ok(())
}).map_err(|e| e.description().to_string()).map(move |_| unsafe {
let body = body.lock().unwrap();
Ok(())
}).map(|_| ())),
false => Box::new(futures::future::ok(()))
};

Box::new(reader.map_err(|e| e.description().to_string()).map(move |_| unsafe {
let body = body.borrow();

let call_info = Box::into_raw(Box::new(CallInfo {
req: glue::request::Request {
Expand Down
94 changes: 0 additions & 94 deletions src/glue_old.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,13 @@ type Pointer = usize;

#[no_mangle]
extern {
fn ice_glue_create_request() -> Pointer;
fn ice_glue_destroy_request(req: Pointer);
fn ice_glue_request_set_context(req: Pointer, ctx: delegates::ContextHandle);
fn ice_glue_request_set_session(req: Pointer, ctx: delegates::SessionHandle);
fn ice_glue_request_set_remote_addr(req: Pointer, addr: *const c_char);
fn ice_glue_request_set_method(req: Pointer, m: *const c_char);
fn ice_glue_request_set_uri(req: Pointer, uri: *const c_char);
fn ice_glue_request_add_param(req: Pointer, k: *const c_char, v: *const c_char);
fn ice_glue_request_add_cookie(req: Pointer, k: *const c_char, v: *const c_char);

fn ice_glue_create_response() -> Pointer;
fn ice_glue_destroy_response(resp: Pointer);
fn ice_glue_request_set_body(t: Pointer, data: *const u8, len: u32);
fn ice_glue_request_get_body(t: Pointer, len_out: *mut u32) -> *const u8;
fn ice_glue_response_set_body(t: Pointer, data: *const u8, len: u32);
fn ice_glue_response_get_body(t: Pointer, len_out: *mut u32) -> *const u8;
fn ice_glue_response_get_file(t: Pointer) -> *const c_char;

fn ice_glue_request_get_header(t: Pointer, k: *const c_char) -> *const c_char;
fn ice_glue_request_add_header(t: Pointer, k: *const c_char, v: *const c_char);
fn ice_glue_request_create_header_iterator(t: Pointer) -> Pointer;
fn ice_glue_old_destroy_header_iterator(itr_p: Pointer);
fn ice_glue_request_header_iterator_next(t: Pointer, itr_p: Pointer) -> *const c_char;

fn ice_glue_response_get_cookie(t: Pointer, k: *const c_char) -> *const c_char;
fn ice_glue_response_create_cookie_iterator(t: Pointer) -> Pointer;
Expand All @@ -50,84 +34,6 @@ extern {
pub fn ice_glue_async_endpoint_handler(id: i32, call_info: Pointer);
}

pub struct Request {
handle: Pointer
}

impl Request {
pub fn new() -> Request {
Request {
handle: unsafe { ice_glue_create_request() }
}
}

pub unsafe fn from_raw(handle: Pointer) -> Request {
if handle == 0 {
panic!("Got a null pointer");
}
Request {
handle: handle
}
}

pub fn into_raw(mut self) -> Pointer {
let handle = self.handle;
self.handle = 0;
handle
}

pub unsafe fn get_raw(&self) -> Pointer {
self.handle
}

pub fn set_context(&mut self, ctx: delegates::ContextHandle) {
unsafe { ice_glue_request_set_context(self.handle, ctx); }
}

pub fn set_session(&mut self, sess: delegates::SessionHandle) {
unsafe { ice_glue_request_set_session(self.handle, sess); }
}

pub fn set_remote_addr(&mut self, addr: &str) {
unsafe { ice_glue_request_set_remote_addr(self.handle, CString::new(addr).unwrap().as_ptr()); }
}

pub fn set_method(&mut self, m: &str) {
unsafe { ice_glue_request_set_method(self.handle, CString::new(m).unwrap().as_ptr()); }
}

pub fn set_uri(&mut self, uri: &str) {
unsafe { ice_glue_request_set_uri(self.handle, CString::new(uri).unwrap().as_ptr()); }
}

pub fn add_header(&mut self, k: &str, v: &str) {
unsafe { ice_glue_request_add_header(self.handle, CString::new(k).unwrap().as_ptr(), CString::new(v).unwrap().as_ptr()); }
}

pub fn set_body(&mut self, data: &[u8]) {
unsafe { ice_glue_request_set_body(self.handle, data.as_ptr(), data.len() as u32); }
}

pub fn add_param(&mut self, k: &str, v: &str) {
unsafe { ice_glue_request_add_param(self.handle, CString::new(k).unwrap().as_ptr(), CString::new(v).unwrap().as_ptr()); }
}

pub fn add_cookie(&mut self, k: &str, v: &str) {
unsafe { ice_glue_request_add_cookie(self.handle, CString::new(k).unwrap().as_ptr(), CString::new(v).unwrap().as_ptr()); }
}
}

impl Drop for Request {
fn drop(&mut self) {
if self.handle == 0 {
return;
}

unsafe { ice_glue_destroy_request(self.handle); }
self.handle = 0;
}
}

pub struct Response {
handle: Pointer
}
Expand Down
13 changes: 11 additions & 2 deletions src/ice_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use delegates;
use router;
use tokio_core;
use static_file;
use logging;
use session_storage::SessionStorage;
use config;
use template::TemplateStorage;
Expand All @@ -27,7 +28,8 @@ pub struct Preparation {
pub session_cookie_name: Mutex<String>,
pub session_timeout_ms: RwLock<u64>,
pub templates: Arc<TemplateStorage>,
pub max_request_body_size: Mutex<u32>
pub max_request_body_size: Mutex<u32>,
pub log_requests: Mutex<bool>
}

pub struct Context {
Expand All @@ -40,6 +42,7 @@ pub struct Context {
pub session_storage: Arc<SessionStorage>,
pub templates: Arc<TemplateStorage>,
pub max_request_body_size: u32,
pub log_requests: bool,
pub stats: stat::ServerStats
}

Expand All @@ -56,12 +59,15 @@ impl IceServer {
session_cookie_name: Mutex::new(config::DEFAULT_SESSION_COOKIE_NAME.to_string()),
session_timeout_ms: RwLock::new(600000),
templates: Arc::new(TemplateStorage::new()),
max_request_body_size: Mutex::new(config::DEFAULT_MAX_REQUEST_BODY_SIZE)
max_request_body_size: Mutex::new(config::DEFAULT_MAX_REQUEST_BODY_SIZE),
log_requests: Mutex::new(true)
})
}
}

pub fn listen_in_this_thread(&self, addr: &str) {
let logger = logging::Logger::new("IceServer::listen_in_this_thread");

let addr = addr.parse().unwrap();

let mut ev_loop = tokio_core::reactor::Core::new().unwrap();
Expand All @@ -83,6 +89,7 @@ impl IceServer {
session_storage: session_storage.clone(),
templates: self.prep.templates.clone(),
max_request_body_size: *self.prep.max_request_body_size.lock().unwrap(),
log_requests: *self.prep.log_requests.lock().unwrap(),
stats: stat::ServerStats::new()
});

Expand All @@ -102,6 +109,8 @@ impl IceServer {
Ok(())
});

logger.log(logging::Message::Info(format!("Ice Server v{} listening at {}", env!("CARGO_PKG_VERSION"), addr)));

ev_loop.run(server).unwrap();
}

Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ pub fn ice_server_set_max_request_body_size(handle: ServerHandle, size: u32) {
Arc::into_raw(handle);
}

#[no_mangle]
pub fn ice_server_disable_request_logging(handle: ServerHandle) {
let handle = unsafe { Arc::from_raw(handle) };

{
let mut server = handle.lock().unwrap();
*server.prep.log_requests.lock().unwrap() = false;
}

Arc::into_raw(handle);
}

#[no_mangle]
pub fn ice_context_render_template(handle: ContextHandle, name: *const c_char, data: *const c_char) -> *mut c_char {
let handle = unsafe { Arc::from_raw(handle) };
Expand Down

0 comments on commit 5fb5f2c

Please sign in to comment.