一般一个业务很少不用到goroutine的,因为很多方法是需要等待的,例如http.Server.ListenAndServe
这个就是等待的,除非关闭了Server或Listener,否则是不会返回的。除非是一个API服务器,否则肯定需要另外起goroutine发起其他的服务,而且对于API服务器来说,在http.Handler
的处理函数中一般也需要起goroutine,如何管理这些goroutine,在GOLANG1.7提供context.Context
。
先看一个简单的,如果启动两个goroutine,一个是HTTP,还有个信号处理的收到退出信号做清理:
wg := sync.WaitGroup{} defer wg.Wait() wg.Add(1) go func() { defer wg.Done() ss := make(os.Signal, 0) signal.Notify(ss, syscall.SIGINT, syscall.SIGTERM) for s := ss { fmt.Println("Got signal", s) break } }() wg.Add(1) go func() { defer wg.Done() svr := &http.Server{ Addr:":8080", Handler:nil, } fmt.Println(svr.ListenAndServe()) }
很清楚,起了两个goroutine,然后用WaitGroup等待它们退出。如果它们之间没有交互,不互相影响,那真的是蛮简单的,可惜这样是不行的,因为信号的goroutine收到退出信号后,应该通知server退出。暴力一点的是直接调用svr.Close()
,但是如果有些请求还需要取消怎么办呢?最好用Context了:
wg := sync.WaitGroup{} defer wg.Wait() ctx,cancel := context.WithCancel(context.Background()) wg.Add(1) go func() { defer wg.Done() ss := make(chan os.Signal, 0) signal.Notify(ss, syscall.SIGINT, syscall.SIGTERM) select { case <- ctx.Done(): return case s := <- ss: fmt.Println("Got signal", s) cancel() // 取消请求,通知用到ctx的所有goroutine return } }() wg.Add(1) go func() { defer wg.Done() defer cancel() svr := &http.Server{ Addr:":8080", Handler:nil, } go func(){ select { case <- ctx.Done(): svr.Close() } } fmt.Println(svr.ListenAndServe()) }
这个方式可以在新开goroutine时继续使用,譬如新加一个goroutine,里面读写了UDPConn:
wg.Add(1) go func() { defer wg.Done() defer cancel() var conn *net.UDPConn if conn,err = net.Dial("udp", "127.0.0.1:1935"); err != nil { fmt.Println("Dial UDP server failed, err is", err) return } fmt.Println(UDPRead(ctx, conn)) }() UDPRead = func(ctx context.Context, conn *net.UDPConn) (err error) { wg := sync.WaitGroup{} defer wg.Wait() ctx, cancel := context.WithCancel(ctx) wg.Add(1) go func() { defer wg.Done() defer cancel() for { b := make([]byte, core.MTUSize) size, _, err := conn.ReadFromUDP(b) // 处理UDP包 b[:size] } }() select { case <-ctx.Done(): conn.Close() } return }
如果只是用到HTTP Server,可以这么写:
func run(ctx contex.Context) { server := &http.Server{Addr: addr, Handler: nil} go func() { select { case <-ctx.Done(): server.Close() } }() http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) { }) fmt.Println(server.ListenAndServe()) }
如果需要提供一个API来让服务器退出,可以这么写:
func run(ctx contex.Context) { server := &http.Server{Addr: addr, Handler: nil} ctx, cancel := context.WithCancel(ctx) http.HandleFunc("/quit", func(w http.ResponseWriter, r *http.Request) { cancel() // 使用局部的ctx和cancel }) go func() { select { case <-ctx.Done(): server.Close() } }() fmt.Println(server.ListenAndServe()) }
使用局部的ctx和cancel,可以避免cancel传入的ctx,只是影响当前的ctx。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新动态
- 群星《影视剧 群星闪耀时 影视原声带》[320K/MP3][38.69MB]
- 姜育恒.1998-成名金曲(马来西亚瑞华金碟珍藏版)【瑞华】【WAV+CUE】
- 水木年华.2006-生命狂想曲特别版2CD【水木同创】【WAV+CUE】
- 卢庚戌.2000-未来的未来【喜洋洋】【WAV+CUE】
- 钟汉良.1997-亲热【艺能动音】【WAV+CUE】
- 满文军.2002-我需要你【新索音乐】【WAV+CUE】
- 毛阿敏.2000-毛阿敏·精选集【中唱】【WAV+CUE】
- 钟明秋《爱有天意HQ》头版限量[低速原抓WAV+CUE]
- 云飞《云在飞HQ》头版限量编号[低速原抓WAV+CUE]
- 戏班-就是这个调调【FLAC分轨】
- 黄品源《寂寞旋律》[WAV+CUE][367M]
- 群星《家传户晓~我们三十年的主题曲XRCD》[WAV分轨][532M]
- 谢军《那一夜+又一夜》2CD[WAV分轨][1.2G]
- 苏有朋.1992-我只要你爱我【飞碟】【WAV+CUE】
- 谢金燕.2010-爱你辣【乾坤唱片】【WAV+CUE】