Home >Backend Development >Golang >Golang function communication pipeline timeout processing strategy

Golang function communication pipeline timeout processing strategy

PHPz
PHPzOriginal
2024-05-04 12:00:021084browse

When using pipelines for communication, in order to prevent the pipeline receiver from being blocked all the time, Golang provides two timeout processing strategies: use Context to set time limits or use select to listen to multiple pipelines. When the pipeline receiver does not receive data, these two All strategies will time out.

Golang function communication pipeline timeout processing strategy

Golang function communication pipeline timeout processing strategy

Pipelines are a common way of inter-process communication in Golang. However, when the receiving end of the pipeline cannot receive data, it will block forever. To prevent this blocking, we can use a piped receive operation with a timeout.

Timeout processing strategy

There are two main timeout processing strategies:

  • Use Context:Use context .Context type, we can set a time limit. If no data is received within this time limit, the operation will time out.
  • Use select: Using the select statement, we can listen to multiple pipes at the same time. When the pipeline receiver does not receive data, select will time out.

Practical case

The following is an example of a pipeline receiving operation using the Context timeout processing strategy:

package main

import (
    "context"
    "fmt"
    "log"
    "sync/atomic"
    "time"
)

func main() {
    // 创建一个管道
    ch := make(chan int)
    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()

    // 并发地将数据发送到管道
    go func() {
        for i := 0; i < 10; i++ {
            ch <- i
        }
    }()

    // 使用 Context 超时接收数据
    go func() {
        var total uint64
        for {
            select {
            case <-ctx.Done():
                fmt.Println("Timeout reached!")
                return
            case value := <-ch:
                total += uint64(value)
            }
        }
    }()

    log.Printf("Total: %d", total)
}

Using the select timeout processing strategy Pipe receive operation example:

package main

import (
    "fmt"
    "log"
    "sync/atomic"
    "time"
)

func main() {
    // 创建一个管道
    ch := make(chan int)

    // 创建一个 select 语句来监听管道和超时
    var total uint64
    go func() {
        for {
            select {
            case value := <-ch:
                total += uint64(value)
            case <-time.After(5 * time.Second):
                fmt.Println("Timeout reached!")
                return
            }
        }
    }()

    // 并发地将数据发送到管道
    go func() {
        for i := 0; i < 10; i++ {
            ch <- i
        }
    }()

    log.Printf("Total: %d", total)
}

The above is the detailed content of Golang function communication pipeline timeout processing strategy. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn