{"id":18403736,"url":"https://github.com/russellluo/goodtimer","last_synced_at":"2025-04-07T07:32:46.907Z","repository":{"id":72256695,"uuid":"148030368","full_name":"RussellLuo/goodtimer","owner":"RussellLuo","description":" Golang timer for humans.","archived":false,"fork":false,"pushed_at":"2022-05-18T06:33:52.000Z","size":11,"stargazers_count":39,"open_issues_count":1,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-22T14:41:30.952Z","etag":null,"topics":["go","timer"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/RussellLuo/goodtimer","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/RussellLuo.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":"2018-09-09T13:53:59.000Z","updated_at":"2024-06-20T06:35:12.000Z","dependencies_parsed_at":"2023-02-26T21:45:42.782Z","dependency_job_id":null,"html_url":"https://github.com/RussellLuo/goodtimer","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/RussellLuo%2Fgoodtimer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RussellLuo%2Fgoodtimer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RussellLuo%2Fgoodtimer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RussellLuo%2Fgoodtimer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RussellLuo","download_url":"https://codeload.github.com/RussellLuo/goodtimer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247612515,"owners_count":20966768,"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":["go","timer"],"created_at":"2024-11-06T02:48:02.505Z","updated_at":"2025-04-07T07:32:45.902Z","avatar_url":"https://github.com/RussellLuo.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# goodtimer\n\nGolang timer for humans.\n\n`goodtimer` is a thin wrapper around the standard [time.Timer][1], and it tries to play two roles:\n\n1. A library that helps you use [time.Timer][1] more easily.\n2. As well as a demonstration that shows you how to use [time.Timer][1] correctly.\n\n中文博客：[使用 Golang Timer 的正确方式](http://russellluo.com/2018/09/the-correct-way-to-use-timer-in-golang.html)。\n\n\n## Installation\n\n```bash\n$ go get -u github.com/RussellLuo/goodtimer\n```\n\n\n## Documentation\n\nFor usage and examples see the [Godoc][2].\n\n\n## Why?!\n\n**TL;DR**: The timer created with [time.NewTimer][9] is hard to use correctly.\n\n### Timer.Stop\n\nPer the documentation of [Timer.Stop][3], to stop the timer created with [time.NewTimer][9], you need to check the return value and drain the channel if necessary:\n\n```go\nif !t.Stop() {\n\t\u003c-t.C\n}\n```\n\nBut the draining operation will be blocked if the the program has already received from the Timer's channel before. So someone suggests doing a non-blocking draining:\n\n```go\nif !t.Stop() {\n\tselect {\n\tcase \u003c-t.C: // try to drain the channel\n\tdefault:\n\t}\n}\n```\n\nHowever, [there is a race condition][4] between draining the channel and sending time into the channel, which may lead to a undrained channel.\n\n\n### Timer.Reset\n\nTo reset a timer, is must have expired or be stopped before. So [Timer.Reset][5] has almost the same issue with [Timer.Stop][3].\n\n\n### Solutions\n\nFinally, as [Russ Cox][6] suggested ([here][7] and [here][8]), the correct way to use [time.Timer][1] is:\n\n- All the Timer operations ([Timer.Stop][3], [Timer.Reset][5] and receiving from or draining the channel) should be done in the same goroutine.\n- The program should manage an extra status showing whether it has received from the Timer's channel or not.\n\n\n[1]: https://golang.org/pkg/time/#Timer\n[2]: https://pkg.go.dev/github.com/RussellLuo/goodtimer\n[3]: https://golang.org/pkg/time/#Timer.Stop\n[4]: https://github.com/golang/go/issues/14383#issuecomment-185977844\n[5]: https://golang.org/pkg/time/#Timer.Reset\n[6]: https://github.com/rsc\n[7]: https://github.com/golang/go/issues/11513#issuecomment-157062583\n[8]: https://groups.google.com/d/msg/golang-dev/c9UUfASVPoU/tlbK2BpFEwAJ\n[9]: https://pkg.go.dev/time#NewTimer\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frussellluo%2Fgoodtimer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frussellluo%2Fgoodtimer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frussellluo%2Fgoodtimer/lists"}