1"use strict";(self.webpackChunklayotto_docusaurus=self.webpackChunklayotto_docusaurus||[]).push([[9402],{1432:(e,n,o)=>{o.r(n),o.d(n,{assets:()=>l,contentTitle:()=>s,default:()=>u,frontMatter:()=>i,metadata:()=>c,toc:()=>d});var t=o(4848),r=o(8453);const i={},s="Distributed Lock API",c={id:"building_blocks/lock/reference",title:"Distributed Lock API",description:"\u4ec0\u4e48\u662f\u5206\u5e03\u5f0f\u9501 API",source:"@site/docs/building_blocks/lock/reference.md",sourceDirName:"building_blocks/lock",slug:"/building_blocks/lock/reference",permalink:"/layotto/docs/building_blocks/lock/reference",draft:!1,unlisted:!1,editUrl:"https://github.com/mosn/layotto/edit/main/docs/docs/building_blocks/lock/reference.md",tags:[],version:"current",frontMatter:{},sidebar:"mySidebar",previous:{title:"Sequencer API",permalink:"/layotto/docs/building_blocks/sequencer/reference"},next:{title:"\u53d1\u5e03/\u8ba2\u9605 API",permalink:"/layotto/docs/building_blocks/pubsub/reference"}},l={},d=[{value:"\u4ec0\u4e48\u662f\u5206\u5e03\u5f0f\u9501 API",id:"\u4ec0\u4e48\u662f\u5206\u5e03\u5f0f\u9501-api",level:2},{value:"\u5982\u4f55\u4f7f\u7528\u5206\u5e03\u5f0f\u9501 API",id:"\u5982\u4f55\u4f7f\u7528\u5206\u5e03\u5f0f\u9501-api",level:2},{value:"\u4f7f\u7528\u793a\u4f8b",id:"\u4f7f\u7528\u793a\u4f8b",level:3},{value:"TryLock",id:"trylock",level:3},{value:"Unlock",id:"unlock",level:3},{value:"\u4e3a\u4ec0\u4e48\u5206\u5e03\u5f0f\u9501 API\u88ab\u8bbe\u8ba1\u6210\u8fd9\u6837",id:"\u4e3a\u4ec0\u4e48\u5206\u5e03\u5f0f\u9501-api\u88ab\u8bbe\u8ba1\u6210\u8fd9\u6837",level:2}];function a(e){const n={a:"a",code:"code",h1:"h1",h2:"h2",h3:"h3",p:"p",pre:"pre",strong:"strong",...(0,r.R)(),...e.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(n.h1,{id:"distributed-lock-api",children:"Distributed Lock API"}),"\n",(0,t.jsx)(n.h2,{id:"\u4ec0\u4e48\u662f\u5206\u5e03\u5f0f\u9501-api",children:"\u4ec0\u4e48\u662f\u5206\u5e03\u5f0f\u9501 API"}),"\n",(0,t.jsx)(n.p,{children:"\u5206\u5e03\u5f0f\u9501 API\u57fa\u4e8e\u67d0\u79cd\u5b58\u50a8\u7cfb\u7edf\uff08\u6bd4\u5982Etcd,Zookeeper)\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u7b80\u5355\u3001\u6613\u7528\u7684\u5206\u5e03\u5f0f\u9501API\u63a5\u53e3\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u7528\u8be5API\u83b7\u53d6\u9501\u3001\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\u514d\u53d7\u5e76\u53d1\u95ee\u9898\u7684\u70e6\u6270\u3002"}),"\n",(0,t.jsx)(n.h2,{id:"\u5982\u4f55\u4f7f\u7528\u5206\u5e03\u5f0f\u9501-api",children:"\u5982\u4f55\u4f7f\u7528\u5206\u5e03\u5f0f\u9501 API"}),"\n",(0,t.jsxs)(n.p,{children:["\u60a8\u53ef\u4ee5\u901a\u8fc7grpc\u8c03\u7528\u5206\u5e03\u5f0f\u9501 API\uff0c\u63a5\u53e3\u5b9a\u4e49\u5728",(0,t.jsx)(n.a,{href:"https://github.com/mosn/layotto/blob/main/spec/proto/runtime/v1/runtime.proto",children:"runtime.proto"})," \u4e2d\u3002"]}),"\n",(0,t.jsxs)(n.p,{children:["\u4f7f\u7528\u524d\u9700\u8981\u5148\u5bf9\u7ec4\u4ef6\u8fdb\u884c\u914d\u7f6e\uff0c\u8be6\u7ec6\u7684\u914d\u7f6e\u8bf4\u660e\u89c1",(0,t.jsx)(n.a,{href:"/layotto/docs/component_specs/lock/common",children:"\u5206\u5e03\u5f0f\u9501\u7ec4\u4ef6\u6587\u6863"})]}),"\n",(0,t.jsx)(n.h3,{id:"\u4f7f\u7528\u793a\u4f8b",children:"\u4f7f\u7528\u793a\u4f8b"}),"\n",(0,t.jsxs)(n.p,{children:["Layotto client sdk\u5c01\u88c5\u4e86grpc\u8c03\u7528\u7684\u903b\u8f91\uff0c\u4f7f\u7528sdk\u8c03\u7528\u5206\u5e03\u5f0f\u9501 API\u7684\u793a\u4f8b\u53ef\u4ee5\u53c2\u8003",(0,t.jsx)(n.a,{href:"/layotto/docs/start/lock/start",children:"\u5feb\u901f\u5f00\u59cb\uff1a\u4f7f\u7528\u5206\u5e03\u5f0f\u9501API"})]}),"\n",(0,t.jsx)(n.h3,{id:"trylock",children:"TryLock"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-protobuf",children:"// A non-blocking method trying to get a lock with ttl.\nrpc TryLock(TryLockRequest)returns (TryLockResponse) {}\n\nmessage TryLockRequest {\n // Required. The lock store name,e.g. `redis`.\n string store_name = 1;\n // Required. resource_id is the lock key. e.g. `order_id_111`\n // It stands for \"which resource I want to protect\"\n string resource_id = 2;\n \n // Required. lock_owner indicate the identifier of lock owner.\n // You can generate a uuid as lock_owner.For example,in golang:\n //\n // req.LockOwner = uuid.New().String()\n //\n // This field is per request,not per process,so it is different for each request,\n // which aims to prevent multi-thread in the same process trying the same lock concurrently.\n //\n // The reason why we don't make it automatically generated is:\n // 1. If it is automatically generated,there must be a 'my_lock_owner_id' field in the response.\n // This name is so weird that we think it is inappropriate to put it into the api spec\n // 2. If we change the field 'my_lock_owner_id' in the response to 'lock_owner',which means the current lock owner of this lock,\n // we find that in some lock services users can't get the current lock owner.Actually users don't need it at all.\n // 3. When reentrant lock is needed,the existing lock_owner is required to identify client and check \"whether this client can reenter this lock\".\n // So this field in the request shouldn't be removed.\n string lock_owner = 3;\n \n // Required. expire is the time before expire.The time unit is second.\n int32 expire = 4;\n}\n\n\nmessage TryLockResponse {\n bool success = 1;\n}\n"})}),"\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.strong,{children:"Q: expire\u5b57\u6bb5\u7684\u65f6\u95f4\u5355\u4f4d\uff1f"})}),"\n",(0,t.jsx)(n.p,{children:"A: \u79d2\u3002"}),"\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.strong,{children:"Q: \u5982\u679c\u591a\u4e2a\u5ba2\u6237\u7aef\u4f20\u76f8\u540c\u7684LockOwner\u4f1a\u600e\u4e48\u6837?"})}),"\n",(0,t.jsx)(n.p,{children:"case 1. \u4e24\u4e2a\u5ba2\u6237\u7aefapp-id\u4e0d\u4e00\u6837\uff0c\u4f20\u7684LockOwner\u76f8\u540c\uff0c\u4e0d\u4f1a\u53d1\u751f\u51b2\u7a81"}),"\n",(0,t.jsx)(n.p,{children:"case 2. \u4e24\u4e2a\u5ba2\u6237\u7aefapp-id\u4e00\u6837\uff0c\u4f20\u7684LockOwner\u76f8\u540c\uff0c\u4f1a\u53d1\u751f\u51b2\u7a81\u3002\u53ef\u80fd\u4f1a\u51fa\u73b0\u62a2\u5230\u522b\u4eba\u7684\u9501\u3001\u91ca\u653e\u522b\u4eba\u7684\u9501\u7b49\u5f02\u5e38\u60c5\u51b5"}),"\n",(0,t.jsx)(n.p,{children:"\u56e0\u6b64\u7528\u6237\u9700\u8981\u4fdd\u8bc1LockOwner\u7684\u552f\u4e00\u6027\uff0c\u4f8b\u5982\u7ed9\u6bcf\u4e2a\u8bf7\u6c42\u5206\u914d\u4e00\u4e2aUUID,golang\u5199\u6cd5\uff1a"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-go",children:'import "github.com/google/uuid"\n//...\nreq.LockOwner = uuid.New().String()\n'})}),"\n",(0,t.jsx)(n.h3,{id:"unlock",children:"Unlock"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-protobuf",children:" rpc Unlock(UnlockRequest)returns (UnlockResponse) {}\n"})}),"\n",(0,t.jsxs)(n.p,{children:["\u4e3a\u907f\u514d\u6587\u6863\u548c\u4ee3\u7801\u4e0d\u4e00\u81f4\uff0c\u8be6\u7ec6\u5165\u53c2\u548c\u8fd4\u56de\u503c\u8bf7\u53c2\u8003",(0,t.jsx)(n.a,{href:"https://github.com/mosn/layotto/blob/main/spec/proto/runtime/v1/runtime.proto",children:"proto\u6587\u4ef6"})]}),"\n",(0,t.jsx)(n.h2,{id:"\u4e3a\u4ec0\u4e48\u5206\u5e03\u5f0f\u9501-api\u88ab\u8bbe\u8ba1\u62
110\u8fd9\u6837",children:"\u4e3a\u4ec0\u4e48\u5206\u5e03\u5f0f\u9501 API\u88ab\u8bbe\u8ba1\u6210\u8fd9\u6837"}),"\n",(0,t.jsxs)(n.p,{children:["\u5982\u679c\u60a8\u5bf9\u5b9e\u73b0\u539f\u7406\u3001\u8bbe\u8ba1\u903b\u8f91\u611f\u5174\u8da3\uff0c\u53ef\u4ee5\u67e5\u9605",(0,t.jsx)(n.a,{href:"/docs/design/lock/lock-api-design",children:"\u5206\u5e03\u5f0f\u9501API\u8bbe\u8ba1\u6587\u6863"})]})]})}function u(e={}){const{wrapper:n}={...(0,r.R)(),...e.components};return n?(0,t.jsx)(n,{...e,children:(0,t.jsx)(a,{...e})}):a(e)}},8453:(e,n,o)=>{o.d(n,{R:()=>s,x:()=>c});var t=o(6540);const r={},i=t.createContext(r);function s(e){const n=t.useContext(i);return t.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function c(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:s(e.components),t.createElement(i.Provider,{value:n},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.