{"id":19288519,"url":"https://github.com/samber/go-singleflightx","last_synced_at":"2025-04-22T04:33:35.695Z","repository":{"id":225894380,"uuid":"767158415","full_name":"samber/go-singleflightx","owner":"samber","description":"🧬 x/sync/singleflight but with generics, batching, sharding and nullable result","archived":false,"fork":false,"pushed_at":"2024-11-09T18:03:33.000Z","size":55,"stargazers_count":27,"open_issues_count":7,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-09T19:17:46.594Z","etag":null,"topics":["cache","channel","concurrent","deduplication","generics","go","in-flight","singleflight","sync"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/samber/go-singleflightx","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/samber.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["samber"]}},"created_at":"2024-03-04T20:03:22.000Z","updated_at":"2024-11-09T18:02:22.000Z","dependencies_parsed_at":"2024-03-04T21:20:59.525Z","dependency_job_id":"d8c48216-c511-4da2-8adb-7ac881a62ff6","html_url":"https://github.com/samber/go-singleflightx","commit_stats":null,"previous_names":["samber/go-singleflightx"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samber%2Fgo-singleflightx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samber%2Fgo-singleflightx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samber%2Fgo-singleflightx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samber%2Fgo-singleflightx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/samber","download_url":"https://codeload.github.com/samber/go-singleflightx/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223888365,"owners_count":17220083,"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":["cache","channel","concurrent","deduplication","generics","go","in-flight","singleflight","sync"],"created_at":"2024-11-09T22:09:15.387Z","updated_at":"2025-04-22T04:33:35.688Z","avatar_url":"https://github.com/samber.png","language":"Go","funding_links":["https://github.com/sponsors/samber"],"categories":[],"sub_categories":[],"readme":"\n# go-singleflightx\n\n[![tag](https://img.shields.io/github/tag/samber/go-singleflightx.svg)](https://github.com/samber/go-singleflightx/releases)\n![Go Version](https://img.shields.io/badge/Go-%3E%3D%201.18.0-%23007d9c)\n[![GoDoc](https://godoc.org/github.com/samber/go-singleflightx?status.svg)](https://pkg.go.dev/github.com/samber/go-singleflightx)\n![Build Status](https://github.com/samber/go-singleflightx/actions/workflows/test.yml/badge.svg)\n[![Go report](https://goreportcard.com/badge/github.com/samber/go-singleflightx)](https://goreportcard.com/report/github.com/samber/go-singleflightx)\n[![Coverage](https://img.shields.io/codecov/c/github/samber/go-singleflightx)](https://codecov.io/gh/samber/go-singleflightx)\n[![Contributors](https://img.shields.io/github/contributors/samber/go-singleflightx)](https://github.com/samber/go-singleflightx/graphs/contributors)\n[![License](https://img.shields.io/github/license/samber/go-singleflightx)](./LICENSE)\n\n\u003e x/sync/singleflight but better\n\n## Features\n\nThis library is inspired by `x/sync/singleflight` but adds many features:\n- 🧬 generics\n- 🍱 batching: fetch multiple keys in a single callback, with in-flight deduplication\n- 📭 nullable result\n- 🍕 sharded groups\n\n## 🚀 Install\n\n```sh\ngo get github.com/samber/go-singleflightx\n```\n\nThis library is v0 and follows SemVer strictly. No breaking changes will be made to exported APIs before v1.0.0.\n\n## 💡 Doc\n\nGoDoc: [https://pkg.go.dev/github.com/samber/go-singleflightx](https://pkg.go.dev/github.com/samber/go-singleflightx)\n\n## Examples\n\nHere is an example of a user retrieval in a caching layer:\n\n```go\nimport \"github.com/samber/go-singleflightx\"\n\nfunc getUsersByID(userIDs []string) (map[string]User, error) {\n    users := []User{}\n\n    // 📍 SQL query here...\n    err := sqlx.Select(\u0026users, \"SELECT * FROM users WHERE id IN (?);\", userIDs...)\n    if err != nil {\n        return nil, err\n    }\n\n    var results = map[string]User{}\n    for _, u := range users {\n        results[u.ID] = u\n    }\n\n    return results, nil\n}\n\nfunc main() {\n    var g singleflightx.Group[string, User]\n\n    // 👇 concurrent queries will be dedup\n    output := g.DoX([]string{\"user-1\", \"user-2\"}, getUsersByID)\n}\n```\n\n`output` is of type `map[K]singleflightx.Result[V]`, and will always have as many entries as requested, whatever the callback result.\n\n```go\ntype Result[V any] struct {\n  \t Value  singleflightx.NullValue[V]  // 💡 A result is considered \"null\" if the callback did not return it.\n  \t Err    error\n  \t Shared bool\n}\n\ntype NullValue[V any] struct {\n\tValue V\n\tValid bool\n}\n```\n\n### Sharded groups, for high contention/concurrency environments\n\n```go\ng := singleflightx.NewShardedGroup[K string, User](10, func (key string) uint {\n    h := fnv.New64a()\n    h.Write([]byte(key))\n    return uint(h.Sum64())\n})\n\n// as usual, but if the keys match different shards, getUsersByID will be called twice\noutput := g.DoX([]string{\"user-1\", \"user-2\"}, getUsersByID) \n```\n\n### go-singleflightx + go-batchify\n\n`go-batchify` groups concurrent tasks into a single batch. By adding `go-singleflightx`, you will be able to dedupe\n\n```go\nimport (\n    \"golang.org/x/sync/singleflight\"\n    \"github.com/samber/go-batchify\"\n)\n\nvar group singleflight.Group\n\nbatch := batchify.NewBatchWithTimer(\n    10,\n    func (ids []int) (map[int]string, error) {\n        return ..., nil\n    },\n    5*time.Millisecond,\n)\n\nhttp.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request) {\n    idStr := r.URL.Query().Get(\"id\")\n    id, _ := strconv.Atoi(idStr)\n\n    value, err, _ = group.Do(idStr, func() (interface{}, error) {\n        return batch.Do(id)\n    })\n\n    // ...\n})\n```\n\n## 🤝 Contributing\n\n- Ping me on Twitter [@samuelberthe](https://twitter.com/samuelberthe) (DMs, mentions, whatever :))\n- Fork the [project](https://github.com/samber/go-singleflightx)\n- Fix [open issues](https://github.com/samber/go-singleflightx/issues) or request new features\n\nDon't hesitate ;)\n\n```bash\n# Install some dev dependencies\nmake tools\n\n# Run tests\nmake test\n# or\nmake watch-test\n```\n\n## 👤 Contributors\n\n![Contributors](https://contrib.rocks/image?repo=samber/go-singleflightx)\n\n## 💫 Show your support\n\nGive a ⭐️ if this project helped you!\n\n[![GitHub Sponsors](https://img.shields.io/github/sponsors/samber?style=for-the-badge)](https://github.com/sponsors/samber)\n\n## 📝 License\n\nCopyright © 2023 [Samuel Berthe](https://github.com/samber).\n\nThis project is [MIT](./LICENSE) licensed.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamber%2Fgo-singleflightx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamber%2Fgo-singleflightx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamber%2Fgo-singleflightx/lists"}