{"id":20355870,"url":"https://github.com/hedzr/go-ringbuf","last_synced_at":"2026-02-15T08:12:28.287Z","repository":{"id":57531119,"uuid":"276570854","full_name":"hedzr/go-ringbuf","owner":"hedzr","description":"Lock-free MPMC Ring Buffer (Generic) for SMP, in golang. Some posts in chinese:","archived":false,"fork":false,"pushed_at":"2025-02-07T00:00:09.000Z","size":143,"stargazers_count":98,"open_issues_count":1,"forks_count":7,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-29T21:04:17.846Z","etag":null,"topics":["circular-buffer","circular-queue","generic-programming","generics","go","go-generics","golang","golang-library","golang-package","lock-free","ring-buffer","ringbuffer","smp"],"latest_commit_sha":null,"homepage":"https://hedzr.com/algorithm/golang/ringbuf-index/","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/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,"publiccode":null,"codemeta":null},"funding":{"custom":"https://paypal.me/hezr/3"}},"created_at":"2020-07-02T06:51:01.000Z","updated_at":"2025-02-14T04:54:22.000Z","dependencies_parsed_at":"2023-12-27T09:23:51.265Z","dependency_job_id":"490cc3d7-db38-4131-8c30-264c9d86bf55","html_url":"https://github.com/hedzr/go-ringbuf","commit_stats":{"total_commits":84,"total_committers":3,"mean_commits":28.0,"dds":"0.023809523809523836","last_synced_commit":"73252d2c16b7c25981c2ac6fdba56feb863e7bd4"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hedzr%2Fgo-ringbuf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hedzr%2Fgo-ringbuf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hedzr%2Fgo-ringbuf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hedzr%2Fgo-ringbuf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hedzr","download_url":"https://codeload.github.com/hedzr/go-ringbuf/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247406085,"owners_count":20933803,"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":["circular-buffer","circular-queue","generic-programming","generics","go","go-generics","golang","golang-library","golang-package","lock-free","ring-buffer","ringbuffer","smp"],"created_at":"2024-11-14T23:14:21.179Z","updated_at":"2026-02-15T08:12:28.279Z","avatar_url":"https://github.com/hedzr.png","language":"Go","funding_links":["https://paypal.me/hezr/3"],"categories":[],"sub_categories":[],"readme":"# go-ringbuf [V2]\n\n![Go](https://github.com/hedzr/go-ringbuf/workflows/Go/badge.svg)\n[![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/hedzr/go-ringbuf.svg?label=release)](https://github.com/hedzr/go-ringbuf/releases)\n[![go.dev](https://img.shields.io/badge/go-dev-green)](https://pkg.go.dev/github.com/hedzr/go-ringbuf)\n[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/hedzr/go-ringbuf)\n[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fhedzr%2Fgo-ringbuf.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fhedzr%2Fgo-ringbuf?ref=badge_shield)\n[![Go Report Card](https://goreportcard.com/badge/github.com/hedzr/go-ringbuf)](https://goreportcard.com/report/github.com/hedzr/go-ringbuf)\n[![Coverage Status](https://coveralls.io/repos/github/hedzr/go-ringbuf/badge.svg?branch=master\u0026.9)](https://coveralls.io/github/hedzr/go-ringbuf?branch=master)\n\u003c!--\n[![Build Status](https://travis-ci.org/hedzr/go-ringbuf.svg?branch=master)](https://travis-ci.org/hedzr/go-ringbuf)\n[![codecov](https://codecov.io/gh/hedzr/go-ringbuf/branch/master/graph/badge.svg)](https://codecov.io/gh/hedzr/go-ringbuf) \n--\u003e\n\n`go-ringbuf` provides a high-performance, lock-free circular queue (ring buffer) implementation in golang.\n\nMPMC (multiple producers and multiple consumers) enabled.\n\n## History\n\n### v2.2.5\n\n- security patch\n- fix go version in gh-action\n\n### v2.2.3\n\n- fixed issue [#9](https://github.com/hedzr/go-ringbuf/issues/9) (via pr [#10](https://github.com/hedzr/go-ringbuf/pull/10), thx [@srgg](https://github.com/srgg)) a datarace problem caused by an uncheckd cas load failue\n\n### v2.2.2\n\n- [resp for #7] `NewOverlappedRingBuffer` returns `RichOverlappedRingBuffer[T]` now, which gives some new alternatives apis of `Enqueue` so that you can collect `overwrites` and `size` from the returning data.\n  \u003e The legacy codes keep its stable with the original `Enqueue`.\n  \u003e For a normal ringbuf, non-overlapped, nothing's changed since the current capacity (size) is not a very important value for measuring.\n  \u003e If not, issue me.\n\n### v2.2.1\n\n- security updates\n\n### v2.2.0\n\n- added new impl to support overlapped ringbuf\n- security updates\n\n### v2.1.0\n\n- remove extras deps\n  - replaced 'hedzr/log' with 'log/slog', 'hedzr/errors.v3' with 'errors'\n- remove WithLogger()\n\n### v2.0.+\n\nsecurity updates\n\n### v2.0.0 @20220408 - go 1.18+\n\ngeneric version for MPMC Ring Buffer.\n\n- rewritten with go generics\n\n### v1.0.+ [archived]\n\nsecurity updates\n\n### v1.0.0 @20220408\n\nLast release for classical version.\n\nNext release (v2) will move to go 1.18+ with generic enabled.\n\n### v0.9.1 @2022\n\n- review all codes\n- updated deps\n- review and solve uncertain misreport failed licenses\n  - ~~we have two deps: [hedzr/errors](https://github.com/hedzr/errors) and [hedzr/log](https://github.com/hedzr/log), and both them have no 3rd-party deps.~~\n    - since 2.1.0, any deps removed.\n  - we have no more 3rd-party deps.\n  - we assumed a free license under MIT (unified).\n\n## Getting Start\n\n```bash\ngo get -v github.com/hedzr/go-ringbuf/v2\n```\n\n### Samples\n\n```go\npackage main\n\nimport (\n \"fmt\"\n \"log\"\n\n \"github.com/hedzr/go-ringbuf/v2\"\n)\n\nfunc main() {\n testIntRB()\n testStringRB()\n}\n\nfunc testStringRB() {\n var err error\n var rb = ringbuf.New[string](80)\n err = rb.Enqueue(\"abcde\")\n errChk(err)\n\n var item string\n item, err = rb.Dequeue()\n errChk(err)\n fmt.Printf(\"dequeue ok: %v\\n\", item)\n}\n\nfunc testIntRB() {\n var err error\n var rb = ringbuf.New[int](80)\n err = rb.Enqueue(3)\n errChk(err)\n\n var item int\n item, err = rb.Dequeue()\n errChk(err)\n fmt.Printf(\"dequeue ok: %v\\n\", item)\n}\n\nfunc errChk(err error) {\n if err != nil {\n  log.Fatal(err)\n }\n}\n```\n\n### Using Ring-Buffer as a fixed resource pool\n\nThe following codes is for v1, needed for rewriting\n\n```go\nfunc newRes() *Res{...}\n\nvar rb mpmc.RingBuffer\n\nfunc initFunc() (err error) {\n  const maxSize = 16\n  \n  if rb = mpmc.New(uint32(maxSize)); rb == nil {\n    err = errors.New(\"cannot create mpmc.RingBuffer\")\n    return\n  }\n\n  // CapReal() will be available since v0.8.8, or replace it with Cap() - 1\n  for i := uint32(0); i \u003c rb.CapReal(); i++ {\n    if err = rb.Enqueue(newRes()); err != nil {\n      return\n    }\n  }\n}\n\nfunc loopFor() {\n  var err error\n  for {\n    it, err := rb.Dequeue()\n    checkErr(err)\n    if res, ok := it.(*Res); ok {\n      // do stuff with `res`, and put it back into ring-buffer\n      err = rb.Enqueue(it)\n    }\n  }\n}\n```\n\n### Using Overlapped Ring Buffer\n\nSince v2.2.0, `NewOverlappedRingBuffer()` can initiate a different ring buffer, which\nallows us to overwrite the head element if putting new element into a **full** ring buffer.\n\n```go\nfunc testStringRB() {\n  var err error\n  var rb = ringbuf.NewOverlappedRingBuffer[string](80)\n  err = rb.Enqueue(\"abcde\")\n  errChk(err)\n\n  var item string\n  item, err = rb.Dequeue()\n  errChk(err)\n  fmt.Printf(\"dequeue ok: %v\\n\", item)\n}\n```\n\n## Contrib\n\nWelcome\n\n## LICENSE\n\nApache 2.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhedzr%2Fgo-ringbuf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhedzr%2Fgo-ringbuf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhedzr%2Fgo-ringbuf/lists"}