Load Balancing

Runs on your machine

Distributes requests over workers and mirrors, with a canary deployment.

This sample runs on your machine only. It forwards requests to worker processes running on your machine.

Clone GenHTTP, let Playground/Program.cs serve LoadBalancerSample.Create() and start it with dotnet run - then try the commands below.

LoadBalancerSample.cs42 lines
using GenHTTP.Api.Content;
using GenHTTP.Api.Infrastructure;
using GenHTTP.Api.Protocol;

using GenHTTP.Modules.Layouting;
using GenHTTP.Modules.LoadBalancing;

public static class LoadBalancerSample
{

    private static readonly ByteString CanaryHeader = new("X-Canary");

    public static IHandlerBuilder Create()
    {
        /*
         *
         * Distributes API requests over multiple worker processes (expected to run
         * on http://localhost:5001 and :5002) and sends clients downloading files
         * to one of the mirror servers.
         *
         * Clients sending an "X-Canary" header are served by a canary worker
         * (on http://localhost:5003), so a new version can be tested with real traffic.
         *
         * See https://genhttp.org/documentation/content/handlers/load-balancer/
         *
         */

        var workers = LoadBalancer.Create()
                                  .Proxy("http://localhost:5001/")
                                  .Proxy("http://localhost:5002/")
                                  .Proxy("http://localhost:5003/", r => r.Header.Headers.ContainsKey(CanaryHeader) ? Priority.High : Priority.Low);

        var mirrors = LoadBalancer.Create()
                                  .Redirect("https://mirror1.example.com/")
                                  .Redirect("https://mirror2.example.com/");

        return Layout.Create()
                     .Add("api", workers) // GET http://localhost:8080/api/
                     .Add("downloads", mirrors); // GET http://localhost:8080/downloads/setup.exe
    }

}

Try it

With the sample running on your machine:

  1. Call a worker

    502, unless workers run on ports 5001 and 5002

    curl -i http://localhost:8080/api/
  2. Call the canary worker

    502, unless a worker runs on port 5003

    curl -i -H "X-Canary: 1" http://localhost:8080/api/
  3. Download from a mirror

    Redirects to one of the mirrors.

    curl -i http://localhost:8080/downloads/setup.exe