Closed freehere107 closed 5 years ago
for { s := <-c log.Info("get a signal %s", s.String()) switch s { case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT: ctx, cancel := context.WithTimeout(context.Background(), 35*time.Second) defer cancel() //这里 grpcSrv.Shutdown(ctx) httpSrv.Shutdown(ctx) log.Info("kratos-demo exit") svc.Close() time.Sleep(time.Second) return case syscall.SIGHUP: default: return }
改成
for { s := <-c log.Info("get a signal %s", s.String()) switch s { case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT: func(){ ctx, cancel := context.WithTimeout(context.Background(), 35*time.Second) defer cancel() httpSrv.Shutdown(ctx) log.Info("kratos-demo exit") svc.Close() time.Sleep(time.Second) }() return case syscall.SIGHUP: default: return } }
这样idea就不会报warning了
@freehere107 idea报的warning是什么?
Possible resource leak, 'defer' is called a for loop.Reports defer statements inside loops
cancel直接放到svc.Close()下就行了
@freehere107 已经修复
改成
这样idea就不会报warning了