Aech
Aech

Reputation: 43

GRPC Web Request using golang client?

I am trying to create a grpc server with the hep of grpc-web wrapper. The idea is to use this grpc server both with browser based application as well as with the normal grpc client. But i am confused how can i make it work for both the applications?

package main

import (
    "context"
    "fmt"
    "io"
    "log"
    "net/http"
    "strconv"
    "time"

    "github.com/repo/test-grpc-server/greet/greetpb"
    "github.com/improbable-eng/grpc-web/go/grpcweb"
    "google.golang.org/grpc"
)

type server struct{}

func (*server) Greet(ctx context.Context, req *greetpb.GreetRequest) (*greetpb.GreetResponse, error) {
    fmt.Printf("Greet function was invoked with %v", req)
    firstName := req.GetGreeting().GetFirstName()
    result := "Hello " + firstName
    res := greetpb.GreetResponse{
        Result: result,
    }
    return &res, nil
}

func (*server) GreetManyTimes(req *greetpb.GreetManyTimesRequest, stream greetpb.GreetService_GreetManyTimesServer) error {
    fmt.Printf("GreetMany function was invoked with %v", req)

    firstName := req.GetGreeting().GetFirstName()

    for i := 0; i < 10; i++ {
        result := "Hello " + firstName + " number " + strconv.Itoa(i)
        res := &greetpb.GreetManyTimesResponse{
            Result: result,
        }
        stream.Send(res)
        time.Sleep(1000 * time.Millisecond)
    }
    return nil

}
func (*server) LongGreet(stream greetpb.GreetService_LongGreetServer) error {
    fmt.Printf("LongGreet function was invoked with a streaming request\n")
    result := ""
    for {
        req, err := stream.Recv()
        if err == io.EOF {
            // we have finished reading the client stream
            return stream.SendAndClose(&greetpb.LongGreetResponse{
                Result: result,
            })
        }
        if err != nil {
            log.Fatalf("Error while reading client stream: %v", err)
        }

        firstName := req.GetGreeting().GetFirstName()
        result += "Hello " + firstName + "! "
    }
}



func main() {
    fmt.Println("Go gRPC Server")
    /*lis, err := net.Listen("tcp", ":5051")
    if err != nil {
        log.Fatalf("Failed to listen: %v", err)
    }*/
    grpcServer := grpc.NewServer()
    greetpb.RegisterGreetServiceServer(grpcServer, &server{})
    grpc := grpcweb.WrapServer(grpcServer)
    http.HandleFunc("/", func(resp http.ResponseWriter, req *http.Request) {
        allowCors(resp, req)
        if grpc.IsGrpcWebRequest(req) || grpc.IsAcceptableGrpcCorsRequest(req) {
            grpc.ServeHTTP(resp, req)
        }
    })
    httpPort := ":50051"
    fmt.Println("HTTP server listening on", httpPort)
    err := http.ListenAndServe(httpPort, nil)
    if err != nil {
        log.Fatal("Failed to start a HTTP server:", err)
    }

}

func allowCors(resp http.ResponseWriter, req *http.Request) {
    resp.Header().Set("Access-Control-Allow-Origin", "*")
    resp.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
    resp.Header().Set("Access-Control-Expose-Headers", "grpc-status, grpc-message")
    resp.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, XMLHttpRequest, x-user-agent, x-grpc-web, grpc-status, grpc-message")
}

Now i am trying to call the grpc function from my client like this below. but it is not working..

package main

import (
    "context"
    "fmt"
    "io"
    "log"
    "time"

    "github.com/repo/test-grpc-server/greet/greetpb"
    "github.com/repo/test-grpc-server/sum/sumpb"
    "google.golang.org/grpc"
)

func main() {

    fmt.Println("Hello I'm a client")
    conn, err := grpc.Dial("0.0.0.0:50051", grpc.WithInsecure())
    if err != nil {
        log.Fatalf("could not connect: %v", err)
    }
    defer conn.Close()

    c := greetpb.NewGreetServiceClient(conn)

    doUnary(c)
}
func doUnary(c greetpb.GreetServiceClient) {
    fmt.Println("do unary from the client")
    req := &greetpb.GreetRequest{
        Greeting: &greetpb.Greeting{
            FirstName: "Hsn",
            LastName:  "Hrn",
        },
    }
    res, err := c.Greet(context.Background(), req)
    if err != nil {
        log.Fatalf("error while calling Greet RPC: %v", err)
    }
    log.Printf("Response from Greet: %v", res.Result)

}

My .prot file look like this..

syntax = "proto3";

package greet;
option go_package="./greet/greetpb";

message Greeting {
    string first_name =1 ;
    string last_name = 2;
}

message GreetRequest {
    Greeting greeting = 1;
}

message GreetResponse {
    string result = 1;
}

message GreetManyTimesRequest{
    Greeting greeting =1;
}

message GreetManyTimesResponse{
    string result=1;
}
message LongGreetRequest {
    Greeting greeting = 1;


}

message LongGreetResponse{
    string result = 1;
}


service GreetService{
    //Unary
    rpc Greet (GreetRequest) returns (GreetResponse) {};
    //Server Streaming 
    rpc GreetManyTimes(GreetManyTimesRequest) returns (stream GreetManyTimesResponse) {};
    //Client Streaming 
    rpc LongGreet(stream LongGreetRequest) returns (LongGreetResponse) {};

}

The error logs i get are ..

Hello I'm a client
INFO: 2021/04/27 12:53:17 [core] parsed scheme: ""
INFO: 2021/04/27 12:53:17 [core] scheme "" not registered, fallback to default scheme
INFO: 2021/04/27 12:53:17 [core] ccResolverWrapper: sending update to cc: {[{0.0.0.0:50051  <nil> 0 <nil>}] <nil> <nil>}
INFO: 2021/04/27 12:53:17 [core] ClientConn switching balancer to "pick_first"
INFO: 2021/04/27 12:53:17 [core] Channel switches to new LB policy "pick_first"
INFO: 2021/04/27 12:53:17 [core] Subchannel Connectivity change to CONNECTING
INFO: 2021/04/27 12:53:17 [core] parsed scheme: ""
INFO: 2021/04/27 12:53:17 [core] scheme "" not registered, fallback to default scheme
INFO: 2021/04/27 12:53:17 [core] ccResolverWrapper: sending update to cc: {[{0.0.0.0:50051  <nil> 0 <nil>}] <nil> <nil>}
INFO: 2021/04/27 12:53:17 [core] ClientConn switching balancer to "pick_first"
INFO: 2021/04/27 12:53:17 [core] Channel switches to new LB policy "pick_first"
INFO: 2021/04/27 12:53:17 [core] Subchannel Connectivity change to CONNECTING
do unary from the client
INFO: 2021/04/27 12:53:17 [core] blockingPicker: the picked transport is not ready, loop back to repick
INFO: 2021/04/27 12:53:17 [core] Subchannel picks a new address "0.0.0.0:50051" to connect
INFO: 2021/04/27 12:53:17 [core] pickfirstBalancer: UpdateSubConnState: 0xc000021cd0, {CONNECTING <nil>}
INFO: 2021/04/27 12:53:17 [core] Channel Connectivity change to CONNECTING
INFO: 2021/04/27 12:53:17 [core] Subchannel picks a new address "0.0.0.0:50051" to connect
INFO: 2021/04/27 12:53:17 [core] pickfirstBalancer: UpdateSubConnState: 0xc000021ed0, {CONNECTING <nil>}
INFO: 2021/04/27 12:53:17 [core] Channel Connectivity change to CONNECTING
INFO: 2021/04/27 12:53:17 [core] Subchannel Connectivity change to TRANSIENT_FAILURE
INFO: 2021/04/27 12:53:17 [transport] transport: loopyWriter.run returning. connection error: desc = "transport is closing"
INFO: 2021/04/27 12:53:17 [transport] transport: loopyWriter.run returning. connection error: desc = "transport is closing"
INFO: 2021/04/27 12:53:17 [core] Subchannel Connectivity change to TRANSIENT_FAILURE
INFO: 2021/04/27 12:53:17 [core] pickfirstBalancer: UpdateSubConnState: 0xc000021ed0, {TRANSIENT_FAILURE connection closed}
INFO: 2021/04/27 12:53:17 [core] Channel Connectivity change to TRANSIENT_FAILURE
INFO: 2021/04/27 12:53:17 [core] pickfirstBalancer: UpdateSubConnState: 0xc000021cd0, {TRANSIENT_FAILURE connection closed}
INFO: 2021/04/27 12:53:17 [core] Channel Connectivity change to TRANSIENT_FAILURE
2021/04/27 12:53:17 error while calling Greet RPC: rpc error: code = Unavailable desc = connection closed
exit status 1

Someone help would be really appreciated. Thank you!

Upvotes: 4

Views: 4826

Answers (1)

Brits
Brits

Reputation: 18245

As per the comments the issue is that you are were attempting to connect to a gRPC-Web server using a gRPC client. gRPC and gRPC-Web are different wire protocols (gRPC-Web was created because web browser APIs don't provide sufficient control over HTTP/2 requests to implement gRPC). This blog post provides a good overview.

Because you are building a web-app you will need to use gRPC-Web; if you also wish to connect to your server using a go client then the preferred option is to use gRPC (the server can both simultaneously). Another option that could work would be to use a gRPC-Web client but I've not tried this (it will be less efficient).

The 'official' way of running gRPC-Web is via an envoy plugin but as you are writing this in Go improbable-eng/grpc-web provides another, simpler, option which you are already utilising (they also have a proxy but that makes deployment more complex).

Your server needs to be altered to run both gRPC and gRPC-Web. The simplest option is to run these on different ports (it may be possible to use a mux to detect the content-type but this is not something I've tried; it does work well if you want to serve html/js and gRPC-Web on a single port).

The approach I'd take to run both servers follows (please treat this as incomplete pseudo code, I have pulled bits from a few of my applications but have not compiled/tested etc; feel free to update when you discover issues!):

grpcServer := grpc.NewServer()
greetpb.RegisterGreetServiceServer(grpcServer, &server{})

// Your application is probably doing other things and you will want to be
// able to shutdown cleanly; passing in a context is a good method..
ctx, cancel = context.Cancel(context.Background())
defer cancel()   // Ensure cancel function is called eventually

// Start the grpc server on port 50050
grpcTerminated := make(chan struct{})
lis, err := net.Listen("tcp", ":50050")
if err != nil {
    panic(fmt.Sprintf("gRPC - failed to listen: %s", err))
}

go func() {
    if sErr := grpcServer.Serve(lis); sErr != nil {
       fmt.Printf("grpc server shutdown: %s", err)       
    }
    close(grpcTerminated) // In case server is terminated without us requesting this
}()

// Start the grpc-Web server on port 5051
grpcWebTerminated := make(chan struct{})
grpc := grpcweb.WrapServer(grpcServer)
mux := http.NewServeMux()
mux.HandleFunc("/", func(resp http.ResponseWriter, req *http.Request) {
   allowCors(resp, req)
   if grpc.IsGrpcWebRequest(req) || grpc.IsAcceptableGrpcCorsRequest(req) {
      grpc.ServeHTTP(resp, req)
    }
}))
rpcWebServer := &http.Server{
        Handler: mux,
        Addr:    ":50051"}

wg.Add(1)
go func() {
   defer wg.Done()
   if err := rpcWebServer.ListenAndServe(); err != nil {
      fmt.Printf("Web server (GRPC) shutdown: %s", err)
   }
   close(grpcWebTerminated) // In case server is terminated without us requesting this
}()

// Wait for the web server to shutdown OR the context to be cancelled...
select {
   case <-ctx.Done():
      // Shutdown the servers (there are shutdown commands to request this)
   case <-grpcTerminated:
      // You may want to exit if this happens (will be due to unexpected error)
   case <-grpcWebTerminated:
      // You may want to exit if this happens (will be due to unexpected error)
}

// Wait for the goRoutines to complete
<-grpcTerminated:
<-grpcWebTerminated

Upvotes: 7

Related Questions