{"id":39679106,"url":"https://github.com/everfore/rddlock","last_synced_at":"2026-01-18T09:50:12.314Z","repository":{"id":57505015,"uuid":"101280610","full_name":"everfore/rddlock","owner":"everfore","description":"redis distribute lock","archived":false,"fork":false,"pushed_at":"2017-08-29T01:34:04.000Z","size":25,"stargazers_count":12,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-06-20T11:45:20.132Z","etag":null,"topics":["distributed-lock","redis-distributed-lock","redis-lock"],"latest_commit_sha":null,"homepage":null,"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/everfore.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":"2017-08-24T09:58:14.000Z","updated_at":"2024-06-20T11:45:20.133Z","dependencies_parsed_at":"2022-09-26T17:51:24.621Z","dependency_job_id":null,"html_url":"https://github.com/everfore/rddlock","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/everfore/rddlock","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/everfore%2Frddlock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/everfore%2Frddlock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/everfore%2Frddlock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/everfore%2Frddlock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/everfore","download_url":"https://codeload.github.com/everfore/rddlock/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/everfore%2Frddlock/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28534175,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-18T00:39:45.795Z","status":"online","status_checked_at":"2026-01-18T02:00:07.578Z","response_time":98,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["distributed-lock","redis-distributed-lock","redis-lock"],"created_at":"2026-01-18T09:50:12.204Z","updated_at":"2026-01-18T09:50:12.284Z","avatar_url":"https://github.com/everfore.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rddlock\nredis distributed lock\n\nredis 分布式锁实现, 原理:_[redis 分布式锁实现](https://github.com/toukii/mdblog/blob/master/Java/redis-lock.md)_\n\nlock的超时时间：timeout_ms\n\n## Usage\n\n__Lock \u0026 UnLock__\n\n\n```golang\nlockkey := \"lock-key\"\ntimeout_ms := 3000\n\nlocked, ex := rddlock.Lock(rds, lockkey, timeout_ms)\ndefer reelock.UnLock(rds, lockkey, ex)\n```\n\n__LockRetry__\n\n\n重试retry_times次，尝试获得锁\n\n```golang\nretry_times := 10\nlocked, ex := reelock.LockRetry(rds, lockkey, timeout_ms, retry_times) // get lock by retry\ndefer reelock.UnLock(rds, lockkey, ex)\n```\n\n__UnLockUnsafe__\n\n\n```golang\nlocked, _ := rddlock.Lock(rds, lockkey, timeout_ms)\ndefer reelock.UnLockUnsafe(rds, lockkey)\n```\n\n__UnLockSafe__\n\n预留安全时间，释放锁。\n\n```golang\nlocked, _ := rddlock.Lock(rds, lockkey, timeout_ms)\nsafeDelTime_ms := 40\ndefer reelock.UnLockSafe(rds, lockkey, safeDelTime_ms)\n```\n\n__SyncDo__\n\n```golang\nerr := SyncDo(rds, lockkey, timeout_ms, func(timeout chan bool) chan bool {\n\t\tret := make(chan bool, 1)\n\t\tgo func() {\n\t\t\tfmt.Println(\"doing...\")\n\t\t\t// TODO SOMETHING\n\t\t\tselect {\n\t\t\tcase \u003c-timeout:\n\t\t\t\t// do the rollback\n\t\t\t\tbreak\n\t\t\tcase ret \u003c- true:\n\t\t\t\tfmt.Println(\"success end.\")\n\t\t\t}\n\t\t}()\n\t\treturn ret\n\t})\n```\n\n## Example\n\n```golang\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/everfore/rddlock\"\n\t\"gopkg.in/ezbuy/redis-orm.v1/orm\"\n\tredis \"gopkg.in/redis.v5\"\n)\n\nfunc main() {\n\t// rds, err := orm.NewRedisClient(\"localhost\", 32768, \"\", 0)\n\trds, err := orm.NewRedisClusterClient(\u0026redis.ClusterOptions{\n\t\tAddrs: []string{\"XXX\", \"XXX\", \"XXX\"},\n\t})\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tlock_key := \"lock-key\"\n\n\tlocked, ex := rddlock.Lock(rds, lock_key, 5)\n\tif locked {\n\t\tfmt.Printf(\"LOCK %s: %+v\\n\", lock_key, locked)\n\t\tunlocked := rddlock.UnLock(rds, lock_key, ex)\n\t\tif unlocked {\n\t\t\tfmt.Printf(\"UNLOCK %s: %+v\\n\", lock_key, unlocked)\n\t\t} else {\n\t\t\tunlocked = rddlock.UnLockUnsafe(rds, lock_key)\n\t\t\tfmt.Printf(\"UNLOCK-UNSAFE %s: %+v\\n\", lock_key, unlocked)\n\t\t}\n\t}\n\n\t// retry lock\n\n\t// 1. lock the key first\n\tlocked, _ = rddlock.Lock(rds, lock_key, 5)\n\tfmt.Printf(\"FIRST step, LOCK %s:%+v\\n\", lock_key, locked)\n\t// 2. retry to lock the locked key\n\tlocked, _ = rddlock.LockRetry(rds, lock_key, 100, 100)\n\tfmt.Printf(\"SECOND step, LOCK-RETRY %s:%+v\\n\", lock_key, locked)\n}\n\n// Output\n// LOCK lock-key: true\n// UNLOCK lock-key: true\n// FIRST step, LOCK lock-key:true\n// SECOND step, LOCK-RETRY lock-key:true\n```\n\n## test\n\n```\nsuccess:200, avg:1.1074123 ms\nfailed:0, avg:NaN ms\n--- PASS: TestLockTime (10.59s)\n\n#local-redis\n=== RUN   TestLockRetryTime\nsuccess:200, avg:1.1741205 ms\nfailed:0, avg:NaN ms\n--- PASS: TestLockRetryTime (10.58s)\n\n#uat-redis\n=== RUN   TestLockRetryTime\nsuccess:200, avg:12.572702 ms\nfailed:0, avg:NaN ms\n--- PASS: TestLockRetryTime (10.59s)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feverfore%2Frddlock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feverfore%2Frddlock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feverfore%2Frddlock/lists"}