Skip to content

Working from the helloworld example but getting an error on HTTP requests: grpc: the client connection is closing. Would like to improve the docs. #1924

Closed
@seantcanavan

Description

@seantcanavan

I'm a huge fan of the library and I've been hard at work for over a week trying to massage the docs into something that compiles, runs, and works. I feel I'm extremely close because my GRPC requests successfully return but my HTTP GET requests return 408 apparently?

http curl output:

Mon Jan 25 08:37 PM golang: curl -vvv http://localhost:8080/v1/saygoodbye/Sean
*   Trying 127.0.0.1:8080...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /v1/saygoodbye/Sean HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 408 Request Timeout
< Content-Type: application/json
< Date: Tue, 26 Jan 2021 02:51:17 GMT
< Content-Length: 74
< 
* Connection #0 to host localhost left intact
{"code":1,"message":"grpc: the client connection is closing","details":[]}

the guts of server/main.go:

func runGRPCServer() {
	lis, err := net.Listen("tcp", gw.GrpcPort)
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}
	s := grpc.NewServer()
	gw.RegisterGreeterServer(s, &gw.GreeterServerImplementation{})

	go func() {
		fmt.Printf("Serving GRPC on 0.0.0.0%s\n", gw.GrpcPort)
		if err := s.Serve(lis); err != nil {
			log.Fatalf("failed to serve: %v", err)
		}
	}()
}

func runHTTPServer() {
	ctx := context.Background()
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()

	// Register gRPC server endpoint
	// Note: Make sure the gRPC server is running properly and accessible
	mux := runtime.NewServeMux()
	opts := []grpc.DialOption{grpc.WithInsecure()}
	grpcServerEndpoint := flag.String("grpc-server-endpoint",  "localhost" + gw.GrpcPort, "gRPC server endpoint")
	err := gw.RegisterGreeterHandlerFromEndpoint(ctx, mux,  *grpcServerEndpoint, opts)
	if err != nil {
		log.Fatalf("failed to register ")
	}

	go func() {
		fmt.Println("go func http.ListenAndServe")
		if err := http.ListenAndServe(gw.HttpPort, mux); err != nil {
			glog.Fatal(err)
		}
	}()
}

func main() {
	flag.Parse()
	defer glog.Flush()

	osSignals := make(chan os.Signal, 1)
	signal.Notify(osSignals, syscall.SIGINT, syscall.SIGTERM)

	runGRPCServer()
	runHTTPServer()

	osSignal := <-osSignals
	fmt.Println(osSignal)
	fmt.Println("Exiting")
}

helloworld/greeter_server.go:

const (
	HttpPort = ":8080"
	GrpcPort = ":8081"
)

type GreeterServerImplementation struct { }

func (i GreeterServerImplementation) mustEmbedUnimplementedGreeterServer() {
	panic("implement me") 	// NO IDEA WHAT TO DO HERE
}

func (GreeterServerImplementation) SayHello(_ context.Context, request *HelloRequest) (*HelloReply, error) {
	return &HelloReply{
		Message:       fmt.Sprintf("Hello, %s.", request.Name),
	}, nil
}
func (GreeterServerImplementation) SayGoodbye(_ context.Context, request *GoodbyeRequest) (*GoodbyeReply, error) {
	return &GoodbyeReply{
		Message:       fmt.Sprintf("Goodbye, %s.", request.Name),
	}, nil
}

client/main.go:

func main() {
	// Perform GRPC requests first
	MakeGrpcRequests()

	// Perform HTTP requests after
	MakeHelloHTTPRequest()
	MakeGoodbyeHTTPRequest()
}

func MakeGrpcRequests() {
	// Set up a connection to the server.
	conn, err := grpc.Dial(baseGrpcAddress, grpc.WithInsecure(), grpc.WithBlock())
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c := gw.NewGreeterClient(conn)

	// Contact the server and print out its response.
	name := defaultName
	if len(os.Args) > 1 {
		name = os.Args[1]
	}
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	r1, err := c.SayHello(ctx, &gw.HelloRequest{Name: name})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	log.Printf("Greeting: %s", r1.GetMessage()) // SUCCESS

	r2, err := c.SayGoodbye(ctx, &gw.GoodbyeRequest{Name: name})
	if err != nil {
		log.Fatalf("could not say goodbye: %v", err)
	}
	log.Printf("Goodbye: %s", r2.GetMessage()) // SUCCESS
}

func MakeHelloHTTPRequest() {
	resp, err := http.Get(baseHttpAddress + gw.HttpPort + "/v1/sayhello/Sean")
	if err != nil {
		log.Fatalln(err)
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatalln(err)
	}

	log.Println(string(body)) // ERROR {"code":1,"message":"grpc: the client connection is closing","details":[]}
}

func MakeGoodbyeHTTPRequest() {
	resp, err := http.Get(baseHttpAddress + gw.HttpPort + "/v1/saygoodbye/Sean")
	if err != nil {
		log.Fatalln(err)
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatalln(err)
	}

	log.Println(string(body)) // ERROR {"code":1,"message":"grpc: the client connection is closing","details":[]}
}

Any ideas would be great and once I get everything working I can update the code in examples/helloworld.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions