Created
April 7, 2014 20:36
-
-
Save Smerity/10047616 to your computer and use it in GitHub Desktop.
Go hangs when encountering a type it doesn't know how to handle during RPC + deserialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/gob" | |
"fmt" | |
"log" | |
"net" | |
"net/rpc" | |
) | |
type HiddenStruct struct { | |
Says string | |
} | |
type Reply struct { | |
Data interface{} | |
} | |
type Server struct{} | |
func (this *Server) Ping(i int64, reply *Reply) error { | |
reply.Data = HiddenStruct{"pong"} | |
fmt.Println("Server replied to ping with", reply.Data) | |
return nil | |
} | |
func handleError(err error) { | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
func server(ready chan bool) { | |
rpc.Register(new(Server)) | |
ln, err := net.Listen("tcp", ":9999") | |
handleError(err) | |
ready <- true | |
for { | |
c, err := ln.Accept() | |
if err != nil { | |
continue | |
} | |
go rpc.ServeConn(c) | |
} | |
} | |
func client() { | |
c, err := rpc.Dial("tcp", ":9999") | |
handleError(err) | |
var result Reply | |
fmt.Println("Sending ping...") | |
err = c.Call("Server.Ping", int64(999), &result) | |
fmt.Println("Received pong...") | |
handleError(err) | |
fmt.Println("Reply to ping: ", result) | |
} | |
func main() { | |
gob.Register(Reply{}) | |
//gob.Register(HiddenStruct{}) | |
serverIsReady := make(chan bool) | |
go server(serverIsReady) | |
<-serverIsReady | |
client() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment