8

Go 通道

 4 years ago
source link: https://studygolang.com/articles/27057
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

无缓冲区通道

通过 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,在循环外面创建定时器,避免频繁创建。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK