{"id":206365,"date":"2025-06-12T11:53:40","date_gmt":"2025-06-12T03:53:40","guid":{"rendered":"https:\/\/server.hk\/cnblog\/206365\/"},"modified":"2025-06-12T11:53:40","modified_gmt":"2025-06-12T03:53:40","slug":"%e4%bd%bf%e7%94%a8-go-redsync-%e6%97%b6%ef%bc%8c%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3-panic-redsync-failed-to-acquire-lock-%e9%94%99%e8%af%af%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/206365\/","title":{"rendered":"\u4f7f\u7528 go-redsync \u65f6\uff0c\u5982\u4f55\u89e3\u51b3 &#8220;panic: redsync: failed to acquire lock&#8221; \u9519\u8bef\uff1f"},"content":{"rendered":"<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4f7f\u7528 go-redsync \u65f6\uff0c\u5982\u4f55\u89e3\u51b3 &#8220;panic: redsync: failed to acquire lock&#8221; \u9519\u8bef\uff1f\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241110\/17312341096730893d0edb9.jpg\" class=\"aligncenter\">,<br \/>\n<strong>redsync \u9501\u83b7\u53d6\u5931\u8d25\uff1apanic: redsync: failed to acquire lock<\/strong>,\u5728\u4f7f\u7528 go-redsync \u6267\u884c\u5206\u5e03\u5f0f\u9501\u65f6\uff0c\u9047\u5230\u9519\u8bef &#8220;panic: redsync: failed to acquire lock&#8221;\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u8be5\u9519\u8bef\u7684\u539f\u56e0\u5e76\u63d0\u4f9b\u89e3\u51b3\u65b9\u6848\u3002,go-redsync \u63d0\u4f9b\u4e86\u4e00\u4e2a\u5206\u5e03\u5f0f\u9501\uff0c\u7528\u4e8e\u534f\u8c03\u591a\u4e2a goroutine \u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u8bbf\u95ee\u3002\u7136\u800c\uff0c\u5982\u679c\u60a8\u53d1\u73b0\u7b2c\u4e8c\u4e2a goroutine \u6267\u884c\u65f6\u53d1\u751f\u9519\u8bef\uff0c\u53ef\u80fd\u662f\u7531\u4e8e\u4ee5\u4e0b\u539f\u56e0\uff1a,<br \/>\n<strong>redsync \u9501\u91cd\u8bd5\u9650\u5236<\/strong>,redsync \u7684\u5206\u5e03\u5f0f\u9501\u5177\u6709\u5185\u7f6e\u7684\u91cd\u8bd5\u673a\u5236\u3002\u5982\u679c\u5728\u5c1d\u8bd5\u83b7\u53d6\u9501\u65f6\u9047\u5230\u51b2\u7a81\uff0c\u5b83\u5c06\u5c1d\u8bd5\u91cd\u8bd5\u591a\u6b21\u3002\u4f46\u662f\uff0c\u91cd\u8bd5\u6b21\u6570\u5e76\u4e0d\u662f\u65e0\u9650\u7684\u3002\u5982\u679c\u5728\u6307\u5b9a\u91cd\u8bd5\u6b21\u6570\u540e\u4ecd\u65e0\u6cd5\u83b7\u53d6\u9501\uff0credsync \u5c06\u629b\u51fa errfailed \u9519\u8bef\uff0c\u8fd9\u5c06\u5bfc\u81f4 panic\u3002,<br \/>\n<strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong>,\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u4e0b\u65b9\u6cd5\uff1a,<br \/>\n<strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong>,\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301GOLANG\uff01\u66f4\u591a\u5173\u4e8eGOLANG\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u3002,<br \/>\n<strong>\u4f7f\u7528\u975e\u963b\u585e\u9501\uff1a<\/strong> \u60a8\u53ef\u4ee5\u4f7f\u7528 redsync \u63d0\u4f9b\u7684\u975e\u963b\u585e\u9501\uff0c\u5b83\u5c06\u5728\u4e0d\u83b7\u53d6\u9501\u7684\u60c5\u51b5\u4e0b\u8fd4\u56de\u3002,<br \/>\n<strong>\u589e\u52a0\u91cd\u8bd5\u6b21\u6570\uff1a<\/strong> \u60a8\u53ef\u4ee5\u5728\u521b\u5efa redsync \u5b9e\u4f8b\u65f6\u8bbe\u7f6e\u81ea\u5b9a\u4e49\u91cd\u8bd5\u7b56\u7565\uff0c\u4ee5\u589e\u52a0\u91cd\u8bd5\u6b21\u6570\u3002,<br \/>\n<strong>\u4f7f\u7528\u8fc7\u671f\u65f6\u95f4\uff1a<\/strong> \u60a8\u53ef\u4ee5\u8bbe\u7f6e\u9501\u7684\u8fc7\u671f\u65f6\u95f4\uff0c\u4ee5\u9632\u6b62\u957f\u65f6\u95f4\u6301\u6709\u9501\u800c\u5bfc\u81f4\u6b7b\u9501\u3002,redsync \u9501\u83b7\u53d6\u5931\u8d25\uff1apanic: redsync: failed to acquire lock,\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4f7f\u7528 go-redsync \u65f6\uff0c\u5982\u4f55\u89e3\u51b3 &#8220;panic: redsync: failed to acquire lock&#8221; \u9519\u8bef\uff1f\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u4f7f\u7528 go-redsync \u65f6\uff0c\u5982\u4f55\u89e3\u51b3 &#8220;panic: redsync: failed to acquire lock&#8221; \u9519\u8bef\uff1f<\/span><\/p>\n<h1>\u4f7f\u7528 go-redsync \u65f6\uff0c\u5982\u4f55\u89e3\u51b3 &#8220;panic: redsync: failed to acquire lock&#8221; \u9519\u8bef\uff1f<\/h1>\n<p><span>2024-11-10 18:22:05<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4f7f\u7528 go-redsync \u65f6\uff0c\u5982\u4f55\u89e3\u51b3 &#8220;panic: redsync: failed to acquire lock&#8221; \u9519\u8bef\uff1f\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241110\/17312341096730893d0edb9.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>redsync \u9501\u83b7\u53d6\u5931\u8d25\uff1apanic: redsync: failed to acquire lock<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 go-redsync \u6267\u884c\u5206\u5e03\u5f0f\u9501\u65f6\uff0c\u9047\u5230\u9519\u8bef &#8220;panic: redsync: failed to acquire lock&#8221;\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u8be5\u9519\u8bef\u7684\u539f\u56e0\u5e76\u63d0\u4f9b\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p>go-redsync \u63d0\u4f9b\u4e86\u4e00\u4e2a\u5206\u5e03\u5f0f\u9501\uff0c\u7528\u4e8e\u534f\u8c03\u591a\u4e2a goroutine \u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u8bbf\u95ee\u3002\u7136\u800c\uff0c\u5982\u679c\u60a8\u53d1\u73b0\u7b2c\u4e8c\u4e2a goroutine \u6267\u884c\u65f6\u53d1\u751f\u9519\u8bef\uff0c\u53ef\u80fd\u662f\u7531\u4e8e\u4ee5\u4e0b\u539f\u56e0\uff1a<\/p>\n<p><strong>redsync \u9501\u91cd\u8bd5\u9650\u5236<\/strong><\/p>\n<p>redsync \u7684\u5206\u5e03\u5f0f\u9501\u5177\u6709\u5185\u7f6e\u7684\u91cd\u8bd5\u673a\u5236\u3002\u5982\u679c\u5728\u5c1d\u8bd5\u83b7\u53d6\u9501\u65f6\u9047\u5230\u51b2\u7a81\uff0c\u5b83\u5c06\u5c1d\u8bd5\u91cd\u8bd5\u591a\u6b21\u3002\u4f46\u662f\uff0c\u91cd\u8bd5\u6b21\u6570\u5e76\u4e0d\u662f\u65e0\u9650\u7684\u3002\u5982\u679c\u5728\u6307\u5b9a\u91cd\u8bd5\u6b21\u6570\u540e\u4ecd\u65e0\u6cd5\u83b7\u53d6\u9501\uff0credsync \u5c06\u629b\u51fa errfailed \u9519\u8bef\uff0c\u8fd9\u5c06\u5bfc\u81f4 panic\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><strong>\u589e\u52a0\u91cd\u8bd5\u6b21\u6570\uff1a<\/strong> \u60a8\u53ef\u4ee5\u5728\u521b\u5efa redsync \u5b9e\u4f8b\u65f6\u8bbe\u7f6e\u81ea\u5b9a\u4e49\u91cd\u8bd5\u7b56\u7565\uff0c\u4ee5\u589e\u52a0\u91cd\u8bd5\u6b21\u6570\u3002<\/li>\n<li><strong>\u4f7f\u7528\u975e\u963b\u585e\u9501\uff1a<\/strong> \u60a8\u53ef\u4ee5\u4f7f\u7528 redsync \u63d0\u4f9b\u7684\u975e\u963b\u585e\u9501\uff0c\u5b83\u5c06\u5728\u4e0d\u83b7\u53d6\u9501\u7684\u60c5\u51b5\u4e0b\u8fd4\u56de\u3002<\/li>\n<li><strong>\u4f7f\u7528\u8fc7\u671f\u65f6\u95f4\uff1a<\/strong> \u60a8\u53ef\u4ee5\u8bbe\u7f6e\u9501\u7684\u8fc7\u671f\u65f6\u95f4\uff0c\u4ee5\u9632\u6b62\u957f\u65f6\u95f4\u6301\u6709\u9501\u800c\u5bfc\u81f4\u6b7b\u9501\u3002<\/li>\n<li><strong>\u4f18\u5316 goroutine \u5e76\u53d1\u6027\uff1a<\/strong> \u5982\u679c\u53ef\u80fd\uff0c\u8bf7\u5c1d\u8bd5\u51cf\u5c11\u540c\u65f6\u5c1d\u8bd5\u83b7\u53d6\u9501\u7684 goroutine \u6570\u91cf\uff0c\u4ee5\u964d\u4f4e\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u3002<\/li>\n<\/ul>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n    \"time\"\n\n    goredislib \"github.com\/go-redis\/redis\/v8\"\n    \"github.com\/go-redsync\/redsync\/v4\"\n    \"github.com\/go-redsync\/redsync\/v4\/redis\/goredis\/v8\"\n)\n\nfunc main() {\n    var wg sync.WaitGroup\n    for i := 0; i &lt; 3; i++ {\n        wg.Add(1)\n        go RedisLock(&amp;wg)\n    }\n    wg.Wait()\n}\n\nfunc RedisLock(wg *sync.WaitGroup) {\n    defer wg.Done()\n    redisAddr := \"192.168.31.51:6379\"\n    client := goredislib.NewClient(&amp;goredislib.Options{\n        Addr:     redisAddr,\n        Password: \"123456\",\n    })\n    pool := goredis.NewPool(client)\n\n    \/\/ \u81ea\u5b9a\u4e49\u91cd\u8bd5\u7b56\u7565\n    retryPolicy := redsync.ExponentialBackoff(1000, 500*time.Millisecond)\n\n    rs := redsync.New(pool, redsync.Options{RetryPolicy: retryPolicy})\n    mutexname := \"product@1\"\n    mutex := rs.NewMutex(mutexname)\n    fmt.Println(\"Start Lock...\")\n    err := mutex.Lock()\n    if err != nil {\n        \/\/ \u5904\u7406\u9519\u8bef\n    }\n    fmt.Println(\"Get Lock!\")\n    time.Sleep(time.Second * 3)\n    fmt.Println(\"Unlock!\")\n    ok, err := mutex.Unlock()\n    if !ok {\n        \/\/ \u5904\u7406\u9519\u8bef\n    }\n    if err != nil {\n        \/\/ \u5904\u7406\u9519\u8bef\n    }\n    fmt.Println(\"Release Lock!\")\n}<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301GOLANG\uff01\u66f4\u591a\u5173\u4e8eGOLANG\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u3002<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   MySQL \u5165\u95e8\u4ece\u96f6\u5f00\u59cb\uff1a21 \u5206\u949f\u5185\u638c\u63e1\u6838\u5fc3\u77e5\u8bc6\uff0c\u5982\u4f55\u5165\u95e8\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u5b9e\u73b0CSS\u60ac\u505c\u6548\u679c\u4e0b\u7684\u6bb5\u843d\u6587\u672c\u591a\u884c\u4e0b\u5212\u7ebf\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-206365","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=206365"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206365\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=206365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=206365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=206365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}