workflow
Runs different plugins for different matched requests.
workflowPriority 1006What it does
workflow holds a list of Rules on a route. Each rule pairs Conditions on the request with an action under Actions. return answers the request with a status of your choice, and limit-count and limit-conn limit it with those plugins' own settings.
The rules are read from the top, and only the first one whose conditions pass acts on a request. A condition on the caller's consumer group needs an authentication plugin on the same route, key-auth for example, which names the consumer first.
How a rule is chosen
Each request is held against the rules in the order they are listed.
- The first rule whose Conditions pass takes the request, and the rules below it are never read.
- A rule with no conditions at all takes every request that reaches it.
- The rule that takes the request runs its action, and only the first pair under Actions is ever run.
- Conditions are read like a route's conditions.
When to use it
- Customers on a blocked plan are refused with
403, while customers on the trial plan get 100 requests a minute, each plan being a consumer group. - One path of the orders API closed with a status of your choice for some callers, without a route of its own.
- A limit on concurrent connections that applies only to the requests a condition picks out, with
limit-conn.
Not for refusing or delaying requests on purpose to see how clients cope — see fault-injection (Injects delays or fixed responses for testing) or one limit on every request of a route — see limit-count (Limits the number of requests in a time window, per key).
Set it up
Refuse requests from a blocked plan and rate-limit the trial plan
Plans are consumer groups; needs an authentication plugin on the same route, which is what tells the gateway which consumer, and so which group, is calling.
Fill Rules as in the JSON below.
{
"rules": [
{
"case": [
[
"consumer_group_id",
"==",
"blocked"
]
],
"actions": [
[
"return",
{
"code": 403
}
]
]
},
{
"case": [
[
"consumer_group_id",
"==",
"trial"
]
],
"actions": [
[
"limit-count",
{
"count": 100,
"time_window": 60,
"key": "consumer_name",
"rejected_code": 429
}
]
]
}
]
}
Things to know
- Rules — A condition whose operator is unknown is stored without a word and then passes every request, so the rule's action runs on all of them. A pattern that does not compile is stored too, but then matches nothing.
- Conditions — With key-auth on the same route, a condition on
consumer_group_idreads the group of the consumer whose key the request carries: a key of a consumer inblockedis answered403. - Actions — With
limit-count, the rule counts like that plugin: a key of a consumer intrialis answered200carryingX-Ratelimit-Limitwith100. - Actions — The action is
return,limit-countorlimit-conn. Any other name is refused when you save. - Actions — A
returnwithout a status code is refused when you save. - Actions — A rule without an action is refused when you save.
- Rules — A request that no rule takes goes on as if the plugin were absent: a consumer in neither group reaches the backend, which receives its name in
X-Consumer-Username. - AdvicePut a rule with no conditions last, and give each plan a consumer group of its own, so that one condition on
consumer_group_idcovers every customer of the plan.
Settings
The fields as the console shows them, section by section.
rulesarray of objectsRequiredThe first rule whose conditions pass takes the request and the rules below it are never read.
rules[].casearrayRead like a route's conditions. A rule with no conditions at all takes every request.
rules[].actionsarrayRequiredOne pair of a name and its settings, such as ["return", {"code": 403}]; limit-count and limit-conn take their own plugins' settings. Only the first pair of a rule is ever run.
[["return",{"code":403}]]