From 7050a0ec5cdff61cd289bc8d03dbdd7d46bcda0d Mon Sep 17 00:00:00 2001
From: xingweizheng <xingweizheng@huawei.com>
Date: Sat, 28 Aug 2021 21:56:02 +0800
Subject: [PATCH 16/20] clean code: delete channel within the same goroutine

---
 daemon/manifest.go | 13 ++-----------
 daemon/pull.go     | 13 ++-----------
 daemon/push.go     | 13 ++-----------
 3 files changed, 6 insertions(+), 33 deletions(-)

diff --git a/daemon/manifest.go b/daemon/manifest.go
index fc28998f..36e76749 100644
--- a/daemon/manifest.go
+++ b/daemon/manifest.go
@@ -212,18 +212,9 @@ func (b *Backend) ManifestPush(req *pb.ManifestPushRequest, stream pb.Control_Ma
 	eg, egCtx := errgroup.WithContext(stream.Context())
 	eg.Go(manifestPushHandler(egCtx, opt))
 	eg.Go(manifestPushMessageHandler(stream, cliLogger))
-	errC := make(chan error, 1)
 
-	errC <- eg.Wait()
-	defer close(errC)
-
-	err, ok := <-errC
-	if !ok {
-		logrus.WithField(util.LogKeySessionID, manifestName).Info("Channel errC closed")
-		return nil
-	}
-	if err != nil {
-		logrus.WithField(util.LogKeySessionID, manifestName).Warnf("Stream closed with: %v", err)
+	if err := eg.Wait(); err != nil {
+		logrus.WithField(util.LogKeySessionID, manifestName).Warnf("Manifest push stream closed with: %v", err)
 		return err
 	}
 
diff --git a/daemon/pull.go b/daemon/pull.go
index 6d2e33d9..90be2a91 100644
--- a/daemon/pull.go
+++ b/daemon/pull.go
@@ -62,18 +62,9 @@ func (b *Backend) Pull(req *pb.PullRequest, stream pb.Control_PullServer) error
 	eg, egCtx := errgroup.WithContext(ctx)
 	eg.Go(pullHandler(egCtx, opt))
 	eg.Go(pullMessageHandler(stream, opt.logger))
-	errC := make(chan error, 1)
 
-	errC <- eg.Wait()
-	defer close(errC)
-
-	err, ok := <-errC
-	if !ok {
-		logrus.WithField(util.LogKeySessionID, opt.pullID).Info("Channel errC closed")
-		return nil
-	}
-	if err != nil {
-		logrus.WithField(util.LogKeySessionID, opt.pullID).Warnf("Stream closed with: %v", err)
+	if err := eg.Wait(); err != nil {
+		logrus.WithField(util.LogKeySessionID, opt.pullID).Warnf("Pull stream closed with: %v", err)
 		return err
 	}
 
diff --git a/daemon/push.go b/daemon/push.go
index e36198dc..d3f5571e 100644
--- a/daemon/push.go
+++ b/daemon/push.go
@@ -84,18 +84,9 @@ func (b *Backend) Push(req *pb.PushRequest, stream pb.Control_PushServer) error
 
 	eg.Go(pushHandler(egCtx, opt))
 	eg.Go(pushMessageHandler(stream, opt.logger))
-	errC := make(chan error, 1)
 
-	errC <- eg.Wait()
-	defer close(errC)
-
-	err, ok := <-errC
-	if !ok {
-		logrus.WithField(util.LogKeySessionID, opt.pushID).Info("Channel errC closed")
-		return nil
-	}
-	if err != nil {
-		logrus.WithField(util.LogKeySessionID, opt.pushID).Warnf("Stream closed with: %v", err)
+	if err := eg.Wait(); err != nil {
+		logrus.WithField(util.LogKeySessionID, opt.pushID).Warnf("Push stream closed with: %v", err)
 		return err
 	}
 
-- 
2.31.1

