我试图停止从服务器端连接到流服务器的所有客户端。实际上我正在使用GracefulStop方法来优雅地处理它。
我正在等待os.Interrupt通道上的信号来执行 gRPC 的正常停止。server.GracefulStop()但当客户端连接时它会卡住。
func (s *Service) Subscribe(_ *empty.Empty, srv clientapi.ClientApi_SubscribeServer) error {
ctx := srv.Context()
updateCh := make(chan *clientapi.Update, 100)
stopCh := make(chan bool)
defer func() {
stopCh<-true
close(updateCh)
}
go func() {
ticker := time.NewTicker(1 * time.Second)
defer func() {
ticker.Stop()
close(stopCh)
}
for {
select {
case <-stopCh:
return
case <-ticker.C:
updateCh<- &clientapi.Update{Name: "notification": Payload: "sample notification every 1 second"}
}
}
}()
for {
select {
case <-ctx.Done():
return ctx.Err() …Run Code Online (Sandbox Code Playgroud)