{"id":13411658,"url":"https://github.com/enriquebris/goconcurrentqueue","last_synced_at":"2026-01-11T23:56:26.837Z","repository":{"id":39737961,"uuid":"165132834","full_name":"enriquebris/goconcurrentqueue","owner":"enriquebris","description":"Go concurrent-safe, goroutine-safe, thread-safe queue","archived":false,"fork":false,"pushed_at":"2023-05-12T05:02:43.000Z","size":151,"stargazers_count":380,"open_issues_count":4,"forks_count":33,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-07-31T20:48:22.319Z","etag":null,"topics":["concurrency","concurrent","concurrent-queue","fifo","fifo-queue","go","golang","goroutine-safe","queue","slice"],"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/enriquebris.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":"2019-01-10T21:21:23.000Z","updated_at":"2024-07-31T12:11:46.000Z","dependencies_parsed_at":"2024-06-18T12:41:14.757Z","dependency_job_id":"6009f769-3499-4729-8298-3e29820c4c61","html_url":"https://github.com/enriquebris/goconcurrentqueue","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enriquebris%2Fgoconcurrentqueue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enriquebris%2Fgoconcurrentqueue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enriquebris%2Fgoconcurrentqueue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enriquebris%2Fgoconcurrentqueue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/enriquebris","download_url":"https://codeload.github.com/enriquebris/goconcurrentqueue/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243618652,"owners_count":20320270,"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":["concurrency","concurrent","concurrent-queue","fifo","fifo-queue","go","golang","goroutine-safe","queue","slice"],"created_at":"2024-07-30T20:01:15.487Z","updated_at":"2026-01-11T23:56:26.830Z","avatar_url":"https://github.com/enriquebris.png","language":"Go","readme":"[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go\u0026logoColor=white)](https://pkg.go.dev/mod/github.com/enriquebris/goconcurrentqueue) [![godoc reference](https://img.shields.io/badge/godoc-reference-blue.svg)](https://godoc.org/github.com/enriquebris/goconcurrentqueue) ![version](https://img.shields.io/badge/version-v0.7.0-yellowgreen.svg?style=flat \"goconcurrentqueue v0.7.0\") [![Build Status](https://api.travis-ci.org/enriquebris/goconcurrentqueue.svg?branch=master)](https://travis-ci.org/enriquebris/goconcurrentqueue) [![Go Report Card](https://goreportcard.com/badge/github.com/enriquebris/goconcurrentqueue)](https://goreportcard.com/report/github.com/enriquebris/goconcurrentqueue)  [![codecov](https://codecov.io/gh/enriquebris/goconcurrentqueue/branch/master/graph/badge.svg)](https://codecov.io/gh/enriquebris/goconcurrentqueue) [![CodeFactor](https://www.codefactor.io/repository/github/enriquebris/goconcurrentqueue/badge)](https://www.codefactor.io/repository/github/enriquebris/goconcurrentqueue) [![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)\n\n# goconcurrentqueue - Concurrent safe queues\nThe package goconcurrentqueue offers a public interface Queue with methods for a [queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)).\nIt comes with multiple Queue's concurrent-safe implementations, meaning they could be used concurrently by multiple goroutines without adding race conditions.\n\n## Topics\n - [Installation](#installation)\n - [Documentation](#documentation)\n - [Classes diagram](#classes-diagram)\n - [Queues](#queues)\n    - [FIFO](#fifo)\n    - [FixedFIFO](#fixedfifo)\n    - [Benchmarks](#benchmarks-fixedfifo-vs-fifo)\n - [Get started](#get-started)\n - [History](#history)\n\n## Installation\n\nExecute\n```bash\ngo get github.com/enriquebris/goconcurrentqueue\n```\n\nThis package is compatible with all golang versions \u003e= 1.7.x\n\n## Documentation\nVisit [goconcurrentqueue at go.dev](https://pkg.go.dev/mod/github.com/enriquebris/goconcurrentqueue)\n\n## Classes diagram\n![goconcurrentqueue class diagram](web/class-diagram.svg \"goconcurrentqueue class diagram\")\n\n## Queues\n\n- First In First Out (FIFO)\n    - [FIFO](#fifo)\n    - [FixedFIFO](#fixedfifo)\n    - [Benchmarks FixedFIFO vs FIFO](#benchmarks-fixedfifo-vs-fifo)\n\n### FIFO\n\n**FIFO**: concurrent-safe auto expandable queue.\n\n#### pros\n - It is possible to enqueue as many items as needed.\n - Extra methods to get and remove enqueued items:\n     - [Get](https://godoc.org/github.com/enriquebris/goconcurrentqueue#FIFO.Get): returns an element's value and keeps the element at the queue\n     - [Remove](https://godoc.org/github.com/enriquebris/goconcurrentqueue#FIFO.Get): removes an element (using a given position) from the queue\n\n#### cons\n - It is slightly slower than FixedFIFO.\n\n### FixedFIFO\n\n**FixedFIFO**: concurrent-safe fixed capacity queue.\n\n#### pros\n - FixedFIFO is, at least, 2x faster than [FIFO](#fifo) in concurrent scenarios (multiple GR accessing the queue simultaneously).\n\n#### cons\n - It has a fixed capacity meaning that no more items than this capacity could coexist at the same time. \n\n## Benchmarks FixedFIFO vs FIFO\n\nThe numbers for the following charts were obtained by running the benchmarks in a 2012 MacBook Pro (2.3 GHz Intel Core i7 - 16 GB 1600 MHz DDR3) with golang v1.12 \n\n### Enqueue\n\n![concurrent-safe FixedFIFO vs FIFO . operation: enqueue](web/FixedFIFO-vs-FIFO-enqueue.png \"concurrent-safe FixedFIFO vs FIFO . operation: enqueue\")\n\n### Dequeue\n\n![concurrent-safe FixedFIFO vs FIFO . operation: dequeue](web/FixedFIFO-vs-FIFO-dequeue.png \"concurrent-safe FixedFIFO vs FIFO . operation: dequeue\")\n\n## Get started\n\n### FIFO queue simple usage\n[Live code - playground](https://play.golang.org/p/CRhg7kX0ikH)\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/enriquebris/goconcurrentqueue\"\n)\n\ntype AnyStruct struct {\n\tField1 string\n\tField2 int\n}\n\nfunc main() {\n\tqueue := goconcurrentqueue.NewFIFO()\n\n\tqueue.Enqueue(\"any string value\")\n\tqueue.Enqueue(5)\n\tqueue.Enqueue(AnyStruct{Field1: \"hello world\", Field2: 15})\n\n\t// will output: 3\n\tfmt.Printf(\"queue's length: %v\\n\", queue.GetLen())\n\n\titem, err := queue.Dequeue()\n\tif err != nil {\n\t\tfmt.Println(err)\n\t\treturn\n\t}\n\n\t// will output \"any string value\"\n\tfmt.Printf(\"dequeued item: %v\\n\", item)\n\n\t// will output: 2\n\tfmt.Printf(\"queue's length: %v\\n\", queue.GetLen())\n\n}\n```\n\n### Wait until an element gets enqueued\n[Live code - playground](https://play.golang.org/p/S7oSg3iUNhs)\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/enriquebris/goconcurrentqueue\"\n)\n\nfunc main() {\n\tvar (\n\t\tfifo = goconcurrentqueue.NewFIFO()\n\t\tdone = make(chan struct{})\n\t)\n\n\tgo func() {\n\t\tfmt.Println(\"1 - Waiting for next enqueued element\")\n\t\tvalue, _ := fifo.DequeueOrWaitForNextElement()\n\t\tfmt.Printf(\"2 - Dequeued element: %v\\n\", value)\n\n\t\tdone \u003c- struct{}{}\n\t}()\n\n\tfmt.Println(\"3 - Go to sleep for 3 seconds\")\n\ttime.Sleep(3 * time.Second)\n\n\tfmt.Println(\"4 - Enqueue element\")\n\tfifo.Enqueue(100)\n\n\t\u003c-done\n}\n\n```\n\n### Wait until an element gets enqueued with timeout\n[Live code - playground](https://play.golang.org/p/E3xdHcW5nJy)\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/enriquebris/goconcurrentqueue\"\n)\n\nfunc main() {\n\tvar (\n\t\tfifo = goconcurrentqueue.NewFIFO()\n\t\tctx, cancel = context.WithTimeout(context.Background(), 3*time.Second)\n\t)\n\tdefer cancel()\n\n\tfmt.Println(\"1 - Waiting for next enqueued element\")\n\t_, err := fifo.DequeueOrWaitForNextElementContext(ctx)\n    \n\tif err != nil {\n\t\tfmt.Printf(\"2 - Failed waiting for new element: %v\\n\", err)\n\t\treturn\n\t}\n}\n\n```\n\n### Dependency Inversion Principle using concurrent-safe queues\n\n*High level modules should not depend on low level modules. Both should depend on abstractions.* Robert C. Martin\n\n[Live code - playground](https://play.golang.org/p/3GAbyR7wrX7)\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/enriquebris/goconcurrentqueue\"\n)\n\nfunc main() {\n\tvar (\n\t\tqueue          goconcurrentqueue.Queue\n\t\tdummyCondition = true\n\t)\n\n\t// decides which Queue's implementation is the best option for this scenario\n\tif dummyCondition {\n\t\tqueue = goconcurrentqueue.NewFIFO()\n\t} else {\n\t\tqueue = goconcurrentqueue.NewFixedFIFO(10)\n\t}\n\n\tfmt.Printf(\"queue's length: %v\\n\", queue.GetLen())\n\tworkWithQueue(queue)\n\tfmt.Printf(\"queue's length: %v\\n\", queue.GetLen())\n}\n\n// workWithQueue uses a goconcurrentqueue.Queue to perform the work\nfunc workWithQueue(queue goconcurrentqueue.Queue) error {\n\t// do some work\n\n\t// enqueue an item\n\tif err := queue.Enqueue(\"test value\"); err != nil {\n\t\treturn err\n\t}\n\n\treturn nil\n}\n```\n\n## History\n\n### v0.7.0\n\n- Prevents FIFO.DequeueOrWaitForNextElement to keep waiting for a waitChan while internal queues contain items\n\n### v0.6.3\n\n- Prevents FIFO.DequeueOrWaitForNextElement to add useless wait channels\n\n### v0.6.2\n\n- Prevents FIFO.DequeueOrWaitForNextElement to gets blocked when waiting for an enqueued element\n\n### v0.6.1\n\n- FixedFifo.Enqueue prevents to get blocked trying to send the item over an invalid waitForNextElementChan channel\n\n### v0.6.0\n\n- Added DequeueOrWaitForNextElementContext()\n\n### v0.5.1\n\n- FIFO.DequeueOrWaitForNextElement() was modified to avoid deadlock when DequeueOrWaitForNextElement \u0026\u0026 Enqueue are invoked around the same time.\n- Added multiple goroutine unit testings for FIFO.DequeueOrWaitForNextElement() \n\n### v0.5.0\n\n- Added DequeueOrWaitForNextElement()\n\n### v0.4.0\n\n- Added QueueError (custom error)\n\n### v0.3.0\n\n- Added FixedFIFO queue's implementation (at least 2x faster than FIFO for multiple GRs)\n- Added benchmarks for both FIFO / FixedFIFO\n- Added GetCap() to Queue interface\n- Removed Get() and Remove() methods from Queue interface\n\n### v0.2.0\n\n- Added Lock/Unlock/IsLocked methods to control operations locking\n\n### v0.1.0\n\n- First In First Out (FIFO) queue added\n","funding_links":[],"categories":["Data Integration Frameworks","Data Structures and Algorithms","数据结构与算法","Data Structures","Uncategorized","数据结构`go语言实现的数据结构与算法`","数据结构","Generators","Go"],"sub_categories":["Queues","队列","Advanced Console UIs","Standard CLI","标准 CLI"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenriquebris%2Fgoconcurrentqueue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fenriquebris%2Fgoconcurrentqueue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenriquebris%2Fgoconcurrentqueue/lists"}