Giter Club home page Giter Club logo

Comments (9)

Srlion avatar Srlion commented on July 22, 2024 1

This sums it up! Thanks for the clarification, and thank you for this great framework!

from xitca-web.

fakeshadow avatar fakeshadow commented on July 22, 2024

Thanks for the issue. This is a limitation and bug in the router where it will lose context of router path generating once it's nested. I would look into it.

from xitca-web.

fakeshadow avatar fakeshadow commented on July 22, 2024

With #957 this issue is resolved.

from xitca-web.

fakeshadow avatar fakeshadow commented on July 22, 2024

That said do note that xitca routing nested router in linear manner. which means for the example use in this issue there will be three routers generated:

  1. /api/*
  2. /api/v2/*
  3. /api/v2/create

A request path must be matched against every one in top down order to find the according route service. Apparently this is less efficient than using single router.

This is for simple router design by keeping it consistent with matchit. If more efficient nesting routers is desired I would suggest opening issue to said crate for a native support. I'm also open to idea of using other router crates that can achieve the same goal.

from xitca-web.

Srlion avatar Srlion commented on July 22, 2024

Do you mean that you have to do 3 match_it.at calls to reach it?

from xitca-web.

fakeshadow avatar fakeshadow commented on July 22, 2024

Do you mean that you have to do 3 match_it.at calls to reach it?

yes that's correct. I would close this issue for now as functionally the multiple nesting has been resolved.

from xitca-web.

Srlion avatar Srlion commented on July 22, 2024

@fakeshadow any reason on why not make it simple by collecting added routes and adding it directly to the main matchit router? I was playing with making a http framework on top of hyper and had these two functions that collect all routes to main router, here is what I mean:

#[inline]
pub fn generate(self) -> Result<MatchRouter<F>, matchit::InsertError> {
	let mut match_router = MatchRouter::new();

	let handlers: Vec<(Option<Method>, Handler<F>)> = self.handlers;
	match_router.insert(&self.path, handlers)?;

	Self::child_routers(self.routers, &mut match_router)?;
	Ok(match_router)
}

#[inline]
fn child_routers(
	routers: Vec<Router<F>>,
	match_router: &mut MatchRouter<F>,
) -> Result<(), matchit::InsertError> {
	for router in routers {
		let handlers: Vec<(Option<Method>, Handler<F>)> = router.handlers;

		match_router.insert(router.path, boxed_router)?;
		Self::child_routers(router.routers, match_router)?;
	}
	Ok(())
}

I believe this significantly simplifies the process and aligns with the intended use of matchit.

from xitca-web.

fakeshadow avatar fakeshadow commented on July 22, 2024

Because xitca is a composable framework where a route can be one of many possible types. For example:

App::new()
    .at("/1", fn_service(..))
    .at("/2", get(fn_service(..)))
    .at("/3", Redirect::see_other("/1"))
    .at("/4", Router::new())
    .at("/5", Router::new().ecnlosed(Middleware))

Everyone of them would produce different types of route and the only way to store them in a single router is to cast them into trait objects where the types are erased so there is no way to know what route is a nest router.

It's possible to add new trait bound for additional logic to handle routes differently based on their types which is what xitca is already doing where PathGen and RouteGen traits are used. That being said this approach also coming with one down side.
below is a router with middleware:

let mut router = Router::new().ecnlosed(Middleware));

In order to transform it to a router you have to do this:

router = router.drain().map(|(k, v)| (k, v.enclosed(Middleware)).collect();

Which means you have to construct a separate middleware for every route in the router instead of only construct one around the router itself. This can result in exponential memory bloating when multiple routes are stored in a router with multiple middlewares around the router. And one of the most important design choice of xitca is to be memory efficient when possible which means we always choose some extra cpu cycle compared to more allocated memory.

from xitca-web.

fakeshadow avatar fakeshadow commented on July 22, 2024

A new release has been made to address the issue. And the release has also "fixed" the performance issue of multi layer of nesting.
With the new release multi layer of nesting routers will only pay for extra request path string slicing(to avoid repeatedly walk the whole string for matching route) and boxed object of indirection pointer look up.

from xitca-web.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.