我试图停止从服务器端连接到流服务器的所有客户端。实际上我正在使用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()
case notif := <-updateCh:
err := srv.Send(notif)
if err == io.EOF {
return nil
}
if err != nil {
s.logger.Named("Subscribe").Error("error", zap.Error(err))
continue
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我期望contextin 方法ctx.Done()可以处理它并打破 for 循环。如何关闭像这样的所有响应流?
为您的 gRPC 服务创建一个全局 context。因此,浏览各个部分:
os.Interrupt处理程序将取消全局上下文;从而取消任何当前正在运行的请求server.GracefulStop()- 应该等待所有活动的 gRPC 调用完成(如果他们没有立即看到取消)例如,在设置 gRPC 服务时:
pctx := context.Background()
globalCtx, globalCancel := context.WithCancel(pctx)
mysrv := MyService{
gctx: globalCtx
}
s := grpc.NewServer()
pb.RegisterMyService(s, mysrv)
Run Code Online (Sandbox Code Playgroud)
os.Interrupt处理程序启动并等待关闭:
globalCancel()
server.GracefulStop()
Run Code Online (Sandbox Code Playgroud)
gRPC 方法:
func(s *MyService) SomeRpcMethod(ctx context.Context, req *pb.Request) error {
// merge client and server contexts into one `mctx`
// (client context will cancel if client disconnects)
// (server context will cancel if service Ctrl-C'ed)
mctx, mcancel := mergeContext(ctx, s.gctx)
defer mcancel() // so we don't leak, if neither client or server context cancels
// RPC WORK GOES HERE
// RPC WORK GOES HERE
// RPC WORK GOES HERE
// pass mctx to any blocking calls:
// - http REST calls
// - SQL queries etc.
// - or if running a long loop; status check the context occasionally like so:
// Example long request (10s)
for i:=0; i<10*1000; i++ {
time.Sleep(1*time.Milliscond)
// poll merged context
select {
case <-mctx.Done():
return fmt.Errorf("request canceled: %s", mctx.Err())
default:
}
}
}
Run Code Online (Sandbox Code Playgroud)
和:
func mergeContext(a, b context.Context) (context.Context, context.CancelFunc) {
mctx, mcancel := context.WithCancel(a) // will cancel if `a` cancels
go func() {
select {
case <-mctx.Done(): // don't leak go-routine on clean gRPC run
case <-b.Done():
mcancel() // b canceled, so cancel mctx
}
}()
return mctx, mcancel
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10758 次 |
| 最近记录: |