-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Event timers and supporting utilities (+ lint and fmt) #31
Open
f5yacobucci
wants to merge
17
commits into
nginx:master
Choose a base branch
from
f5yacobucci:ava-subreq-depth
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+435
−54
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
856f2ba
open up the subrequest flags so that others may be used
avahahn 3f5ef75
add a utility to return subrequest depth
avahahn f028d36
utility for getting response status
avahahn 48920cb
subrequest_depth should be subrequests_available to match underlying API
avahahn a3112a1
fix subrequests_available bug
avahahn 68393d1
dont mess up the subrequest
avahahn 431fde3
add getters/setters for status line
avahahn ee1335c
reveal access to output status_line
avahahn da973dc
need to accept any data
avahahn c3ebad1
this is way better than individual getters/setters
avahahn 250d5ce
add increment count, remove unsafe access to internals
avahahn ba676db
add event timer functions to wrapper
avahahn 06d58c6
add event bindings
avahahn 1214a90
extend interface for event binding
avahahn e2c8b50
add Into impelementation for convenience
avahahn ff337b9
implement post_to_queue
avahahn 053d88a
Code review notes
f5yacobucci File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
use crate::ffi::*; | ||
use crate::ngx_log_debug_mask; | ||
|
||
/// Wrapper struct for an `ngx_event_t` pointer, provides an interface into timer methods. | ||
#[repr(transparent)] | ||
pub struct Event(pub ngx_event_t); | ||
|
||
impl Event { | ||
#[inline] | ||
fn ident(&self) -> i32 { | ||
let conn = self.0.data as *const ngx_connection_t; | ||
unsafe { (*conn).fd } | ||
} | ||
|
||
/// Adds a timer to this event. Argument `timer` is in milliseconds. | ||
pub fn add_timer(&mut self, timer_msec: ngx_msec_t) { | ||
let key: ngx_msec_int_t = unsafe { ngx_current_msec as isize + timer_msec as isize }; | ||
if self.0.timer_set() != 0 { | ||
/* FROM NGX: | ||
* Use a previous timer value if difference between it and a new | ||
* value is less than NGX_TIMER_LAZY_DELAY milliseconds: this allows | ||
* to minimize the rbtree operations for fast connections. | ||
*/ | ||
let diff = key - self.0.timer.key as ngx_msec_int_t; | ||
if diff.abs() < NGX_TIMER_LAZY_DELAY as isize { | ||
ngx_log_debug_mask!( | ||
NGX_LOG_DEBUG_EVENT, | ||
self.0.log, | ||
"event time: {}, old: {:?}, new: {:?}", | ||
self.ident(), | ||
self.0.timer.key, | ||
key | ||
); | ||
return; | ||
} | ||
|
||
self.del_timer(); | ||
} | ||
|
||
self.0.timer.key = key as ngx_msec_t; | ||
ngx_log_debug_mask!( | ||
NGX_LOG_DEBUG_EVENT, | ||
self.0.log, | ||
"event time: {}, old: {:?}, new: {:?}", | ||
self.ident(), | ||
self.0.timer.key, | ||
key | ||
); | ||
unsafe { | ||
ngx_rbtree_insert(&mut ngx_event_timer_rbtree as *mut _, &mut self.0.timer as *mut _); | ||
} | ||
|
||
self.0.set_timer_set(1); | ||
} | ||
|
||
/// Deletes an associated timer from this event. | ||
pub fn del_timer(&mut self) { | ||
ngx_log_debug_mask!( | ||
NGX_LOG_DEBUG_EVENT, | ||
self.0.log, | ||
"event timer del: {}:{:?}", | ||
self.ident(), | ||
self.0.timer.key | ||
); | ||
unsafe { | ||
ngx_rbtree_delete(&mut ngx_event_timer_rbtree as *mut _, &mut self.0.timer as *mut _); | ||
} | ||
|
||
self.0.set_timer_set(0); | ||
} | ||
|
||
/// Add event to processing queue. Translated from ngx_post_event macro. | ||
/// | ||
/// # Safety | ||
/// This function is marked unsafe because it dereferences a raw pointer. The pointer (queue) | ||
/// MUST NOT be null to satisfy its contract, will panic with null input. | ||
/// | ||
/// # Panics | ||
/// Panics if the given queue is null. | ||
pub unsafe fn post_to_queue(&mut self, queue: *mut ngx_queue_t) { | ||
assert!(!queue.is_null(), "queue is empty"); | ||
if self.0.posted() == 0 { | ||
self.0.set_posted(1); | ||
// translated from ngx_queue_insert_tail macro | ||
self.0.queue.prev = (*queue).prev; | ||
(*self.0.queue.prev).next = &self.0.queue as *const _ as *mut _; | ||
self.0.queue.next = queue; | ||
(*queue).prev = &self.0.queue as *const _ as *mut _; | ||
} | ||
} | ||
|
||
/// new_for_request creates an new Event (ngx_event_t) from the Request pool. | ||
/// | ||
/// # Safety | ||
/// This function is marked as unsafe because it involves dereferencing a raw pointer memory | ||
/// allocation from the underlying Nginx pool allocator. | ||
/// | ||
/// # Returns | ||
/// An `Option<&mut Event>` representing the result of the allocation. `Some(&mut Event)` | ||
/// indicates successful allocation, while `None` indicates a null Event. | ||
pub unsafe fn new_for_request(req: &mut crate::http::Request) -> Option<&mut Event> { | ||
Some(&mut *(req.pool().alloc(std::mem::size_of::<ngx_event_t>()) as *mut Event)) | ||
} | ||
} | ||
|
||
impl From<*mut ngx_event_t> for &mut Event { | ||
fn from(evt: *mut ngx_event_t) -> Self { | ||
unsafe { &mut *evt.cast::<Event>() } | ||
} | ||
} | ||
|
||
impl From<&mut Event> for *mut ngx_event_t { | ||
fn from(val: &mut Event) -> Self { | ||
&mut val.0 as *mut ngx_event_t | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is that? when unsafe is used in the body of the fn, it is
safe
to invoke it, otherwise, the caller must useunsafe
tounsafe { str_to_uchar(...) }
. it is definitely might break many existing codes, let's keep it back.what's the reason?