Skip to content
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

fix: factorizing code #322

Merged
merged 3 commits into from
Dec 4, 2022
Merged

Conversation

AntoineRR
Copy link
Collaborator

Description

This PR is a big refactor of Robyn's core. It aims at simplifying the code by factorizing it and introducing new useful structs. Here is a description of what I've done to achieve this:

  • Create a Request struct that wraps the parameters of a request (queries, headers, method, params, and body)
  • Use the Request struct in request handlers and executors instead of using each parameters of a request separately
  • Create a FunctionInfo struct to describe functions coming from Python. This struct is made available in the Python code too and wraps handler, is_async and number_of_params
  • The FunctionInfo struct replace handler, is_async and number_of_params in every function call
  • The execute_function function that was used by the const router has been removed as it was very similar to execute_http_function. Const router now uses execute_http_function (not sure about this one, let me know if I should restore execute_function which used tokio::task::spawn_blocking while execute_http_function doesn't)

This a big PR and it affects a lot of modules. It may need some time to be fully reviewed, do not hesitate to ask me questions about it and ask me to restore some parts if I made mistakes :)

@netlify
Copy link

netlify bot commented Nov 27, 2022

Deploy Preview for robyn canceled.

Name Link
🔨 Latest commit 3337230
🔍 Latest deploy log https://app.netlify.com/sites/robyn/deploys/638bcf5458da560008206121

@sansyrox
Copy link
Member

Hey @AntoineRR 👋

Thanks for your PR. I didn't have much time to review your PR over the past few days. I will try reviewing it tomorrow.

Apologies for the delay. 😄

@AntoineRR
Copy link
Collaborator Author

No worries, take as much time as you need ^^

server.add_route(
route_type, endpoint, handler, is_async, number_of_params, const
)
route_type, endpoint, function, const = route
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are refactoring this code, can we please rename the const variable to is_const or is_const_function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, I changed it to is_const where it is relevant.

Comment on lines +74 to +75
route_type, endpoint, function = route
server.add_middleware_route(route_type, endpoint, function)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I love how the routes and the functions are kept segregated ✨

Comment on lines +30 to +36
pub struct Request {
pub queries: HashMap<String, String>,
pub headers: HashMap<String, String>,
pub method: Method,
pub params: HashMap<String, String>,
pub body: Bytes,
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AntoineRR what method does it give to the request object on Request:default()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From actix_web:

impl Default for Method {
    #[inline]
    fn default() -> Method {
        Method::GET
    }
}

The default method is a GET then. It shouldn't matter for the const router, I just wanted a request to use the execute_http_function method, but it shouldn't be used in the case of the const router

@@ -44,7 +42,7 @@ impl Router<String, Method> for ConstRouter {
event_loop.context("Event loop must be provided to add a route to the const router")?;

pyo3_asyncio::tokio::run_until_complete(event_loop, async move {
let output = execute_function(function, number_of_params, is_async)
let output = execute_http_function(&Request::default(), function)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Thanks!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're welcome ;)
I think it makes sense to only have one method to execute the function, as we will only have to change this one for an impact on HttpRouter and ConstRouter at the same time!

@sansyrox
Copy link
Member

sansyrox commented Dec 3, 2022

All looks good from the first glance. I have one last question - is execute_function used anywhere now?

I will just try a final run and test before approving 😄

Great work 👍 ✨

@AntoineRR
Copy link
Collaborator Author

I'm glad you like it!
To answer your question, no, execute_function was only used in the const router. As I replaced this call with a call to execute_http_function, I removed the function completely.
This PR removes many lines of code so another test is certainly not too much ^^ I'll be here for any other adjustements, let me know if you need me :)

Copy link
Member

@sansyrox sansyrox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 👍 🚀

Great work!

@sansyrox sansyrox merged commit 383ffc3 into sparckles:main Dec 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants