title: Go gRPC simple service Asynchronous and Synchronous explanation
body: <p>I am trying to understand GoLang "Go" together with gRPC and to make a simple service scalable.</p>
  
  <p>Lets say I have a client1 that calls a service1(adds numbers) that calls service2(determines if the result is prime), and service2 returns the result to service1 that returns the result to client1 all via gRPC.</p>
  
  <p>When I use protocol buffers "proto3" and generate the Go code via protoc. 
  I get generated methods that call the service in one particular way.
  I see no distinction to call the methods asynchronously "Go".</p>
  
  <p>And the underlying call seems to be "Invoke" which I believe is synchronous,the call returns once a result is received.</p>
  
  <p>How do I make service1 "performant", I know I can run this in a cluster and have copies, but that would mean I can only serve clients as per the amount of instances within the cluster.</p>
  
  <p>I want a "single" service to be able to serve multiple clients(e.g. 1000) .</p>
  
  <p>Here is a simple server and I am not sure if this is performant or not:
  I do know that the <code>getprime</code> function does dial every time,
  and this could probably be moved to make this dial persist and be re-used; But more importantly I want to make a simple performant scaleable service and get a good understanding. </p>
  
  <p><strong>(A)</strong>
  Perhaps the whole design is incorrect and the service1 should just return
  as soon as the instruction is received "ack", do the addition and send the next request to sercice2 which determines if the answer is prime or not; again service2 just responds with an acknowledgement of the request being received. Once prime is determined by the service2 a call is made to the client with an answer. </p>
  
  <p>If (A) above is the better approach, then still please explain the bottlenecks below; what happens when multiple clients are processed?
  The call to "Listen" does what, "blocks, or does not block", etc.</p>
  
  <pre><code>package main
  
  import (
      pb "demo/internal/pkg/proto_gen/calc"
      "fmt"
      "golang.org/x/net/context"
      "google.golang.org/grpc"
      "google.golang.org/grpc/reflection"
      "log"
      "net"
  )
  
  const (
      port = ":8080"
  )
  
  type service struct {
  }
  
  func (s *service) Calculate(ctx context.Context, req *pb.Instruction) (*pb.Response, error) {
  
      var answer float64
      answer = req.Number1 + req.Number2
  
      // call service prime
      p := getprime(int(answer))
      pa := pb.PrimeAnswer{Prime: p}
      return &amp;pb.Response{Answer: answer, Prime: &amp;pa}, nil
  }
  
  const (
      primeAddress = "127.0.0.1:8089"
  )
  
  func getprime(number int) bool {
      conn, err := grpc.Dial(primeAddress, grpc.WithInsecure())
      if err != nil {
          log.Fatalf("Did not connect to prime service: %v", err)
      }
      defer conn.Close()
  
      client := pb.NewPrimeServiceClient(conn)
      p := pb.PrimeMessage{"", float64(number)}
  
      r, err := client.Prime(context.Background(), &amp;p)
      if err != nil {
          log.Fatalf("Call to prime service failed: %v", err)
      }
      return r.Prime
  }
  
  func main() {
      lis, err := net.Listen("tcp", port)
      if err != nil {
          log.Fatalf("failed to listen: %v", err)
      }
  
      s := grpc.NewServer()
      pb.RegisterCalculatorServer(s, &amp;service{})
      reflection.Register(s)
      if err := s.Serve(lis); err != nil {
          log.Fatalf("failed to serve: %v", err)
      }
  }
  </code></pre>