本文整理汇总了Golang中net/rpc.Client.Go方法的典型用法代码示例。如果您正苦于以下问题:Golang Client.Go方法的具体用法?Golang Client.Go怎么用?Golang Client.Go使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/rpc.Client
的用法示例。
在下文中一共展示了Client.Go方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Client
func Client() {
var err error
var c net.Conn
c, err = net.DialTimeout("tcp", "127.0.0.1:1234", 1000*1000*1000*30)
if err != nil {
log.Fatal("dialing:", err)
}
var client *rpc.Client
client = jsonrpc.NewClient(c)
// 同步
var args *Args = &Args{7, 8}
var reply *Data = new(Data)
client.Call("Arith.Plus", args, reply)
fmt.Println(reply)
// 异步
args.A = 1
args.B = 2
var call *rpc.Call
call = client.Go("Arith.Plus", args, reply, nil)
var doneCall *rpc.Call
doneCall = <-call.Done
fmt.Println(doneCall.Args, doneCall.Reply)
fmt.Println(args, reply)
client.Close()
c.Close()
}
示例2: accept
func (pn *paxosNode) accept(client *rpc.Client, args *paxosrpc.AcceptArgs, reply *paxosrpc.AcceptReply, reqChan chan voteRequest) {
finChan := make(chan *rpc.Call, pn.numNodes)
client.Go("PaxosNode.RecvAccept", args, reply, finChan)
call := <-finChan
if call.Reply.(*paxosrpc.AcceptReply).Status == paxosrpc.OK {
reqChan <- voteRequest{vote: true}
} else {
reqChan <- voteRequest{vote: false}
}
}
示例3: ApiQuery
func ApiQuery(w http.ResponseWriter, r *http.Request) {
defer func() {
//r.Body.Close()
}()
//_, err := ioutil.ReadAll(r.Body)
//if err != nil {
// return
//}
var err error
status := make(chan int, 2)
go func() {
var sock *rpc.Client
if sock, err = rpc.DialHTTP("tcp", "localhost:9601"); err != nil {
return
}
defer sock.Close()
//r := new(CommandReply)
var rsp CommandReply
rs := sock.Go("Command.Query", "nil", &rsp, nil)
select {
case <-rs.Done:
status <- 1
case <-time.After(3e9):
status <- 9
}
//runtime.Goexit()
return
}()
for {
select {
case <-status:
goto L
case <-time.After(3e9):
goto L
}
}
L:
//io.WriteString(w, "{\"status\": \"OK\"}")
close(status)
return
}
示例4: prepare
func (pn *paxosNode) prepare(client *rpc.Client, args *paxosrpc.PrepareArgs, reply *paxosrpc.PrepareReply, reqChan chan voteRequest) {
finChan := make(chan *rpc.Call, pn.numNodes)
client.Go("PaxosNode.RecvPrepare", args, reply, finChan)
call := <-finChan
pReply := call.Reply.(*paxosrpc.PrepareReply)
if pReply.Status == paxosrpc.OK {
reqChan <- voteRequest{true, pReply.N_a, pReply.V_a}
} else {
reqChan <- voteRequest{false, pReply.N_a, pReply.V_a}
}
}
示例5: GetWin32Service
func GetWin32Service(client *rpc.Client) {
var reply []Win32_Service
args := "test"
call := client.Go("Win32_Service.GetWin32Service", args, &reply, nil)
replyCall := <-call.Done
if replyCall.Error != nil {
log.Fatal("GetWin32Service.replyCall.Error:", replyCall.Error)
}
for i, v := range reply {
fmt.Println(i, v.Name, v.State)
}
}
示例6: GetWin32Process
func GetWin32Process(client *rpc.Client) {
var reply []Win32_Process
args := "test"
call := client.Go("Win32_Process.GetWin32Process", args, &reply, nil)
replyCall := <-call.Done
if replyCall.Error != nil {
log.Fatal("GetWin32Process.replyCall.Error:", replyCall.Error)
}
for i, v := range reply {
fmt.Println(i, v.Name)
}
}
示例7: rpc_call
func rpc_call(client *rpc.Client, method string, args interface{},
reply interface{}, timeout time.Duration) error {
done := make(chan *rpc.Call, 1)
client.Go(method, args, reply, done)
select {
case <-time.After(timeout):
return errors.New("i/o timeout[rpc]")
case call := <-done:
if call.Error == nil {
return nil
} else {
return call.Error
}
}
}
示例8: testEchoClientAsync
func testEchoClientAsync(t *testing.T, client *rpc.Client) {
// EchoService.Echo
args := &message.EchoRequest{Msg: "Hello, Protobuf-RPC"}
reply := &message.EchoResponse{}
echoCall := client.Go("EchoService.Echo", args, reply, nil)
// EchoService.Echo reply
echoCall = <-echoCall.Done
if echoCall.Error != nil {
t.Fatalf(`EchoService.Echo: %v`, echoCall.Error)
}
if echoCall.Reply.(*message.EchoResponse).GetMsg() != args.GetMsg() {
t.Fatalf(`EchoService.Echo: expected = "%s", got = "%s"`,
args.GetMsg(),
echoCall.Reply.(*message.EchoResponse).GetMsg(),
)
}
}
示例9: MakeAsyncCall
// MakeAsyncCall calls a function at a remote peer 'callee' asynchronously. The three last arguments
// are identical to that of net/rpc's '(client *Client) Go' function.
func (rpcServ *RPCService) MakeAsyncCall(callee *Peer, call string, args interface{}, result interface{}) *rpc.Call {
if callee == nil {
return nil
}
// Check if there is already a connection
var client *rpc.Client
rpcServ.RLock()
client = rpcServ.clientMap[callee.Address]
rpcServ.RUnlock()
// Open if not
var err error
if client == nil {
client, err = rpcServ.rpcConnect(callee)
if err != nil {
fmt.Println("RPC Connect failed!")
return nil
}
}
asyncCall := client.Go("Node."+call, args, result, nil)
return asyncCall
}
示例10: testArithClientAsync
func testArithClientAsync(t *testing.T, client *rpc.Client) {
done := make(chan *rpc.Call, 16)
callInfoList := []struct {
method string
args *msg.ArithRequest
reply *msg.ArithResponse
err error
}{
{
"ArithService.Add",
&msg.ArithRequest{A: proto.Int32(1), B: proto.Int32(2)},
&msg.ArithResponse{C: proto.Int32(3)},
nil,
},
{
"ArithService.Mul",
&msg.ArithRequest{A: proto.Int32(2), B: proto.Int32(3)},
&msg.ArithResponse{C: proto.Int32(6)},
nil,
},
{
"ArithService.Div",
&msg.ArithRequest{A: proto.Int32(13), B: proto.Int32(5)},
&msg.ArithResponse{C: proto.Int32(2)},
nil,
},
{
"ArithService.Div",
&msg.ArithRequest{A: proto.Int32(1), B: proto.Int32(0)},
&msg.ArithResponse{},
errors.New("divide by zero"),
},
{
"ArithService.Error",
&msg.ArithRequest{A: proto.Int32(1), B: proto.Int32(2)},
&msg.ArithResponse{},
errors.New("ArithError"),
},
}
// GoCall list
calls := make([]*rpc.Call, len(callInfoList))
for i := 0; i < len(calls); i++ {
calls[i] = client.Go(callInfoList[i].method,
callInfoList[i].args, callInfoList[i].reply,
done,
)
}
for i := 0; i < len(calls); i++ {
<-calls[i].Done
}
// check result
for i := 0; i < len(calls); i++ {
if callInfoList[i].err != nil {
if calls[i].Error.Error() != callInfoList[i].err.Error() {
t.Fatalf(`%s: expected %v, Got = %v`,
callInfoList[i].method,
callInfoList[i].err,
calls[i].Error,
)
}
continue
}
got := calls[i].Reply.(*msg.ArithResponse).GetC()
expected := callInfoList[i].reply.GetC()
if got != expected {
t.Fatalf(`%v: expected %v, Got = %v`,
callInfoList[i].method, got, expected,
)
}
}
}
示例11: commit
func (pn *paxosNode) commit(client *rpc.Client, args *paxosrpc.CommitArgs, reply *paxosrpc.CommitReply, reqChan chan voteRequest) {
finChan := make(chan *rpc.Call, pn.numNodes)
go client.Go("PaxosNode.RecvCommit", args, reply, finChan)
<-finChan
reqChan <- voteRequest{vote: true}
}