{"id":18552973,"url":"https://github.com/lunarwhite/load-balancer","last_synced_at":"2026-04-28T15:36:22.479Z","repository":{"id":93057754,"uuid":"554152403","full_name":"lunarwhite/load-balancer","owner":"lunarwhite","description":"Build a simple load balancer, test with reverse proxy.","archived":false,"fork":false,"pushed_at":"2023-08-30T14:43:50.000Z","size":31,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-01T11:03:22.435Z","etag":null,"topics":["demo","golang","load-balancer","nginx","reverse-proxy"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/lunarwhite/load-balancer","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lunarwhite.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-19T10:37:37.000Z","updated_at":"2023-03-04T05:43:33.000Z","dependencies_parsed_at":"2024-06-20T04:26:02.339Z","dependency_job_id":"7ca346ee-06d9-4d5d-9a7e-0263f09df506","html_url":"https://github.com/lunarwhite/load-balancer","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/lunarwhite/load-balancer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lunarwhite%2Fload-balancer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lunarwhite%2Fload-balancer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lunarwhite%2Fload-balancer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lunarwhite%2Fload-balancer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lunarwhite","download_url":"https://codeload.github.com/lunarwhite/load-balancer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lunarwhite%2Fload-balancer/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262950293,"owners_count":23389638,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["demo","golang","load-balancer","nginx","reverse-proxy"],"created_at":"2024-11-06T21:15:41.390Z","updated_at":"2026-04-28T15:36:12.468Z","avatar_url":"https://github.com/lunarwhite.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# load-balancer\n\n![GitHub top language](https://img.shields.io/github/languages/top/lunarwhite/load-balancer) [![Go Report Card](https://goreportcard.com/badge/github.com/lunarwhite/load-balancer)](https://goreportcard.com/report/github.com/lunarwhite/load-balancer) [![codecov](https://codecov.io/gh/lunarwhite/load-balancer/branch/master/graph/badge.svg)](https://codecov.io/gh/lunarwhite/load-balancer) [![GitHub Action](https://github.com/lunarwhite/load-balancer/actions/workflows/go.yml/badge.svg)](https://github.com/lunarwhite/load-balancer/actions/workflows/go.yml) [![Releases](https://img.shields.io/github/release/lunarwhite/load-balancer/all.svg)](https://github.com/lunarwhite/load-balancer/releases)\n\n## Introduction\n\nThis project is a simple layer 7 load balancer written in Go, supports both http and https scheme, and implements several common load balancing algorithms. Note that not stable for production use, just for personal practice.\n\n## Highlights\n\nIt currently supports `round-robin`, `random`, `consistent-hash`, `ip-hash` and `least-load` algorithms.\n\nIt will perform `health check` on all proxy sites periodically. When the site is unreachable, it will be removed from the balancer automatically . However, `load-balancer` will still perform `health check` on unreachable sites. When the site is reachable, it will add it to the balancer automatically.\n\n## Getting Started\n\n### Build and run\n\nClone the source code:\n\n```shell\ngit clone https://github.com/lunarwhite/load-balancer.git\n```\n\nYou should specific the `config.yaml` file first, example see [config.yaml](./examples/config.yaml):\n\n```yaml\nschema: http # support `http` and `https`\nport: 8089 # port for balancer\nlocation: # route matching for reverse proxy\n  - pattern: /\n    proxy_pass: # URLs of the reverse proxy\n      - \"https://192.168.1.1\"\n      - \"https://192.168.1.2\"\n      - \"https://192.168.1.3\"\n      - \"https://192.168.1.4\"\n    balance_algo: round-robin # supprt `round-robin`,`random`, `ip-hash, `consistent-hash`, `least-load`\nssl_certificate: # your ssl certificate\nssl_certificate_key: # your ssl certificate key\nmax_req_allowed: 100 # The max number of requests that the balancer can handle at the same time, 0 refers to no limit\nenable_health_check: true\nhealth_check_interval: 3 # second\n```\n\nThen, run commands to build:\n\n```shell\ncd ./load-balancer\ngo build\n```\n\nExecute `load-balancer`, it will print the configuration details:\n\n```shell\n$ ./load-balancer\n\nSchema: http\nPort: 8089\nLocation:\n        Route: /\n        Proxy Pass: [https://192.168.1.1 https://192.168.1.2 https://192.168.1.3 https://192.168.1.4]\n        Algo: round-robin\nEnable Health Check: true\n```\n\n### API Usage\n\nYou can use it as a third-party Go lib in your project.\n\n```shell\ngo get github.com/lunarwhite/load-balancer/balancer\n```\n\nBuild the load balancer with `balancer.Build`:\n\n```go\nhosts := []string{\n\t\"http://192.168.10.102\",\n\t\"http://192.168.10.103\",\n\t\"http://192.168.10.104\",\n\t\"http://192.168.10.105\",\n}\n\nlb, err := balancer.Build(balancer.LeastLoadBalancer, hosts)\nif err != nil {\n\treturn err\n}\n```\n\nThen, you can use `load-balancer` like this:\n\n```go\nclientAddr := \"172.160.10.2\"  // request IP\n\ntargetHost, err := lb.Balance(clientAddr)\nif err != nil {\n\tlog.Fatal(err)\n}\n\nlb.Inc(targetHost)\ndefer lb.Done(targetHost)\n\n// route to target host\n```\n\nEach load balancer implements the `balancer.Balancer` interface:\n\n```go\ntype Balancer interface {\n\tAdd(string)\n\tRemove(string)\n\tBalance(string) (string, error)\n\tInc(string)\n\tDone(string)\n}\n```\n\n## TODO\n\nEnhancement\n\n- [ ] Use a more robust router, such as `gorilla/mux` rather than `http.HandleFunc`.\n- [ ] Use dependency injection to make code more modular and testable.\n- [ ] Use a connection pool to manage connections to the backends.\n- [ ] Use HTTP/2 or WebSockets to reduce the overhead of creating and closing HTTP connections.\n- [ ] Support more lb algorithms.\n- [ ] Add more tests.\n\n## Contributing\n\nContributions are welcome. If you are open source newcomer, you can follow [this guide](https://opensource.guide/how-to-contribute/) by GitHub.\n\n## Report Vulnerability\n\nIf you come across a security related issue, please open an issue.\n\n## License\n\nThis project is licensed under the term of the [MIT License](https://github.com/lunarwhite/load-balancer/blob/main/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flunarwhite%2Fload-balancer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flunarwhite%2Fload-balancer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flunarwhite%2Fload-balancer/lists"}