本文整理汇总了Golang中net/rpc.Client.Close方法的典型用法代码示例。如果您正苦于以下问题:Golang Client.Close方法的具体用法?Golang Client.Close怎么用?Golang Client.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/rpc.Client
的用法示例。
在下文中一共展示了Client.Close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: benchmarkEchoProtoRPC
func benchmarkEchoProtoRPC(b *testing.B, size int) {
var client *rpc.Client
benchmarkEcho(b, size, listenAndServeProtoRPC,
func(addr net.Addr) {
conn, err := tls.Dial(addr.Network(), addr.String(), clientTLSConfig)
if err != nil {
b.Fatal(err)
}
client = rpc.NewClientWithCodec(NewClientCodec(conn))
},
func() {
if err := client.Close(); err != nil {
b.Fatal(err)
}
},
func(echoMsg string) string {
args := EchoRequest{Msg: echoMsg}
reply := EchoResponse{}
if err := client.Call("Echo.Echo", &args, &reply); err != nil {
b.Fatal(err)
}
return reply.Msg
},
)
}
示例2: iNewLibstore
/**@brief helper function for sorting
* @param server master storage server addr
* @param myhostport trib server's port
* @param flags
* @return *Libstore
* @return error
*/
func iNewLibstore(server, myhostport string, flags int) (*Libstore, error) {
var store Libstore
var master *rpc.Client
var args storageproto.GetServersArgs
var reply storageproto.RegisterReply
var err error
store.Addr = myhostport
store.Flags = flags
if store.Addr != "" {
rpc.Register(cacherpc.NewCacheRPC(&store))
}
lsplog.Vlogf(3, "libstore try to connect to master storage %s", server)
master, err = rpc.DialHTTP("tcp", server)
if lsplog.CheckReport(1, err) {
return nil, err
}
lsplog.Vlogf(3, "try to call GetServers")
master.Call("StorageRPC.GetServers", &args, &reply)
if !reply.Ready {
for i := 0; i < 5; i++ {
time.Sleep(1000 * time.Millisecond)
master.Call("StorageRPC.GetServers", &args, &reply)
}
}
err = master.Close()
if lsplog.CheckReport(1, err) {
lsplog.Vlogf(3, "WARNING close master failed")
}
// couldn't get list of servers from master
if (reply.Ready == false) || (reply.Servers == nil) {
return nil, lsplog.MakeErr("Storage system not ready.")
}
store.Nodes = reply.Servers
store.RPCConn = make([]*rpc.Client, len(store.Nodes))
sort.Sort(store.Nodes)
/*
for i := 0; i < len(store.Nodes); i++ {
fmt.Printf("%v\n", store.Nodes[i])
}*/
store.Leases = cache.NewCache()
if lsplog.CheckReport(1, err) {
return nil, err
}
lsplog.Vlogf(3, "libstore create complete")
return &store, nil
}
示例3: Client
func Client() {
var err error
var c net.Conn
c, err = net.DialTimeout("tcp", "127.0.0.1:9999", 1000*1000*1000*30)
if err != nil {
log.Fatal("dialing:", err)
}
var client *rpc.Client
client = jsonrpc.NewClient(c)
// 同步
var a *PlusA = &PlusA{7, 8}
var r *PlusR = new(PlusR)
ch := make(chan int)
for i := 0; i < 10000; i++ {
go func() {
client.Call("Test.Plus", a, r)
<-ch
}()
}
for j := 0; j < 10000; j++ {
ch <- 1
fmt.Println(r)
}
client.Close()
c.Close()
}
示例4: SendPeerMessage
func (srv *ServerNode) SendPeerMessage(peerName string, api string, args interface{}, reply interface{}) error {
srv.lock.RLock()
// Find the peer
var peer *ServerPeer
peer = srv.peers[peerName]
srv.lock.RUnlock()
if peer == nil {
return ERR_PEER_NOT_FOUND
}
// Attempt to get a connection from the pool
//srv_log("Looking for connection in pool for peer %v\n", peerName)
var client *rpc.Client
var ok bool
client, ok = <-peer.connections
if !ok {
srv_log("Peer %v connection in shutdown - unable to send to peer\n", peerName)
return ERR_PEER_NOT_FOUND
}
//srv_log("Found connection - sending api call %v\n", api)
err := client.Call(api, args, reply)
if err != nil {
srv_log("Error in outbound call to %v - closing client and asking for a new connection: %v\n", peerName, err)
client.Close()
peer.broken_connections <- struct{}{}
} else {
//srv_log("Call worked - returning connection to the pool\n")
// It worked - restore the connection to the pool
peer.connections <- client
}
return err
}
示例5: 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()
}
示例6: putConn
// putConn adds a connection to the free pool
func (r *RPCExt) putConn(c *rpc.Client) {
r.mu.Lock()
if n := len(r.clients); !r.closed && n < r.maxIdleConnections {
r.clients = append(r.clients, c)
r.mu.Unlock()
return
}
r.mu.Unlock()
c.Close()
}
示例7: main
func main() {
flag.Parse()
args := flag.Args()
clientName := args[0]
if !strings.Contains(clientName, ":") {
clientName = clientName + ":6969"
}
var client *rpc.Client
var err error
sleepDuration, _ := time.ParseDuration(fmt.Sprintf("%ds", *interval))
for iter := 0; *numPolls < 0 || iter < *numPolls; iter++ {
if iter > 0 {
time.Sleep(sleepDuration)
}
if client == nil {
client, err = rpc.DialHTTP("tcp", clientName)
if err != nil {
fmt.Printf("Error dialing\t%s\n", err)
os.Exit(1)
}
}
arg := new(uint64)
var reply *httpd.FileSystemHistory
err = client.Call("Subd.Poll", arg, &reply)
if err != nil {
fmt.Printf("Error calling\t%s\n", err)
os.Exit(1)
}
if *newConnection {
client.Close()
client = nil
}
fs := reply.FileSystem
if fs == nil {
fmt.Println("No FileSystem pointer")
} else {
fs.RebuildPointers()
if *debug {
fs.DebugWrite(os.Stdout, "")
} else {
fmt.Print(fs)
}
if *file != "" {
f, err := os.Create(*file)
if err != nil {
fmt.Printf("Error creating: %s\t%s\n", *file, err)
os.Exit(1)
}
encoder := gob.NewEncoder(f)
encoder.Encode(fs)
f.Close()
}
}
}
}
示例8: closeBrokenConnection
/*
closeBrokenConnection is an internal method to remove broken connections from our cache.
*/
func (client *Client) closeBrokenConnection(rpcclient *rpc.Client) {
client.nodeClientLock.Lock()
defer client.nodeClientLock.Unlock()
rpcclient.Close()
for key, value := range client.nodeClientMap {
if value == rpcclient {
delete(client.nodeClientMap, key)
return
}
}
}
示例9: 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
}
示例10: pollSubcommand
func pollSubcommand(client *rpc.Client, args []string) {
var err error
clientName := fmt.Sprintf("%s:%d", *subHostname, *subPortNum)
for iter := 0; *numPolls < 0 || iter < *numPolls; iter++ {
if iter > 0 {
time.Sleep(time.Duration(*interval) * time.Second)
}
if client == nil {
client, err = rpc.DialHTTP("tcp", clientName)
if err != nil {
fmt.Printf("Error dialing\t%s\n", err)
os.Exit(1)
}
}
var request sub.PollRequest
var reply sub.PollResponse
pollStartTime := time.Now()
err = client.Call("Subd.Poll", request, &reply)
fmt.Printf("Poll duration: %s\n", time.Since(pollStartTime))
if err != nil {
fmt.Printf("Error calling\t%s\n", err)
os.Exit(1)
}
if *newConnection {
client.Close()
client = nil
}
fs := reply.FileSystem
if fs == nil {
fmt.Println("No FileSystem pointer")
} else {
fs.RebuildInodePointers()
if *debug {
fs.DebugWrite(os.Stdout, "")
} else {
fmt.Print(fs)
}
if *file != "" {
f, err := os.Create(*file)
if err != nil {
fmt.Printf("Error creating: %s\t%s\n", *file, err)
os.Exit(1)
}
encoder := gob.NewEncoder(f)
encoder.Encode(fs)
f.Close()
}
}
}
time.Sleep(time.Duration(*wait) * time.Second)
}
示例11: SetDayCounter
func SetDayCounter(uid uint32, chip int32) (ret int32, err error) {
var cli *rpc.Client
if cli, err = jsonrpc.Dial("tcp", PY_RPC_ADDR); err != nil {
return
}
defer cli.Close()
args := &argSetDayCounter{uid, chip}
reply := new(setDayCounterRep)
if cli.Call("setDayCounter", args, reply); err != nil {
return
}
ret = reply.Chip
return
}
示例12: GetBillboard
func GetBillboard(uid uint32) (ret [][]string, err error) {
var cli *rpc.Client
if cli, err = jsonrpc.Dial("tcp", PY_RPC_ADDR); err != nil {
return
}
defer cli.Close()
args := &argGetBillboard{uid}
reply := new(getBillboardRep)
if cli.Call("getBillboard", args, reply); err != nil {
return
}
ret = reply.Billboard
return
}
示例13: GetBalance
func GetBalance(uids []uint32) (ubl map[uint32]uint32, err error) {
var cli *rpc.Client
if cli, err = jsonrpc.Dial("tcp", PY_RPC_ADDR); err != nil {
return
}
defer cli.Close()
args := &argGetBalance{uids}
reply := new(getBalanceRep)
if err = cli.Call("getBalance", args, reply); err != nil {
return
}
ubl = reply.keyToUint32()
return
}
示例14: SetName
func SetName(uid uint32, name string) (Rname string, err error) {
var cli *rpc.Client
if cli, err = jsonrpc.Dial("tcp", PY_RPC_ADDR); err != nil {
return
}
defer cli.Close()
args := &argSetName{uid, name}
reply := new(setNameRep)
if cli.Call("setName", args, reply); err != nil {
return
}
Rname = reply.Name
return
}
示例15: GetLogTime
func GetLogTime(uid uint32) (intime, outtime string, err error) {
var cli *rpc.Client
if cli, err = jsonrpc.Dial("tcp", PY_RPC_ADDR); err != nil {
return
}
defer cli.Close()
args := &argUid{uid}
reply := new(getLogTimeRep)
if err = cli.Call("getLogTime", args, reply); err != nil {
return
}
intime, outtime = reply.Logintime, reply.Logouttime
return
}