Hi all!

grpc-go version 1.7.0
If client will be disconnected by network error, server must close in my 
case pub/sub connection. I know about ctx.Done() function, but don't know 
how to use it properly in my case. Can somebody explain please?


func (a *API) Notifications(in *empty.Empty, stream 
pb.Service_NotificationsServer) error {
ctx := stream.Context()
_, ok := user.FromContext(ctx)
if !ok {
return grpc.Errorf(codes.Unauthenticated, "user not found")
}

pubsub := a.redisClient.Subscribe("notifications")
defer pubsub.Close()

for {
msg, err := pubsub.ReceiveMessage()
if err != nil {
grpclog.Warningf("Notifications: pubsub error: %v", err)
return grpc.Errorf(codes.Internal, "pubsub error %v", err)
}

notification := &pb.Notification{}
err = json.Unmarshal([]byte(msg.Payload), notification)
if err != nil {
grpclog.Warningf("Notifications: parse error: %v", err)
continue
}
if err := stream.Send(notification); err != nil {
grpclog.Warningf("Notifications: %v", err)
return err
}
grpclog.Infof("Notifications: send msg %v", notification)
}
}


-- 
You received this message because you are subscribed to the Google Groups 
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/grpc-io/ff154cbc-14e3-4a4a-971f-8948d43de4b2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to