{"id":13481418,"url":"https://github.com/hedzr/lb","last_synced_at":"2026-03-09T17:12:19.517Z","repository":{"id":57597097,"uuid":"377125480","full_name":"hedzr/lb","owner":"hedzr","description":"concurrent load balancers, go lib,","archived":false,"fork":false,"pushed_at":"2023-06-07T09:40:55.000Z","size":114,"stargazers_count":13,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T22:35:15.918Z","etag":null,"topics":["consistent-hash-algorithm","consistent-hashing","load-balancer","load-balancing","load-balancing-algorithm","round-robin","weighted-random","weighted-round-robin"],"latest_commit_sha":null,"homepage":"https://hedzr.com/golang/algorithm/go-load-balancer-1/","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/hedzr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"custom":"https://paypal.me/hezr/3"}},"created_at":"2021-06-15T10:36:03.000Z","updated_at":"2024-03-13T08:51:09.000Z","dependencies_parsed_at":"2024-02-16T04:44:52.903Z","dependency_job_id":null,"html_url":"https://github.com/hedzr/lb","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hedzr%2Flb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hedzr%2Flb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hedzr%2Flb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hedzr%2Flb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hedzr","download_url":"https://codeload.github.com/hedzr/lb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248509206,"owners_count":21115963,"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":["consistent-hash-algorithm","consistent-hashing","load-balancer","load-balancing","load-balancing-algorithm","round-robin","weighted-random","weighted-round-robin"],"created_at":"2024-07-31T17:00:51.649Z","updated_at":"2026-03-09T17:12:19.475Z","avatar_url":"https://github.com/hedzr.png","language":"Go","readme":"# go-lb\n\n![Go](https://github.com/hedzr/lb/workflows/Go/badge.svg)\n[![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/hedzr/lb.svg?label=release)](https://github.com/hedzr/lb/releases)\n[![Go Dev](https://img.shields.io/badge/go-dev-green)](https://pkg.go.dev/github.com/hedzr/lb)\n[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/hedzr/lb) \u003c!-- [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fhedzr%2Flb.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fhedzr%2Flb?ref=badge_shield) \n--\u003e [![Go Report Card](https://goreportcard.com/badge/github.com/hedzr/lb)](https://goreportcard.com/report/github.com/hedzr/lb)\n[![Coverage Status](https://coveralls.io/repos/github/hedzr/lb/badge.svg?branch=master\u0026.9)](https://coveralls.io/github/hedzr/lb?branch=master)\n\n`go-lb` provides a general load balancers library.\n\n## Features\n\nThe stocked algorithm are:\n\n- random\n- round-robin\n- weighted round-robin\n- consistent hash\n- weighted random\n- weighted versioning\n\nUse `Register(...)`/`Unregister(...)` to add the balancer with your algorithm and use it with our `New(algorithm, opts...)`.\n\n## History\n\n- v0.5.1\n  - upgrade deps\n\n- v0.5.0\n  - a tiny logger interface has been embedded. So\n    - we removed the dep to `hedzr/log` and free you from it\n    - you may still enable internal logging sentences in `hedzr/lb` by setting up a custom [Logger](https://github.com/hedzr/lb/blob/master/pkg/logger/public.go#L4)\n\n      ```go\n      import \"github.com/hedzr/lb/pkg/logger\"\n      logger.SetLogger(yoursLogger)\n      ```\n\n  - all codes reviewed\n\n- v0.3.3\n  - needs go modules 1.17 and higher\n  - upgraded deps\n  - remove unecessary deps\n  - remove the nest go.mod since it cannot work any more\n  - fix gin vuln report\n\n- v0.3.1, v0.3.0 : work for below go1.17\n\n## Usages\n\n### Simple\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\tlb \"github.com/hedzr/lb\"\n\t\"github.com/hedzr/lb/lbapi\"\n)\n\nfunc main() {\n\tb := lb.New(lb.RoundRobin)\n\n\tb.Add(exP(\"172.16.0.7:3500\"), exP(\"172.16.0.8:3500\"), exP(\"172.16.0.9:3500\"))\n\tsum := make(map[lbapi.Peer]int)\n\tfor i := 0; i \u003c 300; i++ {\n\t\tp, _ := b.Next(lbapi.DummyFactor)\n\t\tsum[p]++\n\t}\n\n\tfor k, v := range sum {\n\t\tfmt.Printf(\"%v: %v\\n\", k, v)\n\t}\n}\n\ntype exP string\n\nfunc (s exP) String() string { return string(s) }\n```\n\n## About `Weighted versioning`\n\nWith the `Weighted versioning` algorithm, a set of version constraints and weights can be put as the basic rule. Such as:\n\n1. \"\u003c= 1.1.x\", weight: 2,\n2. \"^1.2.x\", weight: 4,\n3. \"^2.x\", weight: 11,\n4. ^3.x\", weight: 3,\n\nthe peers will be picked out by its version. And, all versioning peers will be picked with weights specified in constraints totally.\n\nThe partial codes:\n\n```go\nvar testConstraints = []lbapi.Constrainable{\n\tversion.NewConstrainablePeer(\"\u003c= 1.1.x\", 2),\n\tversion.NewConstrainablePeer(\"^1.2.x\", 4),\n\tversion.NewConstrainablePeer(\"^2.x\", 11),\n\tversion.NewConstrainablePeer(\"^3.x\", 3),\n}\n\nfunc TestVersionWRR2(t *testing.T) {\n\tlb := version.New(version.WithConstrainedPeers(testConstraints...))\n\n\tsum := make(map[lbapi.Peer]int)\n\thits := make(map[lbapi.Peer]map[lbapi.Constrainable]bool)\n\n\tfactor := initFactors()\n\t\n\tfor i := 0; i \u003c 500; i++ {\n\t\tpeer, c := lb.Next(factor)\n\n\t\tsum[peer]++\n\t\tif ps, ok := hits[peer]; ok {\n\t\t\tif _, ok := ps[c]; !ok {\n\t\t\t\tps[c] = true\n\t\t\t}\n\t\t} else {\n\t\t\thits[peer] = make(map[lbapi.Constrainable]bool)\n\t\t\thits[peer][c] = true\n\t\t}\n\t}\n\n\t// results\n\ttotal := 0\n\tfor _, v := range sum {\n\t\ttotal += v\n\t}\n\tfor peer, v := range sum {\n\t\tvar keys []string\n\t\tvar w int\n\t\tfor c := range hits[peer] {\n\t\t\tif kk, ok := c.(fmt.Stringer); ok {\n\t\t\t\tkeys = append(keys, kk.String())\n\t\t\t}\n\t\t\tif ww, ok := c.(lbapi.Weighted); ok {\n\t\t\t\tw = ww.Weight()\n\t\t\t}\n\t\t}\n\t\t// ex := findC(peer)\n\t\tt.Logf(\"%v: %v/%0.2f%%/w:%v. [%v =\u003e weight: %v]\",\n\t\t\tpeer, v, (float32(v)/float32(total))*100.0, w,\n\t\t\tstrings.Join(keys, \",\"), w)\n\t}\n}\n```\n\nSee the full codes at [version/new_test.go](https://github.com/hedzr/lb/blob/master/version/new_test.go), \n\nFor the full document of version constraints: [Masterminds/semver](https://github.com/Masterminds/semver) .\n\n\n## API GW demo\n\nPlease check out the source codes:\n\n- [gw-small](https://github.com/hedzr/lb/blob/master/_examples/gw-small/main.go)\n- [svc-small](https://github.com/hedzr/lb/blob/master/_examples/svc-small/main.go)\n\nAnd the command line to test its:\n\n```bash\ngo run ./_examples/svc-small -port 8111 \u0026\ngo run ./_examples/svc-small -port 8112 \u0026\ngo run ./_examples/svc-small -port 8113 \u0026\n\ngo run ./_examples/gw-lb-aware/main.go 8111 8112 8113 \u0026\n\nfor ((i=0;i\u003c5;i++)); do curl http://localhost:8103/ ; done\n```\n\n\n\n## License\n\nMIT\n\n","funding_links":["https://paypal.me/hezr/3"],"categories":["Go"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhedzr%2Flb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhedzr%2Flb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhedzr%2Flb/lists"}