Table of Contents
The request time is too long, the user may have left this page, the server is still consuming resources for processing, and the results obtained are meaningless" >The request time is too long, the user may have left this page, the server is still consuming resources for processing, and the results obtained are meaningless
Logical processing within the process" >Logical processing within the process
hardWork as an example. It doesn’t matter what it is used for. As the name suggests, it may be slower to process. " >To simplify this article, we take a request function hardWork as an example. It doesn’t matter what it is used for. As the name suggests, it may be slower to process.
time.Sleep(time.Minute*2)fmt.Println("number of goroutines:", runtime.NumGoroutine())
Copy after login
sleep 2 minutes is to wait for all The task ends, and then we print the current number of goroutines. Let's execute it and see the result" >Let us add a line of code at the end of the main function to see how many goroutines have been executed
time.Sleep(time.Minute*2)fmt.Println("number of goroutines:", runtime.NumGoroutine())
Copy after login
sleep 2 minutes is to wait for all The task ends, and then we print the current number of goroutines. Let's execute it and see the result
hardWork function implementation to " >Let us change the hardWork function implementation to
Home Backend Development Golang Detailed explanation of how to implement Go timeout control

Detailed explanation of how to implement Go timeout control

Apr 06, 2021 am 11:18 AM
go golang

The following tutorial column will introduce to you how to implement Go timeout control. I hope it will be helpful to friends in need!

Detailed explanation of how to implement Go timeout controlWhy do we need timeout control?

The server-side processing will take too long Occupying too many resources, resulting in reduced concurrency and even unavailability accidents
  • Go Necessity of timeout control

Go is normally used to write the backend For service, generally a request is completed by multiple serial or parallel subtasks. Each subtask may be another internal request. Then when the request times out, we need to return quickly and release the occupied resources. Such as goroutine, file descriptor, etc.

Common timeout control on the server side

Reading and writing clients Side requests, such as HTTP or RPC requests
  • Call other server-side requests, including calling RPC or accessing DB, etc.
  • What will happen if there is no timeout control?

To simplify this article, we take a request function hardWork as an example. It doesn’t matter what it is used for. As the name suggests, it may be slower to process.

func hardWork(job interface{}) error {
    time.Sleep(time.Minute)
    return nil}func requestWork(ctx context.Context, job interface{}) error {
  return hardWork(job)}
Copy after login

At this time, what the client sees is always the familiar picture

The vast majority of users will not look at chrysanthemums for a minute and give up early If you leave, you will be left with a bunch of resource occupation on the entire call link. This article will not go into other details and only focus on the timeout implementation.

Detailed explanation of how to implement Go timeout control Let’s take a look at how to implement timeout and what pitfalls there will be.

First version implementation

You can stop reading and try to think about how to implement the timeout of this function. First try:
func requestWork(ctx context.Context, job interface{}) error {
    ctx, cancel := context.WithTimeout(ctx, time.Second*2)
    defer cancel()

    done := make(chan error)
    go func() {
        done 
Copy after login
Let’s write a main function to test it

func main() {
    const total = 1000
    var wg sync.WaitGroup
    wg.Add(total)
    now := time.Now()
    for i := 0; i <p>Run it to see the effect</p><pre class="brush:php;toolbar:false">➜ go run timeout.go
elapsed: 2.005725931s
Copy after login

The timeout has taken effect. But is this done?

goroutine leakage

➜ go run timeout.go
elapsed: 2.005725931s
number of goroutines: 1001
Copy after login

goroutine is leaked, let's see why this happens? First of all, the

requestWork

function exits after 2 seconds timeout. Once the

requestWork

function exits, then done channel will have no goroutine to receive it. Wait until executiondone This line of code will be stuck and cannot be written, causing each timeout request to occupy a goroutine. This is a big bug. When the resources are exhausted, When the time comes, the entire service becomes unresponsive. <code>So how to fix it? In fact, it is very simple. Just set buffer size to 1 when

make chan

, as follows: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">done := make(chan error, 1)</pre><div class="contentsignin">Copy after login</div></div>This way, done Can write without blocking the goroutine regardless of whether it times out. At this point, someone may ask if there will be any problem if you write to a channel that has no goroutine to receive it. In Go, the channel is not like our common file descriptors. It does not have to be closed, it is just an object.

close(channel)

is only used to tell the receiver that there is nothing to write, and has no other purpose. After changing this line of code, let’s test it again: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">➜ go run timeout.go elapsed: 2.005655146s number of goroutines: 1</pre><div class="contentsignin">Copy after login</div></div>The goroutine leak problem is solved!

panic cannot be caught

panic("oops")
Copy after login

Modifymain The function plus the code for catching exceptions is as follows:

go func() {
  defer func() {
    if p := recover(); p != nil {
      fmt.Println("oops, panic")
    }
  }()

  defer wg.Done()
  requestWork(context.Background(), "any")}()
Copy after login

If you execute it at this time, you will find that panic cannot be captured. The reason is that other panics generated in the goroutine inside requestWork goroutine cannot catch.

The solution is to add panicChan to

requestWork

for processing. Similarly, the buffer size of panicChan is required to be 1 ,as follows:<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">func requestWork(ctx context.Context, job interface{}) error {     ctx, cancel := context.WithTimeout(ctx, time.Second*2)     defer cancel()     done := make(chan error, 1)     panicChan := make(chan interface{}, 1)     go func() {         defer func() {             if p := recover(); p != nil {                 panicChan &lt;p&gt;改完就可以在 &lt;code&gt;requestWork&lt;/code&gt; 的调用方处理 &lt;code&gt;panic&lt;/code&gt; 了。&lt;/p&gt;&lt;h2&gt; &lt;span class=&quot;header-link octicon octicon-link&quot;&gt;&lt;/span&gt;超时时长一定对吗?&lt;/h2&gt;&lt;p&gt;上面的 &lt;code&gt;requestWork&lt;/code&gt; 实现忽略了传入的 &lt;code&gt;ctx&lt;/code&gt; 参数,如果 &lt;code&gt;ctx&lt;/code&gt; 已有超时设置,我们一定要关注此传入的超时是不是小于这里给的2秒,如果小于,就需要用传入的超时,&lt;code&gt;go-zero/core/contextx&lt;/code&gt; 已经提供了方法帮我们一行代码搞定,只需修改如下:&lt;/p&gt;&lt;pre class=&quot;brush:php;toolbar:false&quot;&gt;ctx, cancel := contextx.ShrinkDeadline(ctx, time.Second*2)</pre><div class="contentsignin">Copy after login</div></div><h2> <span class="header-link octicon octicon-link"></span>Data race</h2><p>这里 <code>requestWork 只是返回了一个 error 参数,如果需要返回多个参数,那么我们就需要注意 data race,此时可以通过锁来解决,具体实现参考 go-zero/zrpc/internal/serverinterceptors/timeoutinterceptor.go,这里不做赘述。

完整示例

package mainimport (
    "context"
    "fmt"
    "runtime"
    "sync"
    "time"

    "github.com/tal-tech/go-zero/core/contextx")func hardWork(job interface{}) error {
    time.Sleep(time.Second * 10)
    return nil}func requestWork(ctx context.Context, job interface{}) error {
    ctx, cancel := contextx.ShrinkDeadline(ctx, time.Second*2)
    defer cancel()

    done := make(chan error, 1)
    panicChan := make(chan interface{}, 1)
    go func() {
        defer func() {
            if p := recover(); p != nil {
                panicChan <h2>
<span class="header-link octicon octicon-link"></span>更多细节</h2><p>请参考 <code>go-zero</code> 源码:</p>
Copy after login
  • go-zero/core/fx/timeout.go
  • go-zero/zrpc/internal/clientinterceptors/timeoutinterceptor.go
  • go-zero/zrpc/internal/serverinterceptors/timeoutinterceptor.go

项目地址

github.com/tal-tech/go-zero

欢迎使用 go-zerostar 支持我们!

The above is the detailed content of Detailed explanation of how to implement Go timeout control. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to safely read and write files using Golang? How to safely read and write files using Golang? Jun 06, 2024 pm 05:14 PM

Reading and writing files safely in Go is crucial. Guidelines include: Checking file permissions Closing files using defer Validating file paths Using context timeouts Following these guidelines ensures the security of your data and the robustness of your application.

How to configure connection pool for Golang database connection? How to configure connection pool for Golang database connection? Jun 06, 2024 am 11:21 AM

How to configure connection pooling for Go database connections? Use the DB type in the database/sql package to create a database connection; set MaxOpenConns to control the maximum number of concurrent connections; set MaxIdleConns to set the maximum number of idle connections; set ConnMaxLifetime to control the maximum life cycle of the connection.

How to use gomega for assertions in Golang unit tests? How to use gomega for assertions in Golang unit tests? Jun 05, 2024 pm 10:48 PM

How to use Gomega for assertions in Golang unit testing In Golang unit testing, Gomega is a popular and powerful assertion library that provides rich assertion methods so that developers can easily verify test results. Install Gomegagoget-ugithub.com/onsi/gomega Using Gomega for assertions Here are some common examples of using Gomega for assertions: 1. Equality assertion import "github.com/onsi/gomega" funcTest_MyFunction(t*testing.T){

Golang framework vs. Go framework: Comparison of internal architecture and external features Golang framework vs. Go framework: Comparison of internal architecture and external features Jun 06, 2024 pm 12:37 PM

The difference between the GoLang framework and the Go framework is reflected in the internal architecture and external features. The GoLang framework is based on the Go standard library and extends its functionality, while the Go framework consists of independent libraries to achieve specific purposes. The GoLang framework is more flexible and the Go framework is easier to use. The GoLang framework has a slight advantage in performance, and the Go framework is more scalable. Case: gin-gonic (Go framework) is used to build REST API, while Echo (GoLang framework) is used to build web applications.

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

How to find the first substring matched by a Golang regular expression? How to find the first substring matched by a Golang regular expression? Jun 06, 2024 am 10:51 AM

The FindStringSubmatch function finds the first substring matched by a regular expression: the function returns a slice containing the matching substring, with the first element being the entire matched string and subsequent elements being individual substrings. Code example: regexp.FindStringSubmatch(text,pattern) returns a slice of matching substrings. Practical case: It can be used to match the domain name in the email address, for example: email:="user@example.com", pattern:=@([^\s]+)$ to get the domain name match[1].

Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Apr 02, 2025 am 09:12 AM

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

How to use predefined time zone with Golang? How to use predefined time zone with Golang? Jun 06, 2024 pm 01:02 PM

Using predefined time zones in Go includes the following steps: Import the "time" package. Load a specific time zone through the LoadLocation function. Use the loaded time zone in operations such as creating Time objects, parsing time strings, and performing date and time conversions. Compare dates using different time zones to illustrate the application of the predefined time zone feature.

See all articles