最近发布的主题
最近发布的文章
- gRPC负载均衡(自定义负载均衡策略) at
- gRPC负载均衡(客户端负载均衡) at
- etcd实现服务发现 at
- 多阶段构建Golang程序Docker镜像 at
- gin请求数据校验 at
最近分享的资源
暂无
最近发布的项目
暂无
最近的评论
-
评论了主题 go协程超时后怎么中断协程执行的任务可以用runtime.Goexit()停止当前Go协程 ``` package main import ( "log" "runtime" "time" ) func main() { cb := make(chan bool, 1) go func() { timer2 := time.NewTimer(time.Second * 3) tick := time.Tick(time.Second * 5) select { case <-timer2.C: runtime.Goexit() //超时后退出该Go协程 case <-tick: //模拟超时任务 log.Println(222) //处理业务代码 cb <- true return } }() log.Println() timer := time.NewTimer(time.Second * 2) select { case <-timer.C: log.Println("time out") cb <- false case tmp := <-cb: log.Println(tmp) } time.Sleep(time.Second * 10) log.Println(123) } ```
-
``` func ProcessChannelMessages(ctx context.Context, in <-chan string, idleCounter prometheus.Counter) { idleDuration := 5 * time.Minute tick:= time.Tick(idleDuration) for { select { case s, ok := <-in: if !ok { return } // handle `s` case <-tick: idleCounter.Inc() case <-ctx.Done(): return } } } ``` 用这种周期性运行的方法,同样实现上面的功能,这个应该不会有内存泄露吧
-
评论了主题 gin+vue的前后端分离开源项目