前言
超时,指一个协程A开启另一个协程B,A会阻塞等待B一段指定的时间,例如:5秒,A通知B结束(也有可能不通知,让B继续运行)。也就是说,A就不愿意阻塞等待太久。
Go语言有多种方法实现这种超时,我总结出3种:
方法一:用两个通道 + A协程sleep
一个通道用来传数据,一个用来传停止信号。
- package main
-
- import (
- "fmt"
- "time"
- )
-
- // 老师视频里的生产者消费者
-
- func main() {
- //知识点: 老师这里用了两个线程,一个用个传数据,一个用来传关闭信号
- messages := make(chan int, 10)
- done := make(chan bool)
-
- defer close(messages)
-
- // consumer
- go func() {
- ticker := time.NewTicker(1 * time.Second)
- for range ticker.C {
- select {
- case <-done:
- fmt.Println("child process interrupt...") // 数据还没收完,就被停止了。
- return
- default:
- fmt.Printf("receive message:%d\n", <-messages)
- }
-
- }
- }()
-
- // producer
- for i := 0; i < 10; i++ {
- messages <- i
- }
-
- // 5秒后主线程关闭done通道
- time.Sleep(5 * time.Second)
- close(done)
- time.Sleep(1 * time.Second)
- fmt.Println("main process exit!")
- }
程序输出如下:
receive message:0
receive message:1
receive message:2
receive message:3
child process interrupt...
main process exit!
方法二:使用Timer(定时器)
这种方法也方法一类似,只不过是用一个Timer代替通道。
- package main
-
- import (
- "fmt"
- "time"
- )
-
- //知识点:
- // 1) 多通道
- // 2) 定时器
- func main() {
- ch1 := make(chan int, 10)
- go func(ch chan<- int) {
- // 假设子协程j是一个耗时操作,例如访问网络,要10秒后才会有数据
- time.Sleep(10 * time.Second)
- ch <- 1
- }(ch1)
-
- timer := time.NewTimer(5 * time.Second) // 设置定时器的超时时间,主线程只等5秒
-
- fmt.Println("select start....")
- // 知识点:主协程等待子线程,并有超时机制
- select {
- case <-ch1:
- fmt.Println("从channel 1 收到一个数字")
- case <-timer.C: // 定时器也是一个通道
- fmt.Println("5秒到了,超时了,main协程不等了")
- }
-
- fmt.Println("done!")
- }
程序输出如下:
select start....
5秒到了,超时了,main协程不等了
done!
方法三:使用context.WithTimeout
下面的例子比较复杂,基于 Channel 编写一个简单的单协程生产者消费者模型。
要求如下:
1)队列:队列长度 10,队列元素类型为 int
2)生产者:每 1 秒往队列中放入一个类型为 int 的元素,队列满时生产者可以阻塞
3)消费者:每2秒从队列中获取一个元素并打印,队列为空时消费者阻塞
4)主协程30秒后要求所有子协程退出。
5)要求优雅退出,即消费者协程退出前,要先消费完所有的int
6)通过入参支持两种运行模式:
- wb(温饱模式)生产速度快过消费速度、
- je(饥饿模式)生产速度慢于消费速度
context.WithTimeout见第87行。
- package main
-
- import (
- "context"
- "flag"
- "fmt"
- "sync"
- "time"
- )
-
- // 课后练习 1.2
- // 基于 Channel 编写一个简单的单协程生产者消费者模型。
- // 要求如下:
- // 1)队列:队列长度 10,队列元素类型为 int
- // 2)生产者:每 1 秒往队列中放入一个类型为 int 的元素,队列满时生产者可以阻塞
- // 3)消费者:每2秒从队列中获取一个元素并打印,队列为空时消费者阻塞
- // 4)主协程30秒后要求所有子协程退出。
- // 5)要求优雅退出,即消费者协程退出前,要先消费完所有的int。
-
- // 知识点:
- // 1) 切片的零值也是可用的。
- // 2) context.WithTimeout
- var (
- wg sync.WaitGroup
- p Producer
- c Consumer
- )
-
- type Producer struct {
- Time int
- Interval int
- }
-
- type Consumer struct {
- Producer
- }
-
- func (p Producer) produce(queue chan<- int, ctx context.Context) {
- go func() {
- LOOP:
- for {
- p.Time = p.Time + 1
- queue <- p.Time
- fmt.Printf("生产者进行第%d次生产,值:%d\n", p.Time, p.Time)
- time.Sleep(time.Duration(p.Interval) * time.Second)
-
- select {
- case <-ctx.Done():
- close(queue)
- break LOOP
- }
- }
- wg.Done()
- }()
- }
-
- func (c Consumer) consume(queue <-chan int, ctx context.Context) {
- go func() {
- LOOP:
- for {
- c.Time++
- val := <-queue
- fmt.Printf("-->消费者进行第%d次消费,值:%d\n", c.Time, val)
- time.Sleep(time.Duration(c.Interval) * time.Second)
-
- select {
- case <-ctx.Done():
- //remains := new([]int)
- //remains := []int{}
- var remains []int // 知识点:切片的零值也是可用的。
- for val = range queue {
- remains = append(remains, val)
- fmt.Printf("-->消费者: 最后一次消费, 值为:%v\n", remains)
- break LOOP
- }
- }
- }
- wg.Done()
- }()
- }
-
- func main() {
- wg.Add(2)
-
- // 知识点:context.Timeout
- timeout := 30
- ctx, _ := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
-
- queue := make(chan int, 10)
-
- p.produce(queue, ctx)
- fmt.Println("main waiting...")
- wg.Wait()
- fmt.Println("done")
- }
-
- /*
- 启动命令:
- $ go run main/main.go -m wb
- $ go run main/main.go -m je
- */
- func init() {
- // 解析程序入参,运行模式
- mode := flag.String("m", "wb", "请输入运行模式:\nwb(温饱模式)生产速度快过消费速度、\nje(饥饿模式)生产速度慢于消费速度)")
- flag.Parse()
-
- p = Producer{}
- c = Consumer{}
-
- if *mode == "wb" {
- fmt.Println("运行模式:wb(温饱模式)生产速度快过消费速度")
- p.Interval = 1 // 每隔1秒生产一次
- c.Interval = 5 // 每隔5秒消费一次
-
- // p = Producer{Interval: 1}
- // c = Consumer{Interval: 5} // 这一行会报错,为什么?
-
- } else {
- fmt.Println("运行模式:je(饥饿模式)生产速度慢于消费速度")
- p.Interval = 5 // 每隔5秒生产一次
- c.Interval = 1 // 每隔1秒消费一次
- }
- }
wb(温饱模式)生产速度快过消费速度,输出如下:
运行模式:wb(温饱模式)生产速度快过消费速度
生产者: 第1次生产, 值为:1
-->消费者: 第1次消费, 值为:1
生产者: 第2次生产, 值为:2
生产者: 第3次生产, 值为:3
生产者: 第4次生产, 值为:4
生产者: 第5次生产, 值为:5
-->消费者: 第2次消费, 值为:2
生产者: 第6次生产, 值为:6
生产者: 第7次生产, 值为:7
生产者: 第8次生产, 值为:8
生产者: 第9次生产, 值为:9
生产者: 第10次生产, 值为:10
-->消费者: 第3次消费, 值为:3
生产者: 第11次生产, 值为:11
生产者: 第12次生产, 值为:12
生产者: 第13次生产, 值为:13
-->消费者: 第4次消费, 值为:4
生产者: 第14次生产, 值为:14
-->消费者: 第5次消费, 值为:5
生产者: 第15次生产, 值为:15
生产者: 第16次生产, 值为:16
-->消费者: 第6次消费, 值为:6
main waiting
生产者: 第17次生产, 值为:17
-->消费者: 最后一次消费, 值为:[7 8 9 10 11 12 13 14 15 16 17]
-- done --
je(饥饿模式)生产速度慢于消费速度,输出如下:
运行模式:je(饥饿模式)生产速度慢于消费速度
-->消费者: 第1次消费, 值为:1
生产者: 第1次生产, 值为:1
生产者: 第2次生产, 值为:2
-->消费者: 第2次消费, 值为:2
生产者: 第3次生产, 值为:3
-->消费者: 第3次消费, 值为:3
生产者: 第4次生产, 值为:4
-->消费者: 第4次消费, 值为:4
生产者: 第5次生产, 值为:5
-->消费者: 第5次消费, 值为:5
生产者: 第6次生产, 值为:6
-->消费者: 第6次消费, 值为:6
main waiting
-->消费者: 第7次消费, 值为:0
附:go 实现超时退出
之前手写rpc框架的时候,吃多了网络超时处理的苦,今天偶然发现了实现超时退出的方法,MARK
- func AsyncCall() {
- ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Millisecond*800))
- defer cancel()
- go func(ctx context.Context) {
- // 发送HTTP请求
- }()
-
- select {
- case <-ctx.Done():
- fmt.Println("call successfully!!!")
- return
- case <-time.After(time.Duration(time.Millisecond * 900)):
- fmt.Println("timeout!!!")
- return
- }
- }
-
- //2
- func AsyncCall() {
- ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Millisecond * 800))
- defer cancel()
- timer := time.NewTimer(time.Duration(time.Millisecond * 900))
-
- go func(ctx context.Context) {
- // 发送HTTP请求
- }()
-
- select {
- case <-ctx.Done():
- timer.Stop()
- timer.Reset(time.Second)
- fmt.Println("call successfully!!!")
- return
- case <-timer.C:
- fmt.Println("timeout!!!")
- return
- }
- }
-
-
- //3
- func AsyncCall() {
- ctx := context.Background()
- done := make(chan struct{}, 1)
-
- go func(ctx context.Context) {
- // 发送HTTP请求
- done <- struct{}{}
- }()
-
- select {
- case <-done:
- fmt.Println("call successfully!!!")
- return
- case <-time.After(time.Duration(800 * time.Millisecond)):
- fmt.Println("timeout!!!")
- return
- }
- }
总结
到此这篇关于Go语言实现超时的三种方法的文章就介绍到这了,更多相关Go语言实现超时方法内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!