Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang RPC http.Serve vs rpc.ServeConn (HTTP vs raw connection)

The Go net/rpc library documentation enables exposing an object across a network, either via raw network connections or via HTTP.

HTTP Example

arith := new(Arith)
rpc.Register(arith)
rpc.HandleHTTP()
l, e := net.Listen("tcp", ":1234")
if e != nil {
    log.Fatal("listen error:", e)
}
go http.Serve(l, nil)

Raw TCP Network Connection

arith := new(Arith)
rpc.Register(arith)
l, e := net.Listen("tcp", ":1234")
if e != nil {
    log.Fatal("listen error:", e)
}
go func() {
    for {
        conn, err := l.Accept()
        go rpc.ServeConn(conn)
    } 
}

To call the first type of server, one would use rpc.DialHTTP("tcp", "127.0.0.1:1234") and for the second type rpc.Dial("tcp", "127.0.0.1:1234") would be used.

My question is how are these two really different? What pros/cons are there to running an HTTP server vs. a "raw network connection" server? Can one perform RPCs via curl or the browser in some way with HTTP? Is the HTTP version useful for cross-language RPC invocation?

like image 883
dgh Avatar asked Oct 28 '13 16:10

dgh


1 Answers

This question has an answer with a good description about the differences between HTTP and raw TCP in general (not directly concerning Go).

TCP Vs. Http Benchmark

It basically says that since HTTP is a layer on top of TCP for standardization, it is something you should probably use if you plan on having your code having a webpage trying to make requests and processing the response, but if all you care about is speed, leave the HTTP off and go with raw TCP.

like image 145
Verran Avatar answered Sep 23 '22 19:09

Verran