{"id":28491534,"url":"https://github.com/line/garr","last_synced_at":"2025-07-04T23:31:11.229Z","repository":{"id":37472514,"uuid":"473079912","full_name":"line/garr","owner":"line","description":"Collection of high performance, thread-safe, lock-free go data structures","archived":false,"fork":false,"pushed_at":"2022-07-29T01:50:16.000Z","size":107,"stargazers_count":371,"open_issues_count":1,"forks_count":10,"subscribers_count":14,"default_branch":"main","last_synced_at":"2025-03-30T18:35:33.607Z","etag":null,"topics":["golang","high-performance","library","lock-free","thread-safety"],"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/line.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null}},"created_at":"2022-03-23T07:29:32.000Z","updated_at":"2024-10-11T15:22:50.000Z","dependencies_parsed_at":"2022-07-15T15:17:17.009Z","dependency_job_id":null,"html_url":"https://github.com/line/garr","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/line%2Fgarr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/line%2Fgarr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/line%2Fgarr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/line%2Fgarr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/line","download_url":"https://codeload.github.com/line/garr/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/line%2Fgarr/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":258681323,"owners_count":22740554,"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":["golang","high-performance","library","lock-free","thread-safety"],"created_at":"2025-06-08T08:07:39.783Z","updated_at":"2025-07-04T23:31:11.216Z","avatar_url":"https://github.com/line.png","language":"Go","readme":"# Garr - Go libs in a Jar\n\n[![Go Reference](https://pkg.go.dev/badge/go.linecorp.com/garr.svg)](https://pkg.go.dev/go.linecorp.com/garr)\n![CI](https://github.com/line/garr/actions/workflows/ci.yml/badge.svg)\n[![Go Report Card](https://goreportcard.com/badge/go.linecorp.com/garr)](https://goreportcard.com/report/go.linecorp.com/garr)\n\nCollection of high performance, thread-safe, lock-free go data structures.\n\n* [adder](./adder/README.md) - Data structure to perform highly-performant sum under high contention. Inspired by [OpenJDK LongAdder](https://openjdk.java.net/)\n* [circuit-breaker](./circuit-breaker/README.md) - Data structure to implement circuit breaker pattern to detect remote service failure/alive status.\n* [queue](./queue/README.md) - Queue data structure, go implementation of `JDKLinkedQueue` and `MutexLinkedQueue` from `OpenJDK`.\n* [retry](./retry/README.md) - Controls backoff between attempts in a retry operation.\n* [worker-pool](./worker-pool/README.md) - Worker pool implementation in go to help perform multiple tasks concurrently with a fixed-but-expandable amount of workers.\n\n# Usage\n\n## Getting started\n\n```bash\ngo get -u go.linecorp.com/garr\n```\n\n## Examples\n\nPlease find detailed examples in each sub-package.\n\n### Adder\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\tga \"go.linecorp.com/garr/adder\"\n)\n\nfunc main() {\n\t// or ga.DefaultAdder() which uses jdk long-adder as default\n\tadder := ga.NewLongAdder(ga.JDKAdderType) \n\n\tfor i := 0; i \u003c 100; i++ {\n\t\tgo func() {\n\t\t\tadder.Add(123)\n\t\t}()\n\t}\n\n\ttime.Sleep(3 * time.Second)\n\n\t// get total added value\n\tfmt.Println(adder.Sum()) \n}\n```\n\n#### Build your own Prometheus counter with Adder\n\n```go\npackage prom\n\nimport (\n\tga \"go.linecorp.com/garr/adder\"\n\n\t\"github.com/prometheus/client_golang/prometheus\"\n\tdto \"github.com/prometheus/client_model/go\"\n)\n\n// NewCounterI64 creates a new CounterI64 based on the provided prometheus.CounterOpts.\nfunc NewCounterI64(opts prometheus.CounterOpts) CounterI64 {\n\treturn CounterI64{counter: prometheus.NewCounter(opts)}\n}\n\n// CounterI64 is optimized Prometheus Counter for int64 value type.\ntype CounterI64 struct {\n\tval     ga.JDKAdder\n\tcounter prometheus.Counter\n}\n\n// Value returns current value.\nfunc (c *CounterI64) Value() int64 {\n\treturn c.val.Sum()\n}\n\n// Reset value.\nfunc (c *CounterI64) Reset() {\n\tc.val.Reset()\n}\n\n// Desc returns metric desc.\nfunc (c *CounterI64) Desc() *prometheus.Desc {\n\treturn c.counter.Desc()\n}\n\n// Inc by 1.\nfunc (c *CounterI64) Inc() {\n\tc.val.Add(1)\n}\n\n// Add by variant.\nfunc (c *CounterI64) Add(val int64) {\n\tif val \u003e 0 {\n\t\tc.val.Add(val)\n\t}\n}\n\n// Write implements prometheus.Metric interface.\nfunc (c *CounterI64) Write(out *dto.Metric) (err error) {\n\tif err = c.counter.Write(out); err == nil {\n\t\tvalue := float64(c.val.Sum())\n\t\tout.Counter.Value = \u0026value\n\t}\n\treturn\n}\n\n// Collect implements prometheus.Collector interface.\nfunc (c *CounterI64) Collect(ch chan\u003c- prometheus.Metric) {\n\tch \u003c- c\n}\n\n// Describe implements prometheus.Collector interface.\nfunc (c *CounterI64) Describe(ch chan\u003c- *prometheus.Desc) {\n\tch \u003c- c.counter.Desc()\n}\n```\n\n### Queue\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n\n    \"go.linecorp.com/garr/queue\"\n)\n\nfunc main() {\n    q := queue.DefaultQueue() // default using jdk linked queue\n\n    // push\n    q.Offer(123)\n\n    // return head queue but not remove\n    head := q.Peek()\n    fmt.Println(head)\n\n    // remove and return head queue\n    polled := q.Poll()\n    fmt.Println(polled)\n}\n```\n\n### Circuit Breaker\n\n```go\npackage main\n\nimport (\n    cbreaker \"go.linecorp.com/garr/circuit-breaker\"\n)\n\nfunc makeRequest() error {\n\treturn nil\n}\n\nfunc main() {\n    cb := cbreaker.NewCircuitBreakerBuilder().\n                        SetTicker(cbreaker.SystemTicker).\n                        SetFailureRateThreshold(validFailureRateThreshold).\n                        Build()\n\n    if cb.CanRequest() {\n        err := makeRequest()\n        if err != nil {\n            cb.OnFailure()\n        } else {\n            cb.OnSuccess()\n        }\n    }\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fline%2Fgarr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fline%2Fgarr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fline%2Fgarr/lists"}