Browse Source

fix(app/log): prevent close of closed channel

The close of `done` channel may be called many times.
And the handler will be blocked when the log client exists and the
closure func is still not be called.
So use context to resolve those two problems.
Allo 2 years ago
parent
commit
4f2fc729ce
1 changed files with 3 additions and 3 deletions
  1. 3 3
      app/log/command/command.go

+ 3 - 3
app/log/command/command.go

@@ -43,18 +43,18 @@ func (s *LoggerServer) FollowLog(_ *FollowLogRequest, stream LoggerService_Follo
 	if !ok {
 	if !ok {
 		return newError("logger not support following")
 		return newError("logger not support following")
 	}
 	}
-	done := make(chan struct{})
+	ctx, cancel := context.WithCancel(stream.Context())
 	f := func(msg cmlog.Message) {
 	f := func(msg cmlog.Message) {
 		err := stream.Send(&FollowLogResponse{
 		err := stream.Send(&FollowLogResponse{
 			Message: msg.String(),
 			Message: msg.String(),
 		})
 		})
 		if err != nil {
 		if err != nil {
-			close(done)
+			cancel()
 		}
 		}
 	}
 	}
 	follower.AddFollower(f)
 	follower.AddFollower(f)
 	defer follower.RemoveFollower(f)
 	defer follower.RemoveFollower(f)
-	<-done
+	<-ctx.Done()
 	return nil
 	return nil
 }
 }