reverseproxy: Add fallback
for some policies, instead of always random
#5488
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #5486
This adds a new
fallback
option for some load balancing policies:query
,header
andcookie
. See https://caddyserver.com/docs/caddyfile/directives/reverse_proxy#load-balancingPreviously, the default was to always choose a random upstream when using those policies when there's no value available. Now, the fallback policy is configurable.
Example usecases:
cookie
+least_conn
, where the first request (with no cookie) gets the upstream with the fewest current requests, instead of a random oneheader
+first
where if a header isn't provided, the first available upstream is chosen instead; so essentially thefirst
policy allows for primary/secondary behaviour normally, but allows for an escape hatch for testing the secondary upstream by using a headerIn the Caddyfile, it looks like this:
Also, an insane case like this could be done:
There's lots of boilerplate here unfortunately, I tried to reduce it where I could. Added a new test for
cookie
+first
since it's an easy case. Now, the policies need provisioning, some default value cases were hoisted to the Provision function.I used Copilot to speed up writing this PR, it wrote a lot of the boilerplate for me. I've reviewed and tested the code it generated.