Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reverseproxy: Add fallback for some policies, instead of always random #5488

Merged
merged 1 commit into from May 5, 2023

Conversation

francislavoie
Copy link
Member

@francislavoie francislavoie commented Apr 12, 2023

Closes #5486

This adds a new fallback option for some load balancing policies: query, header and cookie. See https://caddyserver.com/docs/caddyfile/directives/reverse_proxy#load-balancing

Previously, 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 one
  • header + first where if a header isn't provided, the first available upstream is chosen instead; so essentially the first policy allows for primary/secondary behaviour normally, but allows for an escape hatch for testing the secondary upstream by using a header

In the Caddyfile, it looks like this:

reverse_proxy :8001 :8002 :8003 {
	lb_policy cookie LB-Cookie-Name SuperSecretHmacKey {
		fallback least_conn
	}
}

Also, an insane case like this could be done:

reverse_proxy :8001 :8002 :8003 {
	lb_policy cookie LB-Cookie-Name SuperSecretHmacKey {
		fallback cookie A-Different-Cookie {
			fallback cookie Another-Different-Cookie {
				fallback cookie Yet-Another-Different-Cookie
			}
		}
	}
}

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.

@francislavoie francislavoie added the feature ⚙️ New feature or request label Apr 12, 2023
@francislavoie francislavoie added this to the v2.7.0 milestone Apr 12, 2023
Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, I think this makes sense. Thank you!

PS. And thanks for the AI disclosure 💯

@mholt mholt merged commit 48598e1 into master May 5, 2023
23 checks passed
@mholt mholt deleted the lb-policy-fallback branch May 5, 2023 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature ⚙️ New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Question about load balancing
2 participants