{"id":207177,"date":"2025-07-08T08:28:18","date_gmt":"2025-07-08T00:28:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207177\/"},"modified":"2025-07-08T08:28:18","modified_gmt":"2025-07-08T00:28:18","slug":"%e5%b8%a6-ttl-%e7%9a%84-etcd-%e4%ba%92%e6%96%a5%e9%94%81","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207177\/","title":{"rendered":"\u5e26 ttl \u7684 etcd \u4e92\u65a5\u9501"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5e26 ttl \u7684 etcd \u4e92\u65a5\u9501<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 16:54:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5e26 ttl \u7684 etcd \u4e92\u65a5\u9501\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6f14\u793agolang etcd\u5ba2\u6237\u7aef\u7a0b\u5e8f\uff0c\u5b83\u4f7f\u7528etcd\u4e92\u65a5\u4f53\u521b\u5efa\u4e00\u4e2a\u5171\u4eab\u9501\uff0c\u5e76\u8bbe\u7f6e\u8d85\u65f6\u3002\u76ee\u6807\u662f\u8ba9\u4e92\u65a5\u9501\u5728\u4e00\u6bb5\u65f6\u95f4\u540e\u8fc7\u671f\u3002<\/p>\n<pre>package main\n\nimport (\n    \"context\"\n    \"log\"\n    \"time\"\n\n    \"go.etcd.io\/etcd\/clientv3\"\n    \"go.etcd.io\/etcd\/clientv3\/concurrency\"\n)\n\nvar c chan int\n\nfunc init() {\n    c = make(chan int)\n}\n\nfunc main() {\n    client, err := clientv3.new(clientv3.config{\n        endpoints: []string{\"http:\/\/localhost:2379\"},\n    })\n    if err != nil {\n        panic(err)\n    }\n\n    watcher := clientv3.newwatcher(client)\n    channel := watcher.watch(context.background(), \"\/foobar\", clientv3.withprefix())\n    go func() {\n        for {\n            select {\n            case change := &lt;-channel:\n                for _, ev := range change.events {\n                    log.printf(\"etcd change on key; %s, type = %v\", string(ev.kv.key), ev.type)\n                }\n            }\n        }\n    }()\n\n    go lockfoobar(client, 1)\n    go lockfoobar(client, 2)\n    &lt;-c\n    &lt;-c\n}\n\nfunc lockfoobar(client *clientv3.client, id int) {\n    res, err := client.grant(context.background(), 1)\n    if err != nil {\n        panic(err)\n    }\n\n    session, err := concurrency.newsession(client, concurrency.withlease(res.id))\n    if err != nil {\n        panic(err)\n    }\n\n    mux := concurrency.newmutex(session, \"\/foobar\")\n\n    log.printf(\"trying to lock by #%d\\n\", id)\n    ctx, _ := context.withtimeout(context.background(), 15*time.second)\n    if err := mux.lock(ctx); err != nil {\n        log.printf(\"failed to lock #%d: %v\\n\", id, err)\n        c &lt;- id\n        return\n    }\n\n    log.printf(\"post-lock #%d (lease id = %x) bullshit\\n\", id, res.id)\n    time.sleep(10 * time.second)\n    ttl, _ := client.timetolive(context.todo(), res.id)\n    log.printf(\"post-post-lock-#%d-sleep. lease ttl = %v\", id, ttl.ttl)\n    \/\/ mux.unlock(ctx)\n    \/\/ log.printf(\"post-unlock #%d bullshit\\n\", id)\n\n    time.sleep(200 * time.millisecond)\n    c &lt;- id\n}<\/pre>\n<p>\u79df\u7ea6\u7684 ttl \u4e3a 1 \u79d2\uff0c\u800c\u4e0a\u4e0b\u6587\u7684\u8d85\u65f6\u65f6\u95f4\u4e3a 5 \u79d2\uff0c\u56e0\u6b64\uff0c\u5728\u4e0a\u4e0b\u6587\u5230\u671f\u65f6\u9501\u5e94\u8be5\u5df2\u88ab\u5220\u9664\u3002\u4f46\u662f\uff0c\u65e0\u8bba\u4e0a\u4e0b\u6587\u8d85\u65f6\u5982\u4f55\uff0c\u201c\u5df2\u9501\u5b9a\u201d\u9501\u59cb\u7ec8\u4ec5\u5728\u9501\u5b9a\u5931\u8d25\u540e\u624d\u4f1a\u88ab\u5220\u9664\u3002<\/p>\n<p>\u8fd9\u662f\u5f53\u524d\u7684\u8f93\u51fa\uff1a<\/p>\n<pre>2018-10-04 18:39:59.413274 i | trying to lock by #2\n2018-10-04 18:39:59.414530 i | trying to lock by #1\n2018-10-04 18:39:59.414656 i | etcd change on key; \/foobar\/2a0966398d0677a2, type = put\n2018-10-04 18:39:59.414684 i | post-lock #2 (lease id = 2a0966398d0677a2) bullshit\n2018-10-04 18:39:59.415617 i | etcd change on key; \/foobar\/2a0966398d0677a4, type = put\n2018-10-04 18:40:10.239045 i | post-post-lock-#2-sleep. lease ttl = 1                       &lt;-- lock for #2 has ttl = 1 even after 10s\n2018-10-04 18:40:15.238871 i | failed to lock #1: context deadline exceeded                 &lt;-- lock for #1 fails after 15s<\/pre>\n<p>\u5982\u60a8\u6240\u89c1\uff0c#2 \u7684\u9501\u5373\u4f7f\u5728 15 \u79d2\u540e\u4ecd\u7136\u6709\u6548\u3002<\/p>\n<p>\u5728\u53e6\u4e00\u4e2a\u7ec8\u7aef\u4e2d\u8fd0\u884c <code>etcdctl_api=3 etcdctl watch --prefix=true \/foobar<\/code> \u6765\u89c2\u5bdf\u6309\u952e\u7684\u66f4\u6539\uff0c\u663e\u793a\u4ee5\u4e0b\u8f93\u51fa<\/p>\n<pre>PUT\n\/foobar\/2a0966398d0677a2\n\nPUT\n\/foobar\/2a0966398d0677a4\n\nDELETE\n\/foobar\/2a0966398d0677a4\n\nDELETE\n\/foobar\/2a0966398d0677a2<\/pre>\n<p>\u8fd9\u662f\u9884\u671f\u7684\u884c\u4e3a\u5417\uff1f\u6709\u6ca1\u6709\u529e\u6cd5\u5b9e\u73b0\u6211\u60f3\u8981\u7684\u76ee\u6807\uff1f<\/p>\n<p>p.s.\uff1a\u73b0\u5b9e\u4e16\u754c\u7684\u7528\u4f8b\u662f\u521b\u5efa\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u8be5\u7a0b\u5e8f\u5728\u591a\u4e2a\u5b9e\u4f8b\u4e2d\u8fd0\u884c\uff0c\u5e76\u4e14\u4e0d\u4f1a\u5728\u5d29\u6e83\u548c\/\u6216\u7ec8\u6b62\uff08sigkill\uff09\u65f6\u5728 etcd \u4e2d\u7559\u4e0b\u9501\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u7ecf\u8fc7\u4e00\u756a\u641c\u7d22\uff0c\u6211\u627e\u5230\u4e86\u8fd9\u79cd\u884c\u4e3a\u7684\u539f\u56e0\u3002\u4f1a\u8bdd\u4f7f\u79df\u7ea6\u4fdd\u6301\u6d3b\u52a8\u72b6\u6001\uff0c\u76f4\u5230\u51fa\u73b0\u9519\u8bef\u6216\u53d6\u6d88\u4e3a\u6b62\u3002<\/p>\n<p>\u6765\u81ea <code>session.go<\/code>\uff1a<\/p>\n<pre>...\n\/\/ keep the lease alive until client error or cancelled context\ngo func() {\n    defer close(donec)\n    for range keepAlive {\n        \/\/ eat messages until keep alive channel closes\n    }\n}()\n...<\/pre>\n<p>\u521b\u5efa\u4e92\u65a5\u4f53\u540e\u8c03\u7528 <code>session.orphan()<\/code> \u5c06\u963b\u6b62\u4f1a\u8bdd\u4fdd\u6301\u6d3b\u52a8\u72b6\u6001\u5e76\u8fbe\u5230\u6211\u7684\u76ee\u7684\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5e26 ttl \u7684 etcd \u4e92\u65a5\u9501\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#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-207177","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207177","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=207177"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207177\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}