-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Work on selectors based of feedback from pingora team
- Loading branch information
1 parent
fd63fcb
commit 713dbdb
Showing
5 changed files
with
134 additions
and
25 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,13 +1,48 @@ | ||
use std::io::Write; | ||
|
||
use pingora_proxy::Session; | ||
|
||
use super::RiverContext; | ||
|
||
pub type RequestSelector = for<'a> fn(&'a mut [u8], &mut RiverContext, &mut Session) -> &'a [u8]; | ||
/// A function used to determine the "key" to use for the selection process. | ||
/// | ||
/// The function may choose an existing series of bytes, or may format into | ||
/// the RiverContext.selector_buf field, using `write!` or similar formatting | ||
/// options. | ||
/// | ||
/// TODO: Should I just do `Cow<'a, [u8]>` instead of providing a buffer? The intent is | ||
/// to avoid allocations on every select (reusing and growing one instead), but this might | ||
/// have "weird" mem-leaky characteristics | ||
pub type RequestSelector = for<'a> fn(&'a mut RiverContext, &'a mut Session) -> &'a [u8]; | ||
|
||
pub fn null_selector<'a>( | ||
_buf: &'a mut [u8], | ||
_ctxt: &mut RiverContext, | ||
_ses: &mut Session, | ||
) -> &'a [u8] { | ||
/// Null selector, useful when using "Random" or "RoundRobin" selection and this key is not used | ||
/// | ||
/// Performs no formatting | ||
pub fn null_selector<'a>(_ctxt: &'a mut RiverContext, _ses: &'a mut Session) -> &'a [u8] { | ||
&[] | ||
} | ||
|
||
/// Basic selector that looks at ONLY the URI of the request as the input key | ||
/// | ||
/// Peforms no formatting | ||
pub fn uri_path_selector<'a>(_ctxt: &'a mut RiverContext, ses: &'a mut Session) -> &'a [u8] { | ||
ses.req_header().uri.path().as_bytes() | ||
} | ||
|
||
/// Selector that uses the source address (if available) and the URI of the request as the input key | ||
/// | ||
/// Performs formatting into the selector buf | ||
pub fn source_addr_and_uri_path_selector<'a>( | ||
ctxt: &'a mut RiverContext, | ||
ses: &'a mut Session, | ||
) -> &'a [u8] { | ||
write!( | ||
&mut ctxt.selector_buf, | ||
"{:?}:{}", | ||
ses.as_downstream().client_addr(), | ||
ses.req_header().uri.path(), | ||
) | ||
.expect("Formatting into a Vec<u8> should never fail"); | ||
|
||
ctxt.selector_buf.as_slice() | ||
} |