{"id":948,"date":"2024-11-07T13:27:48","date_gmt":"2024-11-07T05:27:48","guid":{"rendered":"https:\/\/fwq.ai\/blog\/948\/"},"modified":"2024-11-07T13:27:48","modified_gmt":"2024-11-07T05:27:48","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:\/\/fwq.ai\/blog\/948\/","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><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-963\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173089350687842.jpg\" width=\"800\" height=\"320\"panic: redsync: failed to acquire lock\" \u9519\u8bef\uff1f-1\"panic: redsync: failed to acquire lock\" \u9519\u8bef\uff1f-1\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173089350687842.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173089350687842-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173089350687842-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173089350687842-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"\u4f7f\u7528 go-redsync \u65f6\uff0c\u5982\u4f55\u89e3\u51b3 &#8220;panic: redsync: failed to acquire lock&#8221; \u9519\u8bef\uff1f\u63d2\u56fe\" alt=\"\u4f7f\u7528 go-redsync \u65f6\uff0c\u5982\u4f55\u89e3\u51b3 &#8220;panic: redsync: failed to acquire lock&#8221; \u9519\u8bef\uff1f\u63d2\u56fe\" \/><\/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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528 go-redsync \u65f6\uff0c\u5982\u4f55\u89e3\u51b3 &#8220;panic: redsync: failed to acquire lock&#8221; \u9519\u8bef\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>redsync \u9501\u83b7\u53d6\u5931\u8d25\uff1apanic: redsync: failed to acquire lock \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 redsync \u9501\u91cd\u8bd5\u9650\u5236 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 \u89e3\u51b3\u65b9\u6848\uff1a \u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u4e0b\u65b9\u6cd5\uff1a \u589e\u52a0\u91cd\u8bd5\u6b21\u6570\uff1a \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 \u4f7f\u7528\u975e\u963b\u585e\u9501\uff1a \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 \u4f7f\u7528\u8fc7\u671f\u65f6\u95f4\uff1a \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 \u4f18\u5316 goroutine \u5e76\u53d1\u6027\uff1a \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 \u4ee3\u7801\u793a\u4f8b\uff1a package main import ( &#8220;fmt&#8221; &#8220;sync&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-948","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=948"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/948\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}