{"id":13367298,"url":"https://github.com/beefsack/Go-rate","last_synced_at":"2025-03-12T18:32:22.372Z","repository":{"id":20034041,"uuid":"23302143","full_name":"beefsack/go-rate","owner":"beefsack","description":"A timed rate limiter for Go","archived":false,"fork":false,"pushed_at":"2022-02-14T23:34:05.000Z","size":27,"stargazers_count":395,"open_issues_count":0,"forks_count":38,"subscribers_count":13,"default_branch":"master","last_synced_at":"2024-08-03T17:19:43.543Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/beefsack.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":null,"security":null,"support":null},"funding":{"github":"beefsack"}},"created_at":"2014-08-25T04:42:34.000Z","updated_at":"2024-07-19T02:58:13.000Z","dependencies_parsed_at":"2022-07-23T14:32:25.859Z","dependency_job_id":null,"html_url":"https://github.com/beefsack/go-rate","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/beefsack%2Fgo-rate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beefsack%2Fgo-rate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beefsack%2Fgo-rate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beefsack%2Fgo-rate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/beefsack","download_url":"https://codeload.github.com/beefsack/go-rate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221309967,"owners_count":16795837,"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":[],"created_at":"2024-07-30T00:01:43.929Z","updated_at":"2024-10-24T11:31:11.151Z","avatar_url":"https://github.com/beefsack.png","language":"Go","funding_links":["https://github.com/sponsors/beefsack"],"categories":["实用工具","實用工具"],"sub_categories":["高级控制台界面","高級控制台界面"],"readme":"go-rate\n===============\n\n[![Build Status](https://travis-ci.org/beefsack/go-rate.svg?branch=master)](https://travis-ci.org/beefsack/go-rate)\n[![GoDoc](https://godoc.org/github.com/beefsack/go-rate?status.svg)](https://godoc.org/github.com/beefsack/go-rate)\n\n**go-rate** is a rate limiter designed for a range of use cases,\nincluding server side spam protection and preventing saturation of APIs you\nconsume.\n\nIt is used in production at\n[LangTrend](http://langtrend.com/l/Java,PHP,JavaScript) to adhere to the GitHub\nAPI rate limits.\n\nUsage\n-----\n\nImport `github.com/beefsack/go-rate` and create a new rate limiter with\nthe `rate.New(limit int, interval time.Duration)` function.\n\nThe rate limiter provides a `Wait()` and a `Try() (bool, time.Duration)` method\nfor both blocking and non-blocking functionality respectively.\n\nAPI documentation available at [godoc.org](http://godoc.org/github.com/beefsack/go-rate).\n\nExamples\n--------\n\n### Blocking rate limiting\n\nThis example demonstrates limiting the output rate to 3 times per second.\n\n```Go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/beefsack/go-rate\"\n)\n\nfunc main() {\n\trl := rate.New(3, time.Second) // 3 times per second\n\tbegin := time.Now()\n\tfor i := 1; i \u003c= 10; i++ {\n\t\trl.Wait()\n\t\tfmt.Printf(\"%d started at %s\\n\", i, time.Now().Sub(begin))\n\t}\n\t// Output:\n\t// 1 started at 12.584us\n\t// 2 started at 40.13us\n\t// 3 started at 44.92us\n\t// 4 started at 1.000125362s\n\t// 5 started at 1.000143066s\n\t// 6 started at 1.000144707s\n\t// 7 started at 2.000224641s\n\t// 8 started at 2.000240751s\n\t// 9 started at 2.00024244s\n\t// 10 started at 3.000314332s\n}\n```\n\n### Blocking rate limiting with multiple limiters\n\nThis example demonstrates combining rate limiters, one limiting at once per\nsecond, the other limiting at 2 times per 3 seconds.\n\n```Go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/beefsack/go-rate\"\n)\n\nfunc main() {\n\tbegin := time.Now()\n\trl1 := rate.New(1, time.Second)   // Once per second\n\trl2 := rate.New(2, time.Second*3) // 2 times per 3 seconds\n\tfor i := 1; i \u003c= 10; i++ {\n\t\trl1.Wait()\n\t\trl2.Wait()\n\t\tfmt.Printf(\"%d started at %s\\n\", i, time.Now().Sub(begin))\n\t}\n\t// Output:\n\t// 1 started at 11.197us\n\t// 2 started at 1.00011941s\n\t// 3 started at 3.000105858s\n\t// 4 started at 4.000210639s\n\t// 5 started at 6.000189578s\n\t// 6 started at 7.000289992s\n\t// 7 started at 9.000289942s\n\t// 8 started at 10.00038286s\n\t// 9 started at 12.000386821s\n\t// 10 started at 13.000465465s\n}\n```\n\n### Non-blocking rate limiting\n\nThis example demonstrates non-blocking rate limiting, such as would be used to\nlimit spam in a chat client.\n\n```Go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/beefsack/go-rate\"\n)\n\nvar rl = rate.New(3, time.Second) // 3 times per second\n\nfunc say(message string) {\n\tif ok, remaining := rl.Try(); ok {\n\t\tfmt.Printf(\"You said: %s\\n\", message)\n\t} else {\n\t\tfmt.Printf(\"Spam filter triggered, please wait %s\\n\", remaining)\n\t}\n}\n\nfunc main() {\n\tfor i := 1; i \u003c= 5; i++ {\n\t\tsay(fmt.Sprintf(\"Message %d\", i))\n\t}\n\ttime.Sleep(time.Second / 2)\n\tsay(\"I waited half a second, is that enough?\")\n\ttime.Sleep(time.Second / 2)\n\tsay(\"Okay, I waited a second.\")\n\t// Output:\n\t// You said: Message 1\n\t// You said: Message 2\n\t// You said: Message 3\n\t// Spam filter triggered, please wait 999.980816ms\n\t// Spam filter triggered, please wait 999.976704ms\n\t// Spam filter triggered, please wait 499.844795ms\n\t// You said: Okay, I waited a second.\n}\n```\n\nAuthors\n-------\n\n* [Michael Alexander](https://github.com/beefsack)\n* [Geert-Johan Riemer](https://github.com/GeertJohan)\n* [Matt T. Proud](https://github.com/matttproud)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeefsack%2FGo-rate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeefsack%2FGo-rate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeefsack%2FGo-rate/lists"}