Skip to content

Commit 8565c2c

Browse files
authored
document rule priority (#3102)
2 parents 4abfbd5 + 3febc7e commit 8565c2c

File tree

1 file changed

+22
-38
lines changed

1 file changed

+22
-38
lines changed

docs/routing.rst

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,28 @@ merged. If you want to disable ``merge_slashes`` for a :class:`Rule` or
8787
appropriately.
8888

8989

90+
Rule Priority
91+
=============
92+
93+
In general, the map matches more specific rules first. Rules are made up of
94+
static and variable parts, separated by slash ``/``. For a given segment, rules
95+
with a static part in that position take priority, and longer static values take
96+
priority over shorter. Variable parts are weighted based on the type of data
97+
they match.
98+
99+
If you're using subdomain or host matching, the domain part can use converters
100+
as well. The domain part is matched before the path parts. Like the path parts,
101+
a static domain part take priority over a variable part.
102+
103+
Rules may end up with the same priority, by having static parts with the same
104+
length, and dynamic parts with the same weight, in the same positions. In this
105+
case, sorting is stable, so rules added earlier take priority.
106+
107+
The exact way that rules are sorted internally is pretty complicated, but the
108+
result should be that you can rely on more specific rules matching before more
109+
general ones.
110+
111+
90112
Built-in Converters
91113
===================
92114

@@ -127,13 +149,6 @@ Maps, Rules and Adapters
127149
:members: empty
128150

129151

130-
Matchers
131-
========
132-
133-
.. autoclass:: StateMachineMatcher
134-
:members:
135-
136-
137152
Rule Factories
138153
==============
139154

@@ -274,34 +289,3 @@ scheme and host, ``force_external=True`` is implied.
274289
275290
url = adapter.build("comm")
276291
assert url == "ws://example.org/ws"
277-
278-
279-
State Machine Matching
280-
======================
281-
282-
The default matching algorithm uses a state machine that transitions
283-
between parts of the request path to find a match. To understand how
284-
this works consider this rule::
285-
286-
/resource/<id>
287-
288-
Firstly this rule is decomposed into two ``RulePart``. The first is a
289-
static part with a content equal to ``resource``, the second is
290-
dynamic and requires a regex match to ``[^/]+``.
291-
292-
A state machine is then created with an initial state that represents
293-
the rule's first ``/``. This initial state has a single, static
294-
transition to the next state which represents the rule's second
295-
``/``. This second state has a single dynamic transition to the final
296-
state which includes the rule.
297-
298-
To match a path the matcher starts and the initial state and follows
299-
transitions that work. Clearly a trial path of ``/resource/2`` has the
300-
parts ``""``, ``resource``, and ``2`` which match the transitions and
301-
hence a rule will match. Whereas ``/other/2`` will not match as there
302-
is no transition for the ``other`` part from the initial state.
303-
304-
The only diversion from this rule is if a ``RulePart`` is not
305-
part-isolating i.e. it will match ``/``. In this case the ``RulePart``
306-
is considered final and represents a transition that must include all
307-
the subsequent parts of the trial path.

0 commit comments

Comments
 (0)