Home > Article > Backend Development > Detailed 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!
Why do we need timeout control?
Common timeout control on the server side
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
Let’s take a look at how to implement timeout and what pitfalls there will be.
First version implementation
func requestWork(ctx context.Context, job interface{}) error { ctx, cancel := context.WithTimeout(ctx, time.Second*2) defer cancel() done := make(chan error) go func() { doneLet’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
time.Sleep(time.Minute*2)fmt.Println("number of goroutines:", runtime.NumGoroutine())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
➜ go run timeout.go elapsed: 2.005725931s number of goroutines: 1001
goroutine is leaked, let's see why this happens? First of all, the
requestWorkfunction 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
, 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.
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
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 <p>改完就可以在 <code>requestWork</code> 的调用方处理 <code>panic</code> 了。</p><h2>
<span class="header-link octicon octicon-link"></span>超时时长一定对吗?</h2><p>上面的 <code>requestWork</code> 实现忽略了传入的 <code>ctx</code> 参数,如果 <code>ctx</code> 已有超时设置,我们一定要关注此传入的超时是不是小于这里给的2秒,如果小于,就需要用传入的超时,<code>go-zero/core/contextx</code> 已经提供了方法帮我们一行代码搞定,只需修改如下:</p><pre class="brush:php;toolbar:false">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-zero
并 star 支持我们!
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!