search
HomeBackend DevelopmentGolangDetailed explanation of how to implement Go timeout control

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)}

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 
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

The timeout has taken effect. But is this done?

goroutine leakage

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

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: <pre class="brush:php;toolbar:false">done := make(chan error, 1)</pre>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: <pre class="brush:php;toolbar:false">➜ go run timeout.go elapsed: 2.005655146s number of goroutines: 1</pre>The goroutine leak problem is solved!

panic cannot be caught

panic("oops")

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")}()

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:<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><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>
  • 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
This article is reproduced at:learnku. If there is any infringement, please contact admin@php.cn delete
Golang vs. Python: Concurrency and MultithreadingGolang vs. Python: Concurrency and MultithreadingApr 17, 2025 am 12:20 AM

Golang is more suitable for high concurrency tasks, while Python has more advantages in flexibility. 1.Golang efficiently handles concurrency through goroutine and channel. 2. Python relies on threading and asyncio, which is affected by GIL, but provides multiple concurrency methods. The choice should be based on specific needs.

Golang and C  : The Trade-offs in PerformanceGolang and C : The Trade-offs in PerformanceApr 17, 2025 am 12:18 AM

The performance differences between Golang and C are mainly reflected in memory management, compilation optimization and runtime efficiency. 1) Golang's garbage collection mechanism is convenient but may affect performance, 2) C's manual memory management and compiler optimization are more efficient in recursive computing.

Golang vs. Python: Applications and Use CasesGolang vs. Python: Applications and Use CasesApr 17, 2025 am 12:17 AM

ChooseGolangforhighperformanceandconcurrency,idealforbackendservicesandnetworkprogramming;selectPythonforrapiddevelopment,datascience,andmachinelearningduetoitsversatilityandextensivelibraries.

Golang vs. Python: Key Differences and SimilaritiesGolang vs. Python: Key Differences and SimilaritiesApr 17, 2025 am 12:15 AM

Golang and Python each have their own advantages: Golang is suitable for high performance and concurrent programming, while Python is suitable for data science and web development. Golang is known for its concurrency model and efficient performance, while Python is known for its concise syntax and rich library ecosystem.

Golang vs. Python: Ease of Use and Learning CurveGolang vs. Python: Ease of Use and Learning CurveApr 17, 2025 am 12:12 AM

In what aspects are Golang and Python easier to use and have a smoother learning curve? Golang is more suitable for high concurrency and high performance needs, and the learning curve is relatively gentle for developers with C language background. Python is more suitable for data science and rapid prototyping, and the learning curve is very smooth for beginners.

The Performance Race: Golang vs. CThe Performance Race: Golang vs. CApr 16, 2025 am 12:07 AM

Golang and C each have their own advantages in performance competitions: 1) Golang is suitable for high concurrency and rapid development, and 2) C provides higher performance and fine-grained control. The selection should be based on project requirements and team technology stack.

Golang vs. C  : Code Examples and Performance AnalysisGolang vs. C : Code Examples and Performance AnalysisApr 15, 2025 am 12:03 AM

Golang is suitable for rapid development and concurrent programming, while C is more suitable for projects that require extreme performance and underlying control. 1) Golang's concurrency model simplifies concurrency programming through goroutine and channel. 2) C's template programming provides generic code and performance optimization. 3) Golang's garbage collection is convenient but may affect performance. C's memory management is complex but the control is fine.

Golang's Impact: Speed, Efficiency, and SimplicityGolang's Impact: Speed, Efficiency, and SimplicityApr 14, 2025 am 12:11 AM

Goimpactsdevelopmentpositivelythroughspeed,efficiency,andsimplicity.1)Speed:Gocompilesquicklyandrunsefficiently,idealforlargeprojects.2)Efficiency:Itscomprehensivestandardlibraryreducesexternaldependencies,enhancingdevelopmentefficiency.3)Simplicity:

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools