Skip to content

TinyBalancer is a reverse proxy load balancer extended from the Go standard library's net/http/httputil. It is designed to be flexible, efficient, and easy to use, with support for both HTTP and HTTPS protocols.

Notifications You must be signed in to change notification settings

zhihali/tiny-balancer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

tiny-balancer

TinyBalancer is a reverse proxy load balancer extended from the Go standard library's net/http/httputil. It is designed to be flexible, efficient, and easy to use, with support for both HTTP and HTTPS protocols.

Features

  • Protocol Support: Works with both HTTP and HTTPS.
  • Load Balancing Algorithms: Supports a variety of load balancing algorithms, including:
    • Round-Robin
    • Random
    • Power of Two Random Choices
    • Consistent Hash
    • Consistent Hash with Bounded
    • IP Hash
    • Least Load
  • Health Checks and Fault Recovery: Implements heartbeat checks for backend servers, enabling automatic recovery from failures.

Getting Started

Prerequisites

  • Go (version 1.x or later)

Installing

To start using TinyBalancer, install Go and run go get:

go get github.com/Charlie-lizhihan/tiny-balancer

Usage

Here's a quick example of how to set up and test a load balancer:

package main

import (
	"fmt"
	"log"

	"github.com/Charlie-lizhihan/tiny-balancer/balancer"
)

func main() {
	hosts := []string{
		"http://192.168.11.101",
		"http://192.168.11.102",
		"http://192.168.11.103",
		"http://192.168.11.104",
	}

	//  the balancer support
	// 	IPHashBalancer         = "ip-hash"
	// 	ConsistentHashBalancer = "consistent-hash"
	// 	P2CBalancer            = "p2c"
	// 	RandomBalancer         = "random"
	// 	R2Balancer             = "round-robin"
	// 	LeastLoadBalancer      = "least-load"
	// 	BoundedBalancer        = "bounded"
	lb, err := balancer.Build(balancer.R2Balancer, hosts)
	if err != nil {
		log.Fatal("Error building balancer:", err)
	}

	clientAddr := "172.160.1.5" // Example client IP

	for i := 0; i <= 5; i++ {
		targetHost, err := lb.Balance(clientAddr)
		if err != nil {
			log.Fatal("Error balancing:", err)
		}

		// Simulate routing the request to 'targetHost'
		fmt.Printf("Routing request from %s to %s\n", clientAddr, targetHost)
		lb.Inc(targetHost)
		defer lb.Done(targetHost)
	}

}

About

TinyBalancer is a reverse proxy load balancer extended from the Go standard library's net/http/httputil. It is designed to be flexible, efficient, and easy to use, with support for both HTTP and HTTPS protocols.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages