gorpc

RPC client stuck infinite


I have created custom net TPC connection, and created RPC client with that connection. But it hang forever.

taskGenConn, err := net.Dial("tcp", cfg.TaskGeneratorRPCUrl)
if err != nil {
    return err
}


avsTaskGeneratorClient := rpc.NewClient(taskGenConn)
fmt.Println("going to infinite loop....")
err = avsTaskGeneratorClient.Call("UserRPC.AddUser", payload, &reply) // This point hang
if err != nil {
   return err
}

If I am doing with avsTaskGeneratorClient, err := rpc.DialHTTP("tcp", cfg.TaskGeneratorRPCUrl) and Call. It works as expected.

What I am doing wrong?


Solution

  • Your rpc client and server should use the same protocol, either over HTTP or over tcp directly. It seems that you have a rpc server runs over HTTP, but you try to call it over tcp directly. So the HTTP server will think your client send an incomplete HTTP reqeuest while your client thinks it has already sent an compelete rpc request through tcp and waiting for the response. So they just wait for each other endlessly. So keep the rpc client and server consistent.

    As for what you mentioned in the comment:

    Recently I tried writing some data, before creating the client, And it works.

    It's probably because you send something like "GET / HTTP/1.0\r\n\r\n" which happens to constitute a complete http request, so your HTTP server can responde to you.