-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rest): switch to trie based routing
- validate and normalize paths for openapi - add benchmark for routing - make the router pluggable - simplify app routing to plain functions - optimize simple route mapping and use regexp as default - add `rest.router` binding to allow customization - add docs for routing requests
- Loading branch information
1 parent
2be9923
commit a682ce2
Showing
27 changed files
with
1,483 additions
and
88 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# REST routing benchmark | ||
|
||
This directory contains a simple benchmarking to measure the performance of two | ||
router implementations for REST APIs. See | ||
https://loopback.io/doc/en/lb4/Routing-requests.html for more information. | ||
|
||
- [TrieRouter](https://github.com/strongloop/loopback-next/tree/master/packages/rest/src/router/trie-router.ts) | ||
- [RegExpRouter](https://github.com/strongloop/loopback-next/tree/master/packages/rest/src/router/regexp-router.ts) | ||
|
||
## Basic use | ||
|
||
```sh | ||
npm run -s benchmark:routing // default to 1000 routes | ||
npm run -s benchmark:routing -- <number-of-routes> | ||
``` | ||
|
||
## Base lines | ||
|
||
``` | ||
name duration count found missed | ||
TrieRouter 0,1453883 10 8 2 | ||
RegExpRouter 0,1220030 10 8 2 | ||
name duration count found missed | ||
TrieRouter 0,2109957 40 35 5 | ||
RegExpRouter 0,8762936 40 35 5 | ||
name duration count found missed | ||
TrieRouter 0,4895252 160 140 20 | ||
RegExpRouter 0,52156699 160 140 20 | ||
name duration count found missed | ||
TrieRouter 0,16065852 640 560 80 | ||
RegExpRouter 0,304921026 640 560 80 | ||
name duration count found missed | ||
TrieRouter 0,60165877 2560 2240 320 | ||
RegExpRouter 4,592089555 2560 2240 320 | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright IBM Corp. 2017,2018. All Rights Reserved. | ||
// Node module: @loopback/rest | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {anOpenApiSpec} from '@loopback/openapi-spec-builder'; | ||
import { | ||
RoutingTable, | ||
TrieRouter, | ||
RestRouter, | ||
RegExpRouter, | ||
OpenApiSpec, | ||
} from '@loopback/rest'; | ||
|
||
function runBenchmark(count = 1000) { | ||
const spec = givenNumberOfRoutes('/hello', count); | ||
|
||
const trieTest = givenRouter(new TrieRouter(), spec, count); | ||
const regexpTest = givenRouter(new RegExpRouter(), spec, count); | ||
|
||
const result1 = trieTest(); | ||
const result2 = regexpTest(); | ||
|
||
console.log( | ||
'%s %s %s %s %s', | ||
'name'.padEnd(12), | ||
'duration'.padStart(16), | ||
'count'.padStart(8), | ||
'found'.padStart(8), | ||
'missed'.padStart(8), | ||
); | ||
for (const r of [result1, result2]) { | ||
console.log( | ||
'%s %s %s %s %s', | ||
`${r.name}`.padEnd(12), | ||
`${r.duration}`.padStart(16), | ||
`${r.count}`.padStart(8), | ||
`${r.found}`.padStart(8), | ||
`${r.missed}`.padStart(8), | ||
); | ||
} | ||
} | ||
|
||
function givenNumberOfRoutes(base: string, num: number) { | ||
const spec = anOpenApiSpec(); | ||
let i = 0; | ||
while (i < num) { | ||
// Add 1/4 paths with vars | ||
if (i % 4 === 0) { | ||
spec.withOperationReturningString( | ||
'get', | ||
`${base}/group${i}/{version}`, | ||
`greet${i}`, | ||
); | ||
} else { | ||
spec.withOperationReturningString( | ||
'get', | ||
`${base}/group${i}/version_${i}`, | ||
`greet${i}`, | ||
); | ||
} | ||
i++; | ||
} | ||
const result = spec.build(); | ||
result.basePath = '/my'; | ||
return result; | ||
} | ||
|
||
function givenRouter(router: RestRouter, spec: OpenApiSpec, count: number) { | ||
const name = router.constructor.name; | ||
class TestController {} | ||
|
||
return (log?: (...args: unknown[]) => void) => { | ||
log = log || (() => {}); | ||
log('Creating %s, %d', name, count); | ||
let start = process.hrtime(); | ||
|
||
const table = new RoutingTable(router); | ||
table.registerController(spec, TestController); | ||
router.list(); // Force sorting | ||
log('Created %s %s', name, process.hrtime(start)); | ||
|
||
log('Starting %s %d', name, count); | ||
let found = 0, | ||
missed = 0; | ||
start = process.hrtime(); | ||
for (let i = 0; i < count; i++) { | ||
let group = `group${i}`; | ||
if (i % 8 === 0) { | ||
// Make it not found | ||
group = 'groupX'; | ||
} | ||
// tslint:disable-next-line:no-any | ||
const request: any = { | ||
method: 'get', | ||
path: `/my/hello/${group}/version_${i}`, | ||
}; | ||
|
||
try { | ||
table.find(request); | ||
found++; | ||
} catch (e) { | ||
missed++; | ||
} | ||
} | ||
log('Done %s', name); | ||
return {name, duration: process.hrtime(start), count, found, missed}; | ||
}; | ||
} | ||
|
||
runBenchmark(+process.argv[2] || 1000); |
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
--- | ||
lang: en | ||
title: 'Routing requests' | ||
keywords: LoopBack 4.0, LoopBack 4 | ||
sidebar: lb4_sidebar | ||
permalink: /doc/en/lb4/Routing-requests.html | ||
--- | ||
|
||
## Routing Requests | ||
|
||
This is an action in the default HTTP sequence. Its responsibility is to find a | ||
route that can handle a given http request. By default, the `FindRoute` action | ||
uses the `RoutingTable` from `@loopback/rest` to match requests against | ||
registered routes including controller methods using `request.method` and | ||
`request.path`. For example: | ||
|
||
- GET /orders => OrderController.getOrders (`@get('/orders')`) | ||
- GET /orders/123 => OrderController.getOrderById (`@get('/orders/{id}')`) | ||
- GET /orders/count => OrderController.getOrderCount (`@get('/orders/count')`) | ||
- POST /orders => OrderController.createOrder (`@post('/orders')`) | ||
|
||
## Customize the `FindRoute` action | ||
|
||
The `FindRoute` action is bound to `SequenceActions.FIND_ROUTE` | ||
('rest.sequence.actions.findRoute') and injected into the default sequence. | ||
|
||
To create your own `FindRoute` action, bind your implementation as follows: | ||
|
||
```ts | ||
const yourFindRoute: FindRoute = ...; | ||
app.bind(SequenceActions.FIND_ROUTE).to(yourFindRoute); | ||
``` | ||
|
||
## Customize the REST Router | ||
|
||
Instead of rewriting `FindRoute` action completely, LoopBack 4 also allows you | ||
to simply replace the `RestRouter` implementation. | ||
|
||
The `@loopback/rest` module ships two built-in routers: | ||
|
||
- TrieRouter: it keeps routes as a `trie` tree and uses traversal to match | ||
`request` to routes based on the hierarchy of the path | ||
- RegExpRouter: it keeps routes as an array and uses `path-to-regexp` to match | ||
`request` to routes based on the path pattern | ||
|
||
For both routers, routes without variables are optimized in a map so that any | ||
requests matching to a fixed path can be resolved quickly. | ||
|
||
By default, `@loopback/rest` uses `TrieRouter` as it performs better than | ||
`RegExpRouter`. There is a simple benchmarking for `RegExpRouter` and | ||
`TrieRouter` at | ||
https://githhub.com/strongloop/loopback-next/benchmark/src/rest-routing/routing-table.ts. | ||
|
||
To change the router for REST routing, we can bind the router class as follows: | ||
|
||
```ts | ||
import {RestBindings, RegExpRouter} from '@loopback/rest'; | ||
app.bind(RestBindings.ROUTER).toClass(RegExpRouter); | ||
``` | ||
|
||
It's also possible to have your own implementation of `RestRouter` interface | ||
below: | ||
|
||
```ts | ||
/** | ||
* Interface for router implementation | ||
*/ | ||
export interface RestRouter { | ||
/** | ||
* Add a route to the router | ||
* @param route A route entry | ||
*/ | ||
add(route: RouteEntry): boolean; | ||
|
||
/** | ||
* Find a matching route for the given http request | ||
* @param request Http request | ||
* @returns The resolved route, if not found, `undefined` is returned | ||
*/ | ||
find(request: Request): ResolvedRoute | undefined; | ||
|
||
/** | ||
* List all routes | ||
*/ | ||
list(): RouteEntry[]; | ||
} | ||
``` | ||
|
||
See examples at: | ||
|
||
- [TrieRouter](https://github.com/strongloop/loopback-next/tree/master/packages/rest/src/router/trie-router.ts) | ||
- [RegExpRouter](https://github.com/strongloop/loopback-next/tree/master/packages/rest/src/router/regexp-router.ts) |
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
Oops, something went wrong.