Go 通道

xiongkun01 · · 526 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

无缓冲区通道

通过make(chan xxx)创建,没有设置缓冲区大小,这种类型的通道会在两种情况下导致阻塞:

  1. 通道中无数据,但执行通道读操作。
  2. 执行通道写操作,但是无协程从通道中读取数据。
// 情况1
func ReadNoDataFromNoBufCh() {
    noBufCh := make(chan int)

    <-noBufCh
    println("read from no buffer channel success")
}

// 情况2
func WriteNoBufCh() {
    ch := make(chan int)

    ch <- 1
    println("write success no block")
}

有缓冲区通道

通过make(chan xxx, int)创建,并设置了缓冲区大小。如果缓存区未满,则写入后会立即返回。如果缓冲区有数据,读取后也会立即返回。这种类型的通道会在两种情况下导致阻塞:

  1. 缓冲区无数据,但执行了通道读操作。
  2. 缓冲区已满,但执行了通道写操作。
//情况1
func ReadNoDataFromBufCh() {
    bufCh := make(chan int, 1)

    <-bufCh // 缓冲区无数据,读取失败
    println("read from buffer channel success")
}

//情况2
func WriteBufCh() {
    ch := make(chan int, 1)

    ch <- 1 // 有缓冲区,写入后立即返回
    ch <- 2 // 缓冲区已满,无法写入
    println("write success no block")
}

Select + Channel

select会随机选择一个未阻塞的通道,如果都阻塞了,则等待直到有一个通道不阻塞。我们也可以通过default分支来实现默认的无阻塞操作,具体代码如下:

func ReadNoDataFromNoBuffChWithSelect() {
    noBufCh := make(chan int)

    if v, err := ReadWithSelect(noBufCh); err != nil {
        fmt.Println(err)
    } else {
        fmt.Printf("read: %d\n", v)
    }
}

func ReadNoDataFromBufChWithSelect() {
    bufCh := make(chan int, 1)

    if v, err := ReadWithSelect(bufCh); err != nil {
        fmt.Println(err)
    } else {
        fmt.Printf("read: %d\n", v)
    }
}

func ReadWithSelect(ch chan int) (int, error) {
    select {
    case x := <-ch:
        return x, nil
    default:
        return 0, errors.New("channel has no data")
    }
}

func WriteNoBufChWithSelect() {
    ch := make(chan int)
    if err := WriteChWithSelect(ch); err != nil {
        fmt.Println(err)
    } else {
        println("success")
    }
}

func WriteBufChButFullWithSelect() {
    ch := make(chan int, 1)
    ch <- 100
    if err := WriteChWithSelect(ch); err != nil {
        fmt.Println(err)
    } else {
        println("success")
    }
}

func WriteChWithSelect(ch chan int) error {
    select {
    case ch <- 1:
        return nil
    default:
        return errors.New("channel blocked, can not write")
    }
}

定时阻塞

使用default实现的无阻塞读写有一个缺点:当通道不可读写时,会立即返回。但是实际场景中,我们可能需要等待一段时间后再返回,使用定时器替代default可以解决这个问题,給通道一定的容忍时间,代码如下:

func ReadWithSelectAndTimer(ch chan int) (int, error) {
    timeout := time.NewTimer(time.Microsecond * 500)

    select {
    case x := <-ch:
        return x, nil
    case <-timeout.C: // 如果500ms内无法读写,就即刻返回
        return 0, errors.New("read time out")
    }
}

func WriteChWithSelectAndTimer(ch chan int) error {
    timeout := time.NewTimer(time.Microsecond * 500)

    select {
    case ch <- 1:
        return nil
    case <-timeout.C: // 如果500ms内无法读写,就即刻返回
        return errors.New("write time out")
    }
}

注意:如果select写在循环语句当中,并且也用了定时通道,不要在select中每次都NewTimer,在循环外面创建定时器,避免频繁创建。


有疑问加站长微信联系(非本文作者)

本文来自:Segmentfault

感谢作者:xiongkun01

查看原文:Go 通道

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

526 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传