Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go Golang select语句无法接收发送的值_Select_Go_Channel - Fatal编程技术网

Go Golang select语句无法接收发送的值

Go Golang select语句无法接收发送的值,select,go,channel,Select,Go,Channel,我是新手,尝试实现一个简单的负载平衡器,如本幻灯片所示: 完整代码: package main import ( "fmt" "time" "container/heap" ) type Request struct { fn func(*Worker) int c chan int } func requester(work chan <-Request) { c := make(chan int) work <-

我是新手,尝试实现一个简单的负载平衡器,如本幻灯片所示:

完整代码:

package main

import (
    "fmt"
    "time"
    "container/heap"
)

type Request struct {
    fn func(*Worker) int
    c  chan int
}

func requester(work chan <-Request) {
    c := make(chan int)
    work <- Request{workFn, c}
    result := <-c
    furtherProcess(result)
}

func workFn(w *Worker) int {
    time.Sleep(1000 * time.Millisecond)
    return w.index
}

func furtherProcess(result int) {
    fmt.Println(result)
}

type Worker struct {
    request chan Request
    pending int
    index   int
}

func (w *Worker) work(done chan *Worker) {
    for req := range w.request {
        req.c <- req.fn(w)
        fmt.Println("sending to done:", done)
        done <- w
        fmt.Println("sended to done")
    }
}

type Pool []*Worker

type Balancer struct {
    pool Pool
    done chan *Worker
}

func (b *Balancer) balance(work chan Request) {
    for {
        fmt.Println("selecting, done:", b.done)
        select {
        case req := <-work:
            b.dispatch(req)
        case w := <-b.done:
            fmt.Println("completed")
            b.completed(w)
        }
    }
}

func (p Pool) Len() int {
    return len(p)
}

func (p Pool) Less(i, j int) bool {
    return p[i].pending < p[j].pending
}

func (p Pool) Swap(i, j int) {
    p[i], p[j] = p[j], p[i]
}

func (p *Pool) Push(x interface{}) {
    *p = append(*p, x.(*Worker))
}

func (p *Pool) Pop() interface{} {
    old := *p
    n := len(old)
    x := old[n - 1]
    *p = old[0 : n - 1]
    return x
}

func (b *Balancer) dispatch(req Request) {
    w := heap.Pop(&b.pool).(*Worker)
    w.request <- req
    w.pending++
    heap.Push(&b.pool, w)
    fmt.Println("dispatched to worker", w.index)
}

func (b *Balancer) completed(w *Worker) {
    w.pending--
    heap.Remove(&b.pool, w.index)
    heap.Push(&b.pool, w)
}

func Run() {
    NumWorkers := 4
    req := make(chan Request)
    done := make(chan *Worker)
    b := Balancer{make([]*Worker, NumWorkers), done}
    for i := 0; i < NumWorkers; i++ {
        w := Worker{make(chan Request), 0, i}
        b.pool[i] = &w
        go w.work(done)
    }
    go b.balance(req)
    for i := 0; i < NumWorkers * 4; i++ {
        go requester(req)
    }
    time.Sleep(200000 * time.Millisecond)
}

func main() {
    Run()
}
如您所见,它正在选择on并发送到同一管道(完成:0xc0820082a0),但select没有收到发送的值,并且一直处于阻塞状态。这怎么会发生?上面的代码有什么问题?谢谢

使用您可以看到您的所有工作人员在
done上被阻止使用您可以看到您的所有工作人员在
done上被阻止
selecting, done: 0xc0820082a0
dispatched to worker 0
selecting, done: 0xc0820082a0
dispatched to worker 3
selecting, done: 0xc0820082a0
dispatched to worker 2
selecting, done: 0xc0820082a0
dispatched to worker 1
selecting, done: 0xc0820082a0
sending to done: 0xc0820082a0
sending to done: 0xc0820082a0
3
sending to done: 0xc0820082a0
2
1
0
sending to done: 0xc0820082a0