From 959d9cf363e7b8f06307736e86d69df227dba0fd Mon Sep 17 00:00:00 2001
From: Manish R Jain <manishrjain@gmail.com>
Date: Tue, 26 Apr 2016 19:36:45 +1000
Subject: [PATCH] Fix bugs: Set GRPC server in Goroutine. Break out of loop if
 ServeRequest returns error -- this was causing the 100% CPU usage issue.

---
 server/main.go   | 8 ++++----
 worker/worker.go | 6 +++++-
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/server/main.go b/server/main.go
index a5e53c18..887677d1 100644
--- a/server/main.go
+++ b/server/main.go
@@ -252,11 +252,11 @@ func (s *server) Query(ctx context.Context,
 
 // This function register a DGraph grpc server on the address, which is used
 // exchanging protocol buffer messages.
-func runGrpcServer(address string) error {
+func runGrpcServer(address string) {
 	ln, err := net.Listen("tcp", address)
 	if err != nil {
 		glog.Fatalf("While running server for client: %v", err)
-		return err
+		return
 	}
 	glog.WithField("address", ln.Addr()).Info("Client Worker listening")
 
@@ -265,7 +265,7 @@ func runGrpcServer(address string) error {
 	if err = s.Serve(ln); err != nil {
 		glog.Fatalf("While serving gRpc requests", err)
 	}
-	return nil
+	return
 }
 
 func main() {
@@ -313,7 +313,7 @@ func main() {
 
 	worker.Connect(addrs)
 	// Grpc server runs on (port + 1)
-	runGrpcServer(fmt.Sprintf(":%d", *port+1))
+	go runGrpcServer(fmt.Sprintf(":%d", *port+1))
 
 	http.HandleFunc("/query", queryHandler)
 	glog.WithField("port", *port).Info("Listening for requests...")
diff --git a/worker/worker.go b/worker/worker.go
index 64d1c9e6..b163cfdd 100644
--- a/worker/worker.go
+++ b/worker/worker.go
@@ -162,7 +162,11 @@ func serveRequests(irwc io.ReadWriteCloser) {
 		sc := &conn.ServerCodec{
 			Rwc: irwc,
 		}
-		rpc.ServeRequest(sc)
+		glog.Info("Serving request from serveRequests")
+		if err := rpc.ServeRequest(sc); err != nil {
+			glog.WithField("method", "serveRequests").Info(err)
+			break
+		}
 	}
 }
 
-- 
GitLab