17

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?

dgh
  • 8,969
  • 9
  • 38
  • 49

1 Answers1

10

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.

Community
  • 1
  • 1
Verran
  • 3,942
  • 2
  • 14
  • 22