Sanic: what's the unhashable url ?

Created on 21 Oct 2016  路  2Comments  路  Source: sanic-org/sanic

I've read the source code of route part and find https://github.com/channelcat/sanic/blob/master/sanic/router.py#L82 , I wonder what's the unhashable url and if you can give an example :)

Most helpful comment

Sure!

An example of unhashable route would be: /picture/<name:[A-Z/]>

jpiasetz added some code that speeds up the router by narrowing down how many routes it has to check against when attempting to match the current requests's URL. It does this by grouping routes and URLs by their values using a hashing function. Because the hashing function groups routes by how many slashes are in the URL, routes with parameters that can accept slashes in them can be unintentionally skipped.

For example, the route /picture/<name:[A-Z/]> would be in group 2 because it has 2 slashes in it (disregarding the parameter). The URL /pictures/123 would check group 2 and match the route. The URL /picture/123/456/789 also matches the route, but would be in group 4 and would not be checked.

Routes that are unhashable are always checked on every request.

All 2 comments

Sure!

An example of unhashable route would be: /picture/<name:[A-Z/]>

jpiasetz added some code that speeds up the router by narrowing down how many routes it has to check against when attempting to match the current requests's URL. It does this by grouping routes and URLs by their values using a hashing function. Because the hashing function groups routes by how many slashes are in the URL, routes with parameters that can accept slashes in them can be unintentionally skipped.

For example, the route /picture/<name:[A-Z/]> would be in group 2 because it has 2 slashes in it (disregarding the parameter). The URL /pictures/123 would check group 2 and match the route. The URL /picture/123/456/789 also matches the route, but would be in group 4 and would not be checked.

Routes that are unhashable are always checked on every request.

@channelcat Got it! thanks

Was this page helpful?
0 / 5 - 0 ratings