{"id":158688,"date":"2020-09-13T13:59:46","date_gmt":"2020-09-13T05:59:46","guid":{"rendered":"http:\/\/4563.org\/?p=158688"},"modified":"2020-09-13T13:59:46","modified_gmt":"2020-09-13T05:59:46","slug":"%e9%98%b2%e6%ad%a2%e7%bc%93%e5%ad%98%e5%87%bb%e7%a9%bf%e4%b9%8b%e8%bf%9b%e7%a8%8b%e5%86%85%e5%85%b1%e4%ba%ab%e8%b0%83%e7%94%a8","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=158688","title":{"rendered":"\u9632\u6b62\u7f13\u5b58\u51fb\u7a7f\u4e4b\u8fdb\u7a0b\u5185\u5171\u4eab\u8c03\u7528"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u9632\u6b62\u7f13\u5b58\u51fb\u7a7f\u4e4b\u8fdb\u7a0b\u5185\u5171\u4eab\u8c03\u7528               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : kevinwan <\/span>  <span><i><\/i> 5<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>go-zero \u5fae\u670d\u52a1\u6846\u67b6\u4e2d\u63d0\u4f9b\u4e86\u8bb8\u591a\u5f00\u7bb1\u5373\u7528\u7684\u5de5\u5177\uff0c\u597d\u7684\u5de5\u5177\u4e0d\u4ec5\u80fd\u63d0\u5347\u670d\u52a1\u7684\u6027\u80fd\u800c\u4e14\u8fd8\u80fd\u63d0\u5347\u4ee3\u7801\u7684\u9c81\u68d2\u6027\u907f\u514d\u51fa\u9519\uff0c\u5b9e\u73b0\u4ee3\u7801\u98ce\u683c\u7684\u7edf\u4e00\u65b9\u4fbf\u4ed6\u4eba\u9605\u8bfb\u7b49\u7b49\u3002<\/p>\n<p>\u672c\u6587\u4e3b\u8981\u8bb2\u8ff0\u8fdb\u7a0b\u5185\u5171\u4eab\u8c03\u7528\u795e\u5668 SharedCalls<\/p>\n<h2>\u4f7f\u7528\u573a\u666f<\/h2>\n<p>\u5e76\u53d1\u573a\u666f\u4e0b\uff0c\u53ef\u80fd\u4f1a\u6709\u591a\u4e2a\u7ebf\u7a0b\uff08\u534f\u7a0b\uff09\u540c\u65f6\u8bf7\u6c42\u540c\u4e00\u4efd\u8d44\u6e90\uff0c\u5982\u679c\u6bcf\u4e2a\u8bf7\u6c42\u90fd\u8981\u8d70\u4e00\u904d\u8d44\u6e90\u7684\u8bf7\u6c42\u8fc7\u7a0b\uff0c\u9664\u4e86\u6bd4\u8f83\u4f4e\u6548\u4e4b\u5916\uff0c\u8fd8\u4f1a\u5bf9\u8d44\u6e90\u670d\u52a1\u9020\u6210\u5e76\u53d1\u7684\u538b\u529b\u3002\u4e3e\u4e00\u4e2a\u5177\u4f53\u4f8b\u5b50\uff0c\u6bd4\u5982\u7f13\u5b58\u5931\u6548\uff0c\u591a\u4e2a\u8bf7\u6c42\u540c\u65f6\u5230\u8fbe\u67d0\u670d\u52a1\u8bf7\u6c42\u67d0\u8d44\u6e90\uff0c\u8be5\u8d44\u6e90\u5728\u7f13\u5b58\u4e2d\u5df2\u7ecf\u5931\u6548\uff0c\u6b64\u65f6\u8fd9\u4e9b\u8bf7\u6c42\u4f1a\u7ee7\u7eed\u8bbf\u95ee DB \u505a\u67e5\u8be2\uff0c\u4f1a\u5f15\u8d77\u6570\u636e\u5e93\u538b\u529b\u77ac\u95f4\u589e\u5927\u3002\u800c\u4f7f\u7528 SharedCalls \u53ef\u4ee5\u4f7f\u5f97\u540c\u65f6\u591a\u4e2a\u8bf7\u6c42\u53ea\u9700\u8981\u53d1\u8d77\u4e00\u6b21\u62ff\u7ed3\u679c\u7684\u8c03\u7528\uff0c\u5176\u4ed6\u8bf7\u6c42&#8221;\u5750\u4eab\u5176\u6210&#8221;\uff0c\u8fd9\u79cd\u8bbe\u8ba1\u6709\u6548\u51cf\u5c11\u4e86\u8d44\u6e90\u670d\u52a1\u7684\u5e76\u53d1\u538b\u529b\uff0c\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u7f13\u5b58\u51fb\u7a7f\u3002<\/p>\n<p>\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\uff0c\u5f53\u67d0\u4e2a\u70ed\u70b9 key \u7f13\u5b58\u5931\u6548\u540e\uff0c\u591a\u4e2a\u8bf7\u6c42\u4f1a\u540c\u65f6\u4ece\u6570\u636e\u5e93\u52a0\u8f7d\u8be5\u8d44\u6e90\uff0c\u5e76\u4fdd\u5b58\u5230\u7f13\u5b58\uff0c\u5982\u679c\u4e0d\u505a\u9632\u8303\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u5e93\u88ab\u76f4\u63a5\u6253\u6b7b\u3002\u9488\u5bf9\u8fd9\u79cd\u573a\u666f\uff0cgo-zero \u6846\u67b6\u4e2d\u5df2\u7ecf\u63d0\u4f9b\u4e86\u5b9e\u73b0\uff0c\u5177\u4f53\u53ef\u53c2\u770b sqlc \u548c mongoc \u7b49\u5b9e\u73b0\u4ee3\u7801\u3002<\/p>\n<p>\u4e3a\u4e86\u7b80\u5316\u6f14\u793a\u4ee3\u7801\uff0c\u6211\u4eec\u901a\u8fc7\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u53bb\u83b7\u53d6\u4e00\u4e2a id \u6765\u6a21\u62df\u7f13\u5b58\u7684\u573a\u666f\u3002\u5982\u4e0b\uff1a<\/p>\n<pre><code>func main() {   const round = 5   var wg sync.WaitGroup   barrier := syncx.NewSharedCalls()    wg.Add(round)   for i := 0; i &lt; round; i++ {     \/\/ \u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6267\u884c     go func() {       defer wg.Done()       \/\/ \u53ef\u4ee5\u770b\u5230\uff0c\u591a\u4e2a\u7ebf\u7a0b\u5728\u540c\u4e00\u4e2a key \u4e0a\u53bb\u8bf7\u6c42\u8d44\u6e90\uff0c\u83b7\u53d6\u8d44\u6e90\u7684\u5b9e\u9645\u51fd\u6570\u53ea\u4f1a\u88ab\u8c03\u7528\u4e00\u6b21       val, err := barrier.Do(\"once\", func() (interface{}, error) {         \/\/ sleep 1 \u79d2\uff0c\u4e3a\u4e86\u8ba9\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u53d6 once \u8fd9\u4e2a key \u4e0a\u7684\u6570\u636e         time.Sleep(time.Second)         \/\/ \u751f\u6210\u4e86\u4e00\u4e2a\u968f\u673a\u7684 id         return stringx.RandId(), nil       })       if err != nil {         fmt.Println(err)       } else {         fmt.Println(val)       }     }()   }    wg.Wait() } <\/code><\/pre>\n<p>\u8fd0\u884c\uff0c\u6253\u5370\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre><code>837c577b1008a0db 837c577b1008a0db 837c577b1008a0db 837c577b1008a0db 837c577b1008a0db <\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u51fa\uff0c\u53ea\u8981\u662f\u540c\u4e00\u4e2a key \u4e0a\u7684\u540c\u65f6\u53d1\u8d77\u7684\u8bf7\u6c42\uff0c\u90fd\u4f1a\u5171\u4eab\u540c\u4e00\u4e2a\u7ed3\u679c\uff0c\u5bf9\u83b7\u53d6 DB \u6570\u636e\u8fdb\u7f13\u5b58\u7b49\u573a\u666f\u7279\u522b\u6709\u7528\uff0c\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u7f13\u5b58\u51fb\u7a7f\u3002<\/p>\n<h2>\u5173\u952e\u6e90\u7801\u5206\u6790<\/h2>\n<ul>\n<li>\n<p>SharedCalls interface \u63d0\u4f9b\u4e86 Do \u548c DoEx \u4e24\u79cd\u65b9\u6cd5\u7684\u62bd\u8c61<\/p>\n<pre><code>type SharedCalls interface {   Do(key string, fn func() (interface{}, error)) (interface{}, error)   DoEx(key string, fn func() (interface{}, error)) (interface{}, bool, error) } <\/code><\/pre>\n<\/li>\n<li>\n<p>SharedCalls interface \u7684\u5177\u4f53\u5b9e\u73b0 sharedGroup<\/p>\n<pre><code>\/\/ call \u4ee3\u8868\u5bf9\u6307\u5b9a\u8d44\u6e90\u7684\u4e00\u6b21\u8bf7\u6c42 type call struct {   wg  sync.WaitGroup  \/\/ \u7528\u4e8e\u534f\u8c03\u5404\u4e2a\u8bf7\u6c42 goroutine \u4e4b\u95f4\u7684\u8d44\u6e90\u5171\u4eab   val interface{}     \/\/ \u7528\u4e8e\u4fdd\u5b58\u8bf7\u6c42\u7684\u8fd4\u56de\u503c   err error           \/\/ \u7528\u4e8e\u4fdd\u5b58\u8bf7\u6c42\u8fc7\u7a0b\u4e2d\u53d1\u751f\u7684\u9519\u8bef }  type sharedGroup struct {   calls map[string]*call   lock  sync.Mutex } <\/code><\/pre>\n<\/li>\n<li>\n<p>sharedGroup \u7684 Do \u65b9\u6cd5<\/p>\n<ul>\n<li>key \u53c2\u6570\uff1a\u53ef\u4ee5\u7406\u89e3\u4e3a\u8d44\u6e90\u7684\u552f\u4e00\u6807\u8bc6\u3002<\/li>\n<li>fn \u53c2\u6570\uff1a\u771f\u6b63\u83b7\u53d6\u8d44\u6e90\u7684\u65b9\u6cd5\u3002<\/li>\n<li>\u5904\u7406\u8fc7\u7a0b\u5206\u6790\uff1a<\/li>\n<\/ul>\n<pre><code>\/\/ \u5f53\u591a\u4e2a\u8bf7\u6c42\u540c\u65f6\u4f7f\u7528 Do \u65b9\u6cd5\u8bf7\u6c42\u8d44\u6e90\u65f6 func (g *sharedGroup) Do(key string, fn func() (interface{}, error)) (interface{}, error) {   \/\/ \u5148\u7533\u8bf7\u52a0\u9501   g.lock.Lock()    \/\/ \u6839\u636e key\uff0c\u83b7\u53d6\u5bf9\u5e94\u7684 call \u7ed3\u679c,\u5e76\u7528\u53d8\u91cf c \u4fdd\u5b58   if c, ok := g.calls[key]; ok {     \/\/ \u62ff\u5230 call \u4ee5\u540e\uff0c\u91ca\u653e\u9501\uff0c\u6b64\u5904 call \u53ef\u80fd\u8fd8\u6ca1\u6709\u5b9e\u9645\u6570\u636e\uff0c\u53ea\u662f\u4e00\u4e2a\u7a7a\u7684\u5185\u5b58\u5360\u4f4d     g.lock.Unlock()     \/\/ \u8c03\u7528 wg.Wait\uff0c\u5224\u65ad\u662f\u5426\u6709\u5176\u4ed6 goroutine \u6b63\u5728\u7533\u8bf7\u8d44\u6e90\uff0c\u5982\u679c\u963b\u585e\uff0c\u8bf4\u660e\u6709\u5176\u4ed6 goroutine \u6b63\u5728\u83b7\u53d6\u8d44\u6e90     c.wg.Wait()     \/\/ \u5f53 wg.Wait \u4e0d\u518d\u963b\u585e\uff0c\u8868\u793a\u8d44\u6e90\u83b7\u53d6\u5df2\u7ecf\u7ed3\u675f\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u7ed3\u679c     return c.val, c.err   }    \/\/ \u6ca1\u6709\u62ff\u5230\u7ed3\u679c\uff0c\u5219\u8c03\u7528 makeCall \u65b9\u6cd5\u53bb\u83b7\u53d6\u8d44\u6e90\uff0c\u6ce8\u610f\u6b64\u5904\u4ecd\u7136\u662f\u9501\u4f4f\u7684\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u53ea\u6709\u4e00\u4e2a goroutine \u53ef\u4ee5\u8c03\u7528 makecall   c := g.makeCall(key, fn)   \/\/ \u8fd4\u56de\u8c03\u7528\u7ed3\u679c   return c.val, c.err } <\/code><\/pre>\n<\/li>\n<li>\n<p>sharedGroup \u7684 DoEx \u65b9\u6cd5<\/p>\n<ul>\n<li>\u548c Do \u65b9\u6cd5\u7c7b\u4f3c\uff0c\u53ea\u662f\u8fd4\u56de\u503c\u4e2d\u589e\u52a0\u4e86\u5e03\u5c14\u503c\u8868\u793a\u503c\u662f\u8c03\u7528 makeCall \u65b9\u6cd5\u76f4\u63a5\u83b7\u53d6\u7684\uff0c\u8fd8\u662f\u53d6\u7684\u5171\u4eab\u6210\u679c<\/li>\n<\/ul>\n<pre><code>func (g *sharedGroup) DoEx(key string, fn func() (interface{}, error)) (val interface{}, fresh bool, err error) {   g.lock.Lock()   if c, ok := g.calls[key]; ok {     g.lock.Unlock()     c.wg.Wait()     return c.val, false, c.err   }    c := g.makeCall(key, fn)   return c.val, true, c.err } <\/code><\/pre>\n<\/li>\n<li>\n<p>sharedGroup \u7684 makeCall \u65b9\u6cd5<\/p>\n<ul>\n<li>\u8be5\u65b9\u6cd5\u7531 Do \u548c DoEx \u65b9\u6cd5\u8c03\u7528\uff0c\u662f\u771f\u6b63\u53d1\u8d77\u8d44\u6e90\u8bf7\u6c42\u7684\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<pre><code>\/\/ \u8fdb\u5165 makeCall \u7684\u4e00\u5b9a\u53ea\u6709\u4e00\u4e2a goroutine\uff0c\u56e0\u4e3a\u8981\u62ff\u9501\u9501\u4f4f\u7684 func (g *sharedGroup) makeCall(key string, fn func() (interface{}, error)) *call {   \/\/ \u521b\u5efa call \u7ed3\u6784\uff0c\u7528\u4e8e\u4fdd\u5b58\u672c\u6b21\u8bf7\u6c42\u7684\u7ed3\u679c   c := new(call)   \/\/ wg \u52a0 1\uff0c\u7528\u4e8e\u901a\u77e5\u5176\u4ed6\u8bf7\u6c42\u8d44\u6e90\u7684 goroutine \u7b49\u5f85\u672c\u6b21\u8d44\u6e90\u83b7\u53d6\u7684\u7ed3\u675f   c.wg.Add(1)   \/\/ \u5c06\u7528\u4e8e\u4fdd\u5b58\u7ed3\u679c\u7684 call \u653e\u5165 map \u4e2d\uff0c\u4ee5\u4f9b\u5176\u4ed6 goroutine \u83b7\u53d6   g.calls[key] = c   \/\/ \u91ca\u653e\u9501\uff0c\u8fd9\u6837\u5176\u4ed6\u8bf7\u6c42\u7684 goroutine \u624d\u80fd\u83b7\u53d6 call \u7684\u5185\u5b58\u5360\u4f4d   g.lock.Unlock()    defer func() {     \/\/ delete key first, done later. can't reverse the order, because if reverse,     \/\/ another Do call might wg.Wait() without get notified with wg.Done()     g.lock.Lock()     delete(g.calls, key)     g.lock.Unlock()      \/\/ \u8c03\u7528 wg.Done\uff0c\u901a\u77e5\u5176\u4ed6 goroutine \u53ef\u4ee5\u8fd4\u56de\u7ed3\u679c\uff0c\u8fd9\u6837\u672c\u6279\u6b21\u6240\u6709\u8bf7\u6c42\u5b8c\u6210\u7ed3\u679c\u7684\u5171\u4eab     c.wg.Done()   }()    \/\/ \u8c03\u7528 fn \u65b9\u6cd5\uff0c\u5c06\u7ed3\u679c\u586b\u5165\u53d8\u91cf c \u4e2d   c.val, c.err = fn()   return c } <\/code><\/pre>\n<\/li>\n<\/ul>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u4e86 go-zero \u6846\u67b6\u4e2d\u7684 SharedCalls \u5de5\u5177\uff0c\u5bf9\u5176\u5e94\u7528\u573a\u666f\u548c\u5173\u952e\u4ee3\u7801\u505a\u4e86\u7b80\u5355\u7684\u68b3\u7406\uff0c\u5e0c\u671b\u672c\u7bc7\u6587\u7ae0\u80fd\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e9b\u6536\u83b7\u3002<\/p>\n<h2>\u9879\u76ee\u5730\u5740<\/h2>\n<p>https:\/\/github.com\/tal-tech\/go-zero<\/p>\n<h2>\u5fae\u4fe1\u4ea4\u6d41\u7fa4<\/h2>\n<p> <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/09\/20200923_5f6b6ddd088ca.jpg\" alt=\"\u9632\u6b62\u7f13\u5b58\u51fb\u7a7f\u4e4b\u8fdb\u7a0b\u5185\u5171\u4eab\u8c03\u7528\" \/>  <\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>1<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3403284\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : woniuge <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/pkg.go.dev\/golang.org\/x\/sync\/singleflight                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u9632\u6b62\u7f13\u5b58\u51fb\u7a7f\u4e4b\u8fdb\u7a0b\u5185\u5171\u4eab\u8c03\u7528 \u8cc7\u6df1&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/158688"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=158688"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/158688\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=158688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=158688"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=158688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}