{"id":17963970,"url":"https://github.com/teivah/multilock","last_synced_at":"2025-03-25T05:32:24.722Z","repository":{"id":64302438,"uuid":"236557095","full_name":"teivah/multilock","owner":"teivah","description":"A Go Library to Efficiently Store a Set of Mutex or RWMutex","archived":false,"fork":false,"pushed_at":"2020-01-29T10:41:17.000Z","size":23,"stargazers_count":6,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-19T09:14:26.027Z","etag":null,"topics":["concurrency","go","golang","lock","memory","memory-management","mutex","mutex-synchronisation","rwmutex"],"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/teivah.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}},"created_at":"2020-01-27T18:04:26.000Z","updated_at":"2024-10-12T17:29:45.000Z","dependencies_parsed_at":"2023-01-15T09:45:39.173Z","dependency_job_id":null,"html_url":"https://github.com/teivah/multilock","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/teivah%2Fmultilock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teivah%2Fmultilock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teivah%2Fmultilock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teivah%2Fmultilock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/teivah","download_url":"https://codeload.github.com/teivah/multilock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245407667,"owners_count":20610231,"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","go","golang","lock","memory","memory-management","mutex","mutex-synchronisation","rwmutex"],"created_at":"2024-10-29T11:46:07.094Z","updated_at":"2025-03-25T05:32:24.397Z","avatar_url":"https://github.com/teivah.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# teivah/multilock\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/teivah/multilock)](https://goreportcard.com/report/github.com/teivah/multilock)\n\n_multilock_ is a Go library allowing to store multiple `sync.Mutex` or `sync.RWMutex`.\n\nThe main benefit is to reduce the memory footprint if we need to have a mutex for every single structure of a set (e.g. thousand of maps or slices).\n\nThe internal data structure managed by _multilock_, depending on the use case, has either a fixed or a variable length.\n\n## Structures\n\n* Fixed length structure of Mutex: `multilock.Fixed`\n* Fixed length structure of RWMutex: `multilock.RWFixed`\n* Variable length structure of Mutex: `multilock.Var`\n* Variable length structure of RWMutex: `multilock.RWVar`\n\n## Examples\n\nIn the following example, we will create a 1/10 ratio _multilock_ (10 mutexes to handle 100 maps):\n\n```go\nconst multilockLength = 10\nconst customStructLength = 100\n\n// Initialize a fixed length multilock structure\nmlock := multilock.NewFixed(multilockLength)\n\n// Create custom structures\nmaps := make([]map[string]string, customStructLength)\nfor i := 0; i \u003c customStructLength; i++ {\n\tmaps[i] = make(map[string]string)\n}\n\n// Retrieve a lock for a given maps index\nmutex := mlock.Get(maps[42])\nmutex.Lock()\ndefer mutex.Unlock()\n``` \n\nInternally, _multilock_ has a distribution strategy which is basically hashing the key and returning a modulo based on the length provided.\n\nIt is also possible to override this distribution strategy this way:\n```go\nmlock := multilock.NewFixed(10, multilock.WithCustomDistribution(func(i interface{}, length int) int {\n    // Return an int between 0 and 10 depending on our distribution strategy\n}))\n```\n\nThe last point is related to variable length structures.\nIn this example, we will create one and resize it:\n\n```\nmlock := multilock.NewVar(10)\nmlock.Resize(15)\n```\n\nThe resize operation is safe. \nTo access/resize a variable length structure, it requires to acquire a shared lock first (which does not exist for a fixed structure hence making it faster than a variable one). ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteivah%2Fmultilock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fteivah%2Fmultilock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteivah%2Fmultilock/lists"}