Round robin load balancer
Round robin is a style of load balancing where instead of a server being chosen randomly, the load balancer iterates through the servers based on a predetermined order. In weighted round robin load balancing, the number of consecutive uses of the same server will be based on the weight each server has. In this case, weight is a number you choose that increases the percentage of requests that will go to an indivudal server. Servers with higher weights tend to be more powerful servers with higher CPU counts and RAM capacity.
You can think of weight like a counter. As we receive requests, we send them to one server while keeping count. Once the count is greater than the weight set for that server, we switch to the other one, and restart our count. Again once we have counted more than the number assigned to the second server, we will switch to another (or in our case back to our original server), and begin counting again.
In this section, we're going to be implementing a relatively simple weighted round robin. But first, why would we want round robin, and in particular why a weighted round robin?
If our two servers were to be relatively similar to each other, it shouldn't matter which one a request is sent to, so round robin would be a "fair" load balancer, evenly distributing load to each server. The addition of weighting could help us in cases where our servers aren't well-matched, sending an appropriate ratio of requests to each server.
Regardless, it is a relatively common load balancing strategy, so we're going to add it as an option for our canary deployment server.
Because we now have two methods of load balancing, let's first create a
type for our load balancing style, as well as constants for those two possibilities. In the
Here we've set up our two types of
LBStyle, using Go's
iota keyword to give our two types values. Iota works by starting with the value
0 and increasing by 1 for every new line that isn't a comment or all empty space. It combines well with
consts implicit repitition, where any line without the
= expression will repeat the most recent line that included it. So here our
RandomPerRequest is set to
RoundRobin is implicitly set to
1. Both of them have also have the
LBStyle type, instead of directly being an