当前位置: > > > > 当通道关闭时,以接收通道为参数的 goroutine 是否会停止?
当通道关闭时,以接收通道为参数的 goroutine 是否会停止?
来源:stackoverflow
2024-04-26 12:03:42
0浏览
收藏
小伙伴们有没有觉得学习Golang很有意思?有意思就对了!今天就给大家带来《当通道关闭时,以接收通道为参数的 goroutine 是否会停止?》,以下内容将会涉及到,若是在学习中对其中部分知识点有疑问,或许看了本文就能帮到你!
问题内容
我一直在阅读《用 go 构建微服务》,书中介绍了用于处理超时的 apache/go-resiliency/deadline 包。
deadline.go
// package deadline implements the deadline (also known as "timeout") resiliency pattern for go.
package deadline
import (
"errors"
"time"
)
// errtimedout is the error returned from run when the deadline expires.
var errtimedout = errors.new("timed out waiting for function to finish")
// deadline implements the deadline/timeout resiliency pattern.
type deadline struct {
timeout time.duration
}
// new constructs a new deadline with the given timeout.
func new(timeout time.duration) *deadline {
return &deadline{
timeout: timeout,
}
}
// run runs the given function, passing it a stopper channel. if the deadline passes before
// the function finishes executing, run returns errtimeout to the caller and closes the stopper
// channel so that the work function can attempt to exit gracefully. it does not (and cannot)
// simply kill the running function, so if it doesn't respect the stopper channel then it may
// keep running after the deadline passes. if the function finishes before the deadline, then
// the return value of the function is returned from run.
func (d *deadline) run(work func(<-chan struct{}) error) error {
result := make(chan error)
stopper := make(chan struct{})
go func() {
result <- work(stopper)
}()
select {
case ret := <-result:
return ret
case <-time.after(d.timeout):
close(stopper)
return errtimedout
}
}
deadline_test.go
package deadline
import (
"errors"
"testing"
"time"
)
func takesfivemillis(stopper <-chan struct{}) error {
time.sleep(5 * time.millisecond)
return nil
}
func takestwentymillis(stopper <-chan struct{}) error {
time.sleep(20 * time.millisecond)
return nil
}
func returnserror(stopper <-chan struct{}) error {
return errors.new("foo")
}
func testdeadline(t *testing.t) {
dl := new(10 * time.millisecond)
if err := dl.run(takesfivemillis); err != nil {
t.error(err)
}
if err := dl.run(takestwentymillis); err != errtimedout {
t.error(err)
}
if err := dl.run(returnserror); err.error() != "foo" {
t.error(err)
}
done := make(chan struct{})
err := dl.run(func(stopper <-chan struct{}) error {
<-stopper
close(done)
return nil
})
if err != errtimedout {
t.error(err)
}
<-done
}
func exampledeadline() {
dl := new(1 * time.second)
err := dl.run(func(stopper <-chan struct{}) error {
// do something possibly slow
// check stopper function and give up if timed out
return nil
})
switch err {
case errtimedout:
// execution took too long, oops
default:
// some other error
}
}
第一个问题
// in deadline_test.go
if err := dl.run(takestwentymillis); err != errtimedout {
t.error(err)
}
我无法理解上述代码的执行流程。据我了解,因为takestwentymillis函数休眠的时间超过了设置的超时时间10毫秒,
// in deadline.go
case <-time.after(d.timeout):
close(stopper)
return errtimedout
time.after 发出当前时间,并且选择这种情况。然后关闭stopper通道并返回errtimeout。
我不明白的是,关闭停止器通道会对可能仍在运行的匿名 goroutine 产生什么作用 我认为,当 stopper 通道关闭时,下面的 goroutine 可能仍在运行。
go func() {
result <- work(stopper)
}()
(如果我错了,请纠正我)我认为在 close(stopper) 之后,这个 goroutine 将调用 takestwentymillis(=work function),并以 stopper 通道作为参数。该函数将继续运行并休眠 20 毫秒并返回 nil 以传递到结果通道。 main() 到这里就结束了,对吗?
我不明白在这里关闭塞子通道有什么意义。 takestwentymillis 函数似乎并没有使用函数体内的通道:(。
第二个问题
// in deadline_test.go within testdeadline()
done := make(chan struct{})
err := dl.run(func(stopper <-chan struct{}) error {
<-stopper
close(done)
return nil
})
if err != errtimedout {
t.error(err)
}
<-done
这是我不完全理解的部分。我认为当 dl.run 运行时,塞子通道被初始化。但是因为塞子通道中没有值,所以函数调用将被阻止在 <-stopper…但是因为我不理解这段代码,所以我不明白为什么这段代码存在(即这段代码是什么)正在尝试测试,以及它是如何执行的,等等)。
关于第二个问题的第三个(附加)问题
所以我明白,当第二个问题中的 run 函数触发塞子通道关闭时,工作函数会收到信号。然后worker关闭done通道并返回nil。 我使用 delve(=go debugger) 来查看这一点,gdb 将我带到 deadline.go 中 return nil 行之后的 goroutine。
err := dl.run(func(stopper <-chan struct{}) error {
<-stopper
close(done)
--> return nil
})
输入 n 进入下一行后,delve 将我带到这里
go func() {
--> result <- work(stopper)
}()
这个过程在这里结束,因为当我再次输入 n 时,命令行提示 pass 并且过程退出。为什么这个过程会在这里结束? work(stopper) 似乎返回 nil,然后应该将其传递到结果通道,对吧?但由于某种原因,这一行似乎没有执行。
我知道主 goroutine,即 run 函数,已经返回了 errtimedout。所以我想这与此有关?
解决方案
第一个问题
stopper 通道的使用是向函数发出信号,例如takestwentymillis 表示已达到截止日期,调用者不再关心其结果。通常这意味着像 takestwentymillis 这样的工作函数应该检查 stopper 通道是否已经关闭,以便它可以取消它的工作。尽管如此,检查 stopper 通道仍然是工作函数的选择。它可能会也可能不会检查通道。
func takestwentymillis(stopper <-chan struct{}) error {
for i := 0; i < 20; i++ {
select {
case <-stopper:
// caller doesn't care anymore might as well stop working
return nil
case <-time.after(time.second): // simulating work
}
}
// work is done
return nil
}
第二个问题
deadline.run() 这部分将关闭塞子通道。
case <-time.After(d.timeout):
close(stopper)
读取关闭通道 (<-stopper) 将立即返回该通道的零值。我认为这只是测试最终超时的工作函数。
文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《当通道关闭时,以接收通道为参数的 goroutine 是否会停止?》文章吧,也可关注米云公众号了解相关技术文章。
