func InitCometRpc(addrs map[int32]string) (err error) { for serverID, addrs := range addrs { var ( rpcClient *protorpc.Client quit chan struct{} network, addr string ) if network, addr, err = inet.ParseNetwork(addrs); err != nil { log.Error("inet.ParseNetwork() error(%v)", err) return } rpcClient, err = protorpc.Dial(network, addr) if err != nil { log.Error("protorpc.Dial(\"%s\") error(%s)", addr, err) return } go protorpc.Reconnect(&rpcClient, quit, network, addr) log.Info("rpc addr:%s connected", addr) cometServiceMap[serverID] = &rpcClient } return }
func InitRouter() (err error) { var ( network, addr string ) routerRing = ketama.NewRing(ketama.Base) for serverId, addrs := range Conf.RouterRPCAddrs { // WARN r must every recycle changed for reconnect var ( r *rpc.Client routerQuit = make(chan struct{}, 1) ) if network, addr, err = inet.ParseNetwork(addrs); err != nil { log.Error("inet.ParseNetwork() error(%v)", err) return } r, err = rpc.Dial(network, addr) if err != nil { log.Error("rpc.Dial(\"%s\", \"%s\") error(%s)", network, addr, err) } go rpc.Reconnect(&r, routerQuit, network, addr) log.Debug("router rpc addr:%s connect", addr) routerServiceMap[serverId] = &r routerRing.AddNode(serverId, 1) } routerRing.Bake() return }
func TestRouterConnect(t *testing.T) { c, err := rpc.Dial("tcp", "localhost:7270") if err != nil { t.Error(err) t.FailNow() } arg := &proto.ConnArg{} reply := &proto.ConnReply{} arg.UserId = 1 arg.Server = 0 if err = c.Call("RouterRPC.Connect", arg, reply); err != nil { t.Error(err) t.FailNow() } if reply.Seq != 1 { t.Errorf("reply seq: %d not equal 0", reply.Seq) t.FailNow() } arg.UserId = 1 arg.Server = 0 if err = c.Call("RouterRPC.Connect", arg, reply); err != nil { t.Error(err) t.FailNow() } if reply.Seq != 2 { t.Errorf("reply seq: %d not equal 1", reply.Seq) t.FailNow() } }
func TestRouterGet(t *testing.T) { c, err := rpc.Dial("tcp", "localhost:7270") if err != nil { t.Error(err) t.FailNow() } arg := &proto.GetArg{} reply := &proto.GetReply{} arg.UserId = 1 if err = c.Call("RouterRPC.Get", arg, reply); err != nil { t.Error(err) t.FailNow() } if len(reply.Seqs) != 2 || len(reply.Servers) != 2 { t.Errorf("reply seqs||servers length not equals 2") t.FailNow() } if reply.Seqs[0] != 1 || reply.Seqs[1] != 2 { t.Error("reply seqs not match") t.FailNow() } if reply.Servers[0] != 0 || reply.Servers[1] != 0 { t.Errorf("reply servers not match, %v", reply.Servers) t.FailNow() } }
func TestRouterDisconnect(t *testing.T) { c, err := rpc.Dial("tcp", "localhost:7270") if err != nil { t.Error(err) t.FailNow() } arg := &proto.ConnArg{} reply := &proto.ConnReply{} arg.UserId = 2 arg.Server = 0 if err = c.Call("RouterRPC.Connect", arg, reply); err != nil { t.Error(err) t.FailNow() } if reply.Seq != 1 { t.Errorf("reply seq: %d not equal 0", reply.Seq) t.FailNow() } arg1 := &proto.DisconnArg{} arg1.UserId = 2 arg1.Seq = 1 reply1 := &proto.DisconnReply{} if err = c.Call("RouterRPC.Disconnect", arg1, reply1); err != nil { t.Error(err) t.FailNow() } if !reply1.Has { t.Errorf("reply has: %d not equal true", reply1.Has) t.FailNow() } }
func InitLogicRpc(network, addr string) (err error) { logicRpcClient, err = protorpc.Dial(network, addr) if err != nil { log.Error("rpc.Dial(\"%s\", \"%s\") error(%s)", network, addr, err) } go protorpc.Reconnect(&logicRpcClient, logicRpcQuit, network, addr) log.Debug("logic rpc addr %s:%s connected", network, addr) return }
func InitLogicRpc(addr string, retry time.Duration) (err error) { logicRpcClient, err = protorpc.Dial("tcp", addr) if err != nil { log.Error("rpc.Dial(\"%s\") error(%s)", addr, err) return } go rpcPing(addr, logicRpcClient, retry) log.Debug("logic rpc addr:%s connected", addr) return }
func InitLogicRpc(addrs string) (err error) { var network, addr string if network, addr, err = inet.ParseNetwork(addrs); err != nil { log.Error("inet.ParseNetwork() error(%v)", err) return } logicRpcClient, err = protorpc.Dial(network, addr) if err != nil { log.Error("rpc.Dial(\"%s\", \"%s\") error(%s)", network, addr, err) } go protorpc.Reconnect(&logicRpcClient, logicRpcQuit, network, addr) log.Debug("logic rpc addr %s:%s connected", network, addr) return }
func InitCometRpc(addrs map[int32]string) (err error) { for serverID, addr := range addrs { var rpcClient *protorpc.Client rpcClient, err = protorpc.Dial("tcp", addr) if err != nil { log.Error("protorpc.Dial(\"%s\") error(%s)", addr, err) return } var quit chan struct{} go protorpc.Reconnect(&rpcClient, quit, "tcp", addr) log.Info("rpc addr:%s connected", addr) cometServiceMap[serverID] = rpcClient } return }
func testNewServerRPC(t *testing.T, addr string) { client, err := protorpc.Dial("tcp", addr) if err != nil { t.Fatal("dialing", err) } defer client.Close() // Synchronous calls args := &Args{7, 8} reply := new(Reply) err = client.Call("newServer.Arith.Add", args, reply) if err != nil { t.Errorf("Add: expected no error but got string %q", err.Error()) } if reply.C != args.A+args.B { t.Errorf("Add: expected %d got %d", reply.C, args.A+args.B) } }
func InitRouterRpc(addrs []string, retry time.Duration) (err error) { var r *rpc.Client routerRing = ketama.NewRing(ketama.Base) for _, addr := range addrs { r, err = rpc.Dial("tcp", addr) if err != nil { log.Error("rpc.Dial(\"%s\") error(%s)", addr, err) return } go rpcPing(addr, r, retry) log.Debug("router rpc addr:%s connect", addr) routerServiceMap[addr] = r routerRing.AddNode(addr, 1) } routerRing.Bake() return }
func InitRouterRpc(addrs []string) (err error) { var r *protorpc.Client routerRing = ketama.NewRing(ketama.Base) for _, addr := range addrs { r, err = protorpc.Dial("tcp", addr) if err != nil { log.Error("protorpc.Dial(\"%s\") error(%s)", addr, err) return } var quit chan struct{} go protorpc.Reconnect(&r, quit, "tcp", addr) log.Debug("router protorpc addr:%s connect", addr) routerServiceMap[addr] = r routerRing.AddNode(addr, 1) } routerRing.Bake() return }
func rpcPing(addr string, c *protorpc.Client, retry time.Duration) { var err error for { if err = c.Call(logicServicePing, nil, nil); err != nil { log.Error("c.Call(\"%s\", nil, nil) error(%v), retry after:%ds", logicServicePing, err, retry/time.Second) rpcTmp, err := protorpc.Dial("tcp", addr) if err != nil { log.Error("protorpc.Dial(\"tcp\", %s) error(%v)", addr, err) time.Sleep(retry) continue } c = rpcTmp time.Sleep(retry) continue } log.Debug("rpc ping:%s ok", addr) time.Sleep(retry) continue } }
func InitRouter() (err error) { var ( r *rpc.Client i = 0 ) routerRing = ketama.NewRing(ketama.Base) for serverId, addr := range Conf.RouterRPCAddrs { r, err = rpc.Dial(Conf.RouterRPCNetworks[i], addr) if err != nil { log.Error("rpc.Dial(\"%s\", \"%s\") error(%s)", Conf.RouterRPCNetworks[i], addr, err) return } go rpc.Reconnect(&r, routerQuit, Conf.RouterRPCNetworks[i], addr) log.Debug("router rpc addr:%s connect", addr) routerServiceMap[serverId] = r routerRing.AddNode(serverId, 1) i++ } routerRing.Bake() return }
func TestProtoError(t *testing.T) { if runtime.GOOS == "plan9" { t.Skip("skipping test; see http://golang.org/issue/8908") } defer func() { err := recover() if err == nil { t.Fatal("no error") } if !strings.Contains("reading body proto: wrong wireType = 2 for field C", err.(error).Error()) { t.Fatal("expected proto', got", err) } }() protorpc.Register(new(S)) listen, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { panic(err) } go protorpc.Accept(listen) client, err := protorpc.Dial("tcp", listen.Addr().String()) if err != nil { panic(err) } go client.Ping(&client) var reply Reply err = client.Call("S.Recv", nil, &reply) if err != nil { panic(err) } fmt.Printf("%#v\n", reply) client.Close() listen.Close() }
func dialDirect() (*protorpc.Client, error) { return protorpc.Dial("tcp", serverAddr) }
func testRPC(t *testing.T, addr string) { client, err := protorpc.Dial("tcp", addr) if err != nil { t.Fatal("dialing", err) } defer client.Close() // Synchronous calls log.Println("Synchronous calls") args := &Args{7, 8} reply := new(Reply) err = client.Call("Arith.Add", args, reply) if err != nil { t.Errorf("Add: expected no error but got string %q", err.Error()) } if reply.C != args.A+args.B { t.Errorf("Add: expected %d got %d", reply.C, args.A+args.B) } // Nonexistent method log.Println("Nonexistent method") args = &Args{7, 0} reply = new(Reply) err = client.Call("Arith.BadOperation", args, reply) // expect an error if err == nil { t.Error("BadOperation: expected error") } else if !strings.HasPrefix(err.Error(), "rpc: can't find method ") { t.Errorf("BadOperation: expected can't find method error; got %q", err) } // Unknown service log.Println("Unknown service") args = &Args{7, 8} reply = new(Reply) err = client.Call("Arith.Unknown", args, reply) if err == nil { t.Error("expected error calling unknown service") } else if strings.Index(err.Error(), "method") < 0 { t.Error("expected error about method; got", err) } // Out of order. log.Println("Out of order") args = &Args{7, 8} mulReply := new(Reply) log.Println("Aritu.Mul") mulCall := client.Go("Arith.Mul", args, mulReply, nil) addReply := new(Reply) log.Println("Aritu.Add") addCall := client.Go("Arith.Add", args, addReply, nil) addCall = <-addCall.Done if addCall.Error != nil { t.Errorf("Add: expected no error but got string %q", addCall.Error.Error()) } if addReply.C != args.A+args.B { t.Errorf("Add: expected %d got %d", addReply.C, args.A+args.B) } mulCall = <-mulCall.Done if mulCall.Error != nil { t.Errorf("Mul: expected no error but got string %q", mulCall.Error.Error()) } if mulReply.C != args.A*args.B { t.Errorf("Mul: expected %d got %d", mulReply.C, args.A*args.B) } // // Error test // args = &Args{7, 0} // reply = new(Reply) // err = client.Call("Arith.Div", args, reply) // // expect an error: zero divide // if err == nil { // t.Error("Div: expected error") // } else if err.Error() != "divide by zero" { // t.Error("Div: expected divide by zero error; got", err) // } // // // Bad type. // reply = new(Reply) // err = client.Call("Arith.Add", reply, reply) // args, reply would be the correct thing to use // if err == nil { // t.Error("expected error calling Arith.Add with wrong arg type") // } else if strings.Index(err.Error(), "type") < 0 { // t.Error("expected error about type; got", err) // } // // // Non-struct argument // const Val = 12345 // str := new(Str) // str.Val = fmt.Sprint(Val) // reply = new(Reply) // err = client.Call("Arith.Scan", str, reply) // if err != nil { // t.Errorf("Scan: expected no error but got string %q", err.Error()) // } else if reply.C != Val { // t.Errorf("Scan: expected %d got %d", Val, reply.C) // } // // // Non-struct reply // args = &Args{27, 35} // str.Val = "" // err = client.Call("Arith.String", args, str) // if err != nil { // t.Errorf("String: expected no error but got string %q", err.Error()) // } // expect := fmt.Sprintf("%d+%d=%d", args.A, args.B, args.A+args.B) // if str.Val != expect { // t.Errorf("String: expected %s got %s", expect, str) // } // // args = &Args{7, 8} // reply = new(Reply) // err = client.Call("Arith.Mul", args, reply) // if err != nil { // t.Errorf("Mul: expected no error but got string %q", err.Error()) // } // if reply.C != args.A*args.B { // t.Errorf("Mul: expected %d got %d", reply.C, args.A*args.B) // } // // // ServiceName contain "." character // args = &Args{7, 8} // reply = new(Reply) // err = client.Call("net.rpc.Arith.Add", args, reply) // if err != nil { // t.Errorf("Add: expected no error but got string %q", err.Error()) // } // if reply.C != args.A+args.B { // t.Errorf("Add: expected %d got %d", reply.C, args.A+args.B) // } }