Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

笔记:Google I/O 2012 - Go Concurrency Patterns #7

Open
wisecsj opened this issue Jan 8, 2019 · 1 comment
Open

笔记:Google I/O 2012 - Go Concurrency Patterns #7

wisecsj opened this issue Jan 8, 2019 · 1 comment

Comments

@wisecsj
Copy link
Owner

wisecsj commented Jan 8, 2019

Google I/O 2012 - Go Concurrency Patterns

Rob Pike:

Go was designed for building system software

  1. Go runtime holds waiting FIFO queue for receivers (goroutines ready to receive on the particular channel)

  2. 多个goroutine产生 channel value 最终汇聚到某个 channel (fan-in)

  3. 某个channel的值分发到多个goroutine (worker pattern)

Patterns

  1. Generator:funciton that returns a channel
func boring(msg string) <-chan string { // Returns receive-only channel of strings.
    c := make(chan string)
    go func() { // We launch the goroutine from inside the function.
        for i := 0; ; i++ {
            c <- fmt.Sprintf("%s %d", msg, i)
            time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)
        }
    }()
    return c // Return the channel to the caller.
}
  1. Timeout using select
func main() {
    c := boring("Joe")
    for {
        select {
        case s := <-c:
            fmt.Println(s)
        case <-time.After(1 * time.Second):
            fmt.Println("You're too slow.")
            return
        }
    }
}
  1. Timeout for whole conversation using select

    Create the timer once, outside the loop, to time out the entire conversation.

func main() {
    c := boring("Joe")
    timeout := time.After(5 * time.Second)
    for {
        select {
        case s := <-c:
            fmt.Println(s)
        case <-timeout:
            fmt.Println("You talk too much.")
            return
        }
    }
}
  1. Daisy-chain
    chain
func f(left, right chan int) {
    left <- 1 + <-right
}

func main() {
    const n = 10000
    leftmost := make(chan int)
    right := leftmost
    left := leftmost
    for i := 0; i < n; i++ {
        right = make(chan int)
        go f(left, right)
        left = right
    }
    go func(c chan int) { c <- 1 }(right)
    fmt.Println(<-leftmost)
}

Google Search ,Example

  1. 同步阻塞方式
  2. fan-in 方式
  3. fan-in + timeout
  4. avoid discarding results from slow servers.Send requests to multiple replicas, and use the first response(对提供相同服务的多个服务器发送请求,取最快的那个)

Links

links

@wisecsj
Copy link
Owner Author

wisecsj commented Jan 8, 2019

talk slide

@wisecsj wisecsj pinned this issue Mar 3, 2019
@wisecsj wisecsj unpinned this issue Mar 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant