-
Notifications
You must be signed in to change notification settings - Fork 153
Add v1/orders POST endpoint to get orders in batches #4048
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
Draft
m-sz
wants to merge
18
commits into
main
Choose a base branch
from
get-orders-by-uid
base: main
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.
Draft
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c5928c0
Add v1/orders POST endpoint to get orders in batches
m-sz e5d0199
Merge branch 'main' into get-orders-by-uid
m-sz b768ac8
fmt
m-sz f897dcd
clippy
m-sz 319f5d1
Merge branch 'main' into get-orders-by-uid
m-sz f4be44b
Merge branch 'main' into get-orders-by-uid
m-sz 0c7b052
fmt
m-sz 6af9b50
Merge branch 'main' into get-orders-by-uid
m-sz 01eccfe
Change the bulk endpoint to /v1/orders/lookup to avoid collision
m-sz 9b5d618
Fix unit tests
m-sz 35c2ce7
Add negative test case
m-sz f26a539
Merge branch 'main' into get-orders-by-uid
m-sz 508aa5b
Use bulk query for many orders
m-sz cff1a94
Merge branch 'main' into get-orders-by-uid
m-sz 17a26c3
Clippy fix
m-sz 3dbd193
fmt and clippy
m-sz 7c11725
Merge branch 'main' into get-orders-by-uid
m-sz 5de3e9c
fmt
m-sz 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,108 @@ | ||
| use { | ||
| crate::{ | ||
| api::{error, extract_payload}, | ||
| orderbook::Orderbook, | ||
| }, | ||
| anyhow::Result, | ||
| model::order::{Order, OrderUid}, | ||
| std::{convert::Infallible, sync::Arc}, | ||
| warp::{Filter, Rejection, hyper::StatusCode, reply}, | ||
| }; | ||
|
|
||
| const MAX_ORDERS_LIMIT: usize = 5000; | ||
|
|
||
| #[derive(Debug, Eq, PartialEq)] | ||
| enum ValidationError { | ||
| TooManyOrders(usize), | ||
| } | ||
|
|
||
| fn validate(uids: Vec<OrderUid>) -> Result<Vec<OrderUid>, ValidationError> { | ||
| if uids.len() > MAX_ORDERS_LIMIT { | ||
| return Err(ValidationError::TooManyOrders(uids.len())); | ||
| } | ||
| Ok(uids) | ||
| } | ||
|
|
||
| fn get_orders_by_uid_request() | ||
| -> impl Filter<Extract = (Result<Vec<OrderUid>, ValidationError>,), Error = Rejection> + Clone { | ||
| warp::path!("v1" / "orders" / "lookup") | ||
| .and(warp::post()) | ||
| .and(extract_payload()) | ||
| .map(|uids: Vec<OrderUid>| validate(uids)) | ||
| } | ||
|
|
||
| pub fn get_orders_by_uid_response(result: Result<Vec<Order>>) -> super::ApiReply { | ||
| let orders = match result { | ||
| Ok(orders) => orders, | ||
| Err(err) => { | ||
| tracing::error!(?err, "get_orders_by_uids_response"); | ||
| return crate::api::internal_error_reply(); | ||
| } | ||
| }; | ||
| reply::with_status(reply::json(&orders), StatusCode::OK) | ||
| } | ||
|
|
||
| pub fn get_orders_by_uid( | ||
| orderbook: Arc<Orderbook>, | ||
| ) -> impl Filter<Extract = (super::ApiReply,), Error = Rejection> + Clone { | ||
| get_orders_by_uid_request().and_then( | ||
| move |request_result: Result<Vec<OrderUid>, ValidationError>| { | ||
| let orderbook = orderbook.clone(); | ||
| async move { | ||
| Result::<_, Infallible>::Ok(match request_result { | ||
| Ok(uids) => { | ||
| let result = orderbook.get_orders(&uids).await; | ||
| get_orders_by_uid_response(result) | ||
| } | ||
| Err(ValidationError::TooManyOrders(requested)) => { | ||
| let err = error( | ||
| "TooManyOrders", | ||
| format!( | ||
| "Too many order UIDs requested: {requested}. Maximum allowed: \ | ||
| {MAX_ORDERS_LIMIT}" | ||
| ), | ||
| ); | ||
| reply::with_status(err, StatusCode::BAD_REQUEST) | ||
| } | ||
| }) | ||
| } | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use {super::*, warp::test::request}; | ||
|
|
||
| #[tokio::test] | ||
| async fn get_orders_by_uid_request_ok() { | ||
| let uid = OrderUid::default(); | ||
| let request = request() | ||
| .path("/v1/orders/lookup") | ||
| .method("POST") | ||
| .header("content-type", "application-json") | ||
| .json(&[uid]); | ||
|
|
||
| let filter = get_orders_by_uid_request(); | ||
| let result = request.filter(&filter).await.unwrap().unwrap(); | ||
| assert_eq!(result, [uid]); | ||
| } | ||
m-sz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #[tokio::test] | ||
| async fn get_orders_by_uid_request_too_many_orders() { | ||
| let mut uids = Vec::new(); | ||
| for _ in 0..(MAX_ORDERS_LIMIT + 1) { | ||
| uids.push(OrderUid::default()); | ||
| } | ||
| let request = request() | ||
| .path("/v1/orders/lookup") | ||
| .method("POST") | ||
| .header("content-type", "application-json") | ||
| .json(&uids); | ||
|
|
||
| let filter = get_orders_by_uid_request(); | ||
| let result = request.filter(&filter).await; | ||
| // Assert that the error is a rejection. | ||
| assert!(result.is_err()); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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.
This will accumulate all the orders in memory, which is probably not a good idea. Can we instead return a stream of data directly from the DB?
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.
The stream is problematic to implement since an order can be either a regular one or a Jit order. It is doable though although I am not sure if this its this PR.
Additionally, if keeping the X amount of orders is too much, we can lower the limit I've beset (5K), to a more acceptable level.
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.
Could you elaborate?
sqlx::fetchalready returns a stream, no? Then,axum::Body::from_stream()should send a stream of data back to the client.