{"id":17955685,"url":"https://github.com/smallnest/weighted","last_synced_at":"2025-04-05T05:09:59.337Z","repository":{"id":56534466,"uuid":"75247448","full_name":"smallnest/weighted","owner":"smallnest","description":"high-performance common weighted algorithm library","archived":false,"fork":false,"pushed_at":"2023-04-19T05:54:10.000Z","size":27,"stargazers_count":202,"open_issues_count":3,"forks_count":43,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-03-29T04:11:48.394Z","etag":null,"topics":["algortihm","weight"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/smallnest.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}},"created_at":"2016-12-01T02:32:32.000Z","updated_at":"2025-03-07T17:37:35.000Z","dependencies_parsed_at":"2023-01-25T09:15:23.959Z","dependency_job_id":"72b8902b-eae2-4e27-b887-fe09fc61a1eb","html_url":"https://github.com/smallnest/weighted","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smallnest%2Fweighted","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smallnest%2Fweighted/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smallnest%2Fweighted/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smallnest%2Fweighted/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/smallnest","download_url":"https://codeload.github.com/smallnest/weighted/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247289429,"owners_count":20914464,"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":["algortihm","weight"],"created_at":"2024-10-29T10:31:44.415Z","updated_at":"2025-04-05T05:09:59.320Z","avatar_url":"https://github.com/smallnest.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![License](https://img.shields.io/:license-apache-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![GoDoc](https://godoc.org/github.com/smallnest/weighted?status.png)](http://godoc.org/github.com/smallnest/weighted)  [![travis](https://travis-ci.org/smallnest/weighted.svg?branch=master)](https://travis-ci.org/smallnest/weighted) [![coveralls](https://coveralls.io/repos/smallnest/weighted/badge.svg?branch=master\u0026service=github)](https://coveralls.io/github/smallnest/weighted?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/smallnest/weighted)](https://goreportcard.com/report/github.com/smallnest/weighted)\n\n\n**rust version**: [weighted-rs](https://github.com/smallnest/weighted-rs)\n\nPackage **weighted** implements the smooth weighted round-robin balancing algorithm. This algorithm is implemented in Nginx:\nhttps://github.com/phusion/nginx/commit/27e94984486058d73157038f7950a0a36ecc6e35.\n\n**Notice**: The weighted is NOT goroutine-safe so you MUST use the synchronization primitive to protect it (the Next method) in concurrent cases.\n\n\nAlgorithm is as follows: on each peer selection we increase current_weight\nof each eligible peer by its weight, select peer with greatest current_weight\nand reduce its current_weight by total number of weight points distributed\namong peers.\n\nIn case of { 5, 1, 1 } weights this gives the following sequence of\ncurrent_weight's: (a, a, b, a, c, a, a)\n\nThis is an example to use it:\n\n```go\npackage main\n\nimport \"fmt\"\n\nfunc ExampleSW_Next() {\n\tw := \u0026SW{}\n\tw.Add(\"a\", 5)\n\tw.Add(\"b\", 2)\n\tw.Add(\"c\", 3)\n\n\tfor i := 0; i \u003c 10; i++ {\n\t\tfmt.Printf(\"%s \", w.Next())\n\t}\n}\n```\n\nAnd this lib has provides another weighted round robin algorithm. This algorithm is used in [LVS](http://kb.linuxvirtualserver.org/wiki/Weighted_Round-Robin_Scheduling).\nIt has better performance but it is not so more smooth than the first algorithm, so you can select one algorithm according to your case. It is used like the first:\n\n```go\npackage main\n\nimport \"fmt\"\n\nfunc ExampleRRW_Next() {\n\tw := \u0026RRW{}\n\tw.Add(\"a\", 5)\n\tw.Add(\"b\", 2)\n\tw.Add(\"c\", 3)\n\n\tfor i := 0; i \u003c 10; i++ {\n\t\tfmt.Printf(\"%s \", w.Next())\n\t}\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmallnest%2Fweighted","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmallnest%2Fweighted","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmallnest%2Fweighted/lists"}