Go语言实现超时的方法有哪些

发布时间:2022-07-20 16:53:14 作者:iii
来源:亿速云 阅读:116

这篇文章主要介绍“Go语言实现超时的方法有哪些”,在日常操作中,相信很多人在Go语言实现超时的方法有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Go语言实现超时的方法有哪些”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

方法一:用两个通道 + 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() {
	ch2 := make(chan int, 10)
	go func(ch chan<- int) {
		// 假设子协程j是一个耗时操作,例如访问网络,要10秒后才会有数据
		time.Sleep(10 * time.Second)
		ch <- 1
	}(ch2)
 
	timer := time.NewTimer(5 * time.Second) // 设置定时器的超时时间,主线程只等5秒
 
	fmt.Println("select start....")
	// 知识点:主协程等待子线程,并有超时机制
	select {
	case <-ch2:
		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)通过入参支持两种运行模式:

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语言实现超时的方法有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. go语言异常处理的方法有哪些?
  2. php运行超时的解决方法有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

go语言

上一篇:HttpClient如何使用

下一篇:ios UICollectionView怎么实现图片浏览效果

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》