分布式系统课程笔记——2.RPC和线程

第二篇它来了,老规矩,公众号回复【RPC】 获取完整PPT 。 

附代码

package main
import (
   "fmt"
   "sync"
)

//
// Several solutions to the crawler exercise from the Go tutorial
// https://tour.golang.org/concurrency/10
//
//
// Serial crawler
//
func Serial(url string, fetcher Fetcher, fetched map[string]bool) {
   if fetched[url] {
      return
   }
   fetched[url] = true
   urls, err := fetcher.Fetch(url)
   if err != nil {
      return
   }
   for _, u := range urls {
      Serial(u, fetcher, fetched)
   }
   return
}

//
// Concurrent crawler with shared state and Mutex
//
type fetchState struct {
   mu      sync.Mutex
   fetched map[string]bool
}

func ConcurrentMutex(url string, fetcher Fetcher, f *fetchState) {
   f.mu.Lock()
   already := f.fetched[url]
   f.fetched[url] = true
   f.mu.Unlock()

   if already {
      return
   }

   urls, err := fetcher.Fetch(url)
   if err != nil {
      return
   }
   var done sync.WaitGroup
   for _, u := range urls {
      done.Add(1)
      u2 := u
      go func() {
         defer done.Done()
         ConcurrentMutex(u2, fetcher, f)
      }()
      //go func(u string) {
      // defer done.Done()
      // ConcurrentMutex(u, fetcher, f)
      //}(u)
   }
   done.Wait()
   return
}

func makeState() *fetchState {
   f := &fetchState{}
   f.fetched = make(map[string]bool)
   return f
}

//
// Concurrent crawler with channels
//
func worker(url string, ch chan []string, fetcher Fetcher) {
   urls, err := fetcher.Fetch(url)
   if err != nil {
      ch <- []string{}
   } else {
      ch <- urls
   }
}

func master(ch chan []string, fetcher Fetcher) {
   n := 1
   fetched := make(map[string]bool)
   for urls := range ch {
      for _, u := range urls {
         if fetched[u] == false {
            fetched[u] = true
            n += 1
            go worker(u, ch, fetcher)
         }
      }
      n -= 1
      if n == 0 {
         break
      }
   }
}

func ConcurrentChannel(url string, fetcher Fetcher) {
   ch := make(chan []string)
   go func() {
      ch <- []string{url}
   }()
   master(ch, fetcher)
}

//
// main
//
func main() {
   fmt.Printf("=== Serial===\n")
   Serial("http://golang.org/", fetcher, make(map[string]bool))

   fmt.Printf("=== ConcurrentMutex ===\n")
   ConcurrentMutex("http://golang.org/", fetcher, makeState())

   fmt.Printf("=== ConcurrentChannel ===\n")
   ConcurrentChannel("http://golang.org/", fetcher)
}

//
// Fetcher
//
type Fetcher interface {
   // Fetch returns a slice of URLs found on the page.
   Fetch(url string) (urls []string, err error)
}

// fakeFetcher is Fetcher that returns canned results.
type fakeFetcher map[string]*fakeResult
type fakeResult struct {
   body string
   urls []string
}

func (f fakeFetcher) Fetch(url string) ([]string, error) {
   if res, ok := f[url]; ok {
      fmt.Printf("found:   %s\n", url)
      return res.urls, nil
   }
   fmt.Printf("missing: %s\n", url)
   return nil, fmt.Errorf("not found: %s", url)
}

// fetcher is a populated fakeFetcher.
var fetcher = fakeFetcher{
   "http://golang.org/": &fakeResult{
      "The Go Programming Language",
      []string{
         "http://golang.org/pkg/",
         "http://golang.org/cmd/",
      },
   },
   "http://golang.org/pkg/": &fakeResult{
      "Packages",
      []string{
         "http://golang.org/",
         "http://golang.org/cmd/",
         "http://golang.org/pkg/fmt/",
         "http://golang.org/pkg/os/",
      },
   },
   "http://golang.org/pkg/fmt/": &fakeResult{
      "Package fmt",
      []string{
         "http://golang.org/",
         "http://golang.org/pkg/",
      },
   },
   "http://golang.org/pkg/os/": &fakeResult{
      "Package os",
      []string{
         "http://golang.org/",
         "http://golang.org/pkg/",
      },
   },
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

麒思妙想

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值