From 46cf899c1e895325748d11881e5da57133da16d3 Mon Sep 17 00:00:00 2001
From: Suvarna <suvarnar@mkcl.org>
Date: Wed, 10 Jul 2019 11:39:11 +0530
Subject: [PATCH] grpc related changes

grpc related changes
---
 grpcclientmdl/grpcclientmdl.go   | 57 +++++++++++++++++++++-----------
 routebuildermdl/grpcservermdl.go |  9 +++--
 2 files changed, 43 insertions(+), 23 deletions(-)

diff --git a/grpcclientmdl/grpcclientmdl.go b/grpcclientmdl/grpcclientmdl.go
index 976a412..6dcd0a3 100644
--- a/grpcclientmdl/grpcclientmdl.go
+++ b/grpcclientmdl/grpcclientmdl.go
@@ -2,11 +2,12 @@ package grpcclientmdl
 
 import (
 	"context"
+	"errors"
 	"time"
 
 	"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/grpcbuildermdl"
 	"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
-	"github.com/processout/grpc-go-pool"
+	grpcpool "github.com/processout/grpc-go-pool"
 	"google.golang.org/grpc"
 )
 
@@ -59,13 +60,45 @@ type Employee struct {
 }
 
 var instances map[string]*grpcpool.Pool
+var instancesList map[string]map[string]*grpcpool.Pool
 
 // Init init
-func Init() {
+func Init(grpcServerURLList []string) {
+
 	instances = make(map[string]*grpcpool.Pool)
+	loggermdl.LogDebug("Length of grpcServerURLList", len(grpcServerURLList))
+
+	for index := 0; index < len(grpcServerURLList); index++ {
+		CreateConnection(grpcServerURLList[index])
+	}
+	loggermdl.LogDebug("instances", instances)
+}
+
+//ByteHandler ByteHandler
+func ByteHandler(req *grpcbuildermdl.GRPCRequest, grpcServerURL string) ([]byte, error) {
+	if instances[grpcServerURL] != nil {
+		conn, err := instances[grpcServerURL].Get(context.Background())
+		if err != nil {
+			loggermdl.LogError("Failed to create gRPC pool: %v", err)
+		}
+
+		client := grpcbuildermdl.NewGRPCServiceClient(conn.ClientConn)
+
+		res, err := client.GRPCHandler(context.Background(), req)
+		if err != nil {
+			loggermdl.LogError("GRPCHandler err:", err)
+		}
+		return res.GetData(), nil
+	}
+	return []byte{}, errors.New("no grpc connection found")
+}
+
+// CreateConnection method
+func CreateConnection(serverURL string) {
 	var factory grpcpool.Factory
+	// serverURL := grpcServerURLList[index]
 	factory = func() (*grpc.ClientConn, error) {
-		conn, err := grpc.Dial("0.0.0.0:50051", grpc.WithInsecure())
+		conn, err := grpc.Dial(serverURL, grpc.WithInsecure())
 		if err != nil {
 			loggermdl.LogError("Failed to start gRPC connection: %v", err)
 		}
@@ -78,21 +111,5 @@ func Init() {
 	if err != nil {
 		loggermdl.LogError("Failed to create gRPC pool: %v", err)
 	}
-	instances["0.0.0.0:50051"] = pool
-}
-
-//ByteHandler ByteHandler
-func ByteHandler(req *grpcbuildermdl.GRPCRequest) ([]byte, error) {
-
-	conn, err := instances["0.0.0.0:50051"].Get(context.Background())
-	if err != nil {
-		loggermdl.LogError("Failed to create gRPC pool: %v", err)
-	}
-	client := grpcbuildermdl.NewGRPCServiceClient(conn.ClientConn)
-
-	res, err := client.GRPCHandler(context.Background(), req)
-	// var emp Employee
-	// json.Unmarshal(res.GetData(), emp)
-	// loggermdl.LogInfo("<ByteHandler>", res)
-	return res.GetData(), nil
+	instances[serverURL] = pool
 }
diff --git a/routebuildermdl/grpcservermdl.go b/routebuildermdl/grpcservermdl.go
index 89a07d2..d419c18 100644
--- a/routebuildermdl/grpcservermdl.go
+++ b/routebuildermdl/grpcservermdl.go
@@ -19,12 +19,13 @@ import (
 type Server struct{}
 
 // GRPCInit init
-func GRPCInit() {
+func GRPCInit(GRPCPort string, serverStatus chan string) {
 	loggermdl.LogInfo("GRPC init called")
-
-	list, err := net.Listen("tcp", "0.0.0.0:50051")
+	serverStatusString := "Sucess"
+	list, err := net.Listen("tcp", "0.0.0.0:"+GRPCPort)
 	if err != nil {
 		// log.Fatal("fail to listen: %v", err)
+		serverStatusString = "Failed"
 		loggermdl.LogError("GRPC listening failed: %v", err)
 
 	}
@@ -33,8 +34,10 @@ func GRPCInit() {
 	grpcbuildermdl.RegisterGRPCServiceServer(s, &Server{})
 	if err := s.Serve(list); err != nil {
 		// log.Fatal("Unable to start: %v", err)
+		serverStatusString = "Failed"
 		loggermdl.LogError("Unable to start GRPC server: %v", err)
 	}
+	serverStatus <- serverStatusString
 }
 
 type Employee struct {
-- 
GitLab