Load Balancer

Allows to distribute incoming requests to multiple nodes, either by using a reverse proxy or by redirecting the client.

Kotlin
val workers = LoadBalancer.create()
    .proxy("http://worker-1")
    .proxy("http://worker-2")

val cdn = LoadBalancer.create()
    .redirect("https://cdn1.domain.com")
    .redirect("https://cdn2.domain.com")

val app = Layout.create()
    .add("api", workers)
    .add("content", cdn)

If needed, the incoming request can get analyzed to determine the nodes with the highest priority that should handle the request (e.g. to prefer nodes near to the requesting client). Among the nodes sharing the highest priority, one is picked randomly; nodes without an evaluation have a medium priority.

Kotlin
val workers = LoadBalancer.create()
    .proxy("http://worker-1") { request -> Priority.High } // analyze the request here
    .proxy("http://worker-2") // Priority.Medium

Additionally, the builder accepts any HandlerBuilder as a node, allowing to extend the functionality where needed. For example, the following snippet would distribute the load to two different, local drives.

Kotlin
val files = LoadBalancer.create()
    .add(Assets.from(ResourceTree.fromDirectory("/mnt/storage1/files/")))
    .add(Assets.from(ResourceTree.fromDirectory("/mnt/storage2/files/")))