本文整理汇总了Golang中net/rpc.NewClient函数的典型用法代码示例。如果您正苦于以下问题:Golang NewClient函数的具体用法?Golang NewClient怎么用?Golang NewClient使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewClient函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: connectContext
func connectContext(
broker *muxBroker,
ctx *context.Shared, args *ContextSharedArgs) (io.Closer, error) {
closer := &multiCloser{}
// Setup Directory
conn, err := broker.Dial(args.DirectoryId)
if err != nil {
return closer, err
}
client := rpc.NewClient(conn)
closer.Closers = append(closer.Closers, client)
ctx.Directory = &Directory{
Broker: broker,
Client: client,
Name: "Directory",
}
// Setup Ui
conn, err = broker.Dial(args.UiId)
if err != nil {
return closer, err
}
client = rpc.NewClient(conn)
closer.Closers = append(closer.Closers, client)
ctx.Ui = &Ui{
Client: client,
Name: "Ui",
}
return closer, nil
}
示例2: newRPCClient
func newRPCClient(addr string) (client *rpcClient, err error) {
client = &rpcClient{
addr: addr,
}
cmdConn, err := net.DialTimeout("tcp", addr, maxWait)
if err != nil {
return nil, err
}
client.cmd = rpc.NewClient(cmdConn)
raftConn, err := net.DialTimeout("tcp", addr, maxWait)
if err != nil {
client.raft = client.cmd
} else {
client.raft = rpc.NewClient(raftConn)
}
prioConn, err := net.DialTimeout("tcp", addr, maxWait)
if err != nil {
client.prio = client.raft
} else {
client.prio = rpc.NewClient(prioConn)
}
msgConn, err := net.DialTimeout("tcp", addr, maxWait)
if err != nil {
client.msg = client.cmd
} else {
client.msg = rpc.NewClient(msgConn)
}
return client, nil
}
示例3: TestXRPC
func TestXRPC(t *testing.T) {
inconn, outconn := vtesting.SelfConnection()
ins, outs, err := MuxPairs(inconn, outconn, 2, false)
if err != nil {
t.Error("MuxPairs failed: ", err)
}
type pair struct {
In net.Conn
Out net.Conn
}
pairs := make([]pair, 2)
pairs[0].In = ins[0]
pairs[1].In = ins[1]
pairs[0].Out = outs[0]
pairs[1].Out = outs[1]
for _, p := range pairs {
if p.In.LocalAddr().String() != p.Out.RemoteAddr().String() &&
p.In.LocalAddr().String() != "" {
t.Error("Address mismatch: ", p.In.LocalAddr(), " != ", p.Out.RemoteAddr())
}
if p.In.RemoteAddr().String() != p.Out.LocalAddr().String() &&
p.In.RemoteAddr().String() != "" {
t.Error("Address mismatch: ", p.In.RemoteAddr(), " != ", p.Out.LocalAddr())
}
}
srv := rpc.NewServer()
srv.Register(new(vtesting.RPCRecv))
go srv.ServeConn(pairs[0].In)
go srv.ServeConn(pairs[1].Out)
client1 := rpc.NewClient(pairs[0].Out)
defer client1.Close()
client2 := rpc.NewClient(pairs[1].In)
defer client2.Close()
sdata1 := "abc"
sdata2 := "123"
rdata1 := ""
rdata2 := ""
call1 := client1.Go("RPCRecv.Echo", &sdata1, &rdata1, nil)
call2 := client2.Go("RPCRecv.Echo", &sdata2, &rdata2, nil)
<-call2.Done
<-call1.Done
if sdata1 != rdata1 || sdata2 != rdata2 {
t.Error("XRPC failed")
}
}
示例4: sendConfiguration
func sendConfiguration(nodesToConfigure []string, newConfig *server.ServerNodeConfiguration) (err error) {
for _, node := range nodesToConfigure {
netconn, localerr := net.Dial("tcp", node)
//rpcclient, localerr := rpc.Dial("tcp", node)
if localerr != nil {
fmt.Printf("Error connecting to node %v: %v\n", node, localerr)
err = localerr
} else {
rpcclient := rpc.NewClient(netconn)
reply := &server.ConfigChangeResults{}
localerr = rpcclient.Call("RPCHandler.ConfigurationChange", newConfig, reply)
if localerr != nil {
fmt.Printf("Error configuring node %v: %v\n", node, localerr)
err = localerr
return err
}
if reply.Result.Code != rapi.RI_SUCCESS {
fmt.Printf("Error applying configuration to node %v: %v\n", node, reply.Result.Description)
} else {
fmt.Printf("Configuration set successfully on node %v\n", node)
}
}
}
return err
}
示例5: BenchmarkNetrpcByteSlice
func BenchmarkNetrpcByteSlice(b *testing.B) {
connC, connS := getTcpPipe(b)
defer connC.Close()
defer connS.Close()
s := rpc.NewServer()
if err := s.Register(&NetrpcService{}); err != nil {
b.Fatalf("Error when registering rpc service: %s", err)
}
go s.ServeConn(connS)
c := rpc.NewClient(connC)
defer c.Close()
req := []byte("byte slice byte slice aaa bbb ccc foobar")
b.SetParallelism(250)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
var resp []byte
for i := 0; pb.Next(); i++ {
if err := c.Call("NetrpcService.ByteSlice", req, &resp); err != nil {
b.Fatalf("Unexpected error when calling NetrpcService.ByteSlice(%q): %s", req, err)
}
if !bytes.Equal(resp, req) {
b.Fatalf("Unexpected response: %q. Expected %q", resp, req)
}
}
})
}
示例6: logHandler
func (me *Coordinator) logHandler(w http.ResponseWriter, req *http.Request) {
_, conn, err := me.getHost(req)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "<html><head><title>Termite worker error</title></head>")
fmt.Fprintf(w, "<body>Error: %s</body></html>", err.Error())
return
}
sz := int64(500 * 1024)
sizeStr, ok := req.URL.Query()["size"]
if ok {
fmt.Scanf(sizeStr[0], "%d", &sz)
}
logReq := LogRequest{Whence: os.SEEK_END, Off: -sz, Size: sz}
logRep := LogResponse{}
client := rpc.NewClient(conn)
err = client.Call("Worker.Log", &logReq, &logRep)
client.Close()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "<html><head><title>Termite worker error</title></head>")
fmt.Fprintf(w, "<body>Error: %s</body></html>", err.Error())
return
}
w.Header().Set("Content-Type", "text/plain")
w.Write(logRep.Data)
return
}
示例7: Input
func (s *ResourceProviderServer) Input(
args *ResourceProviderInputArgs,
reply *ResourceProviderInputResponse) error {
conn, err := s.Broker.Dial(args.InputId)
if err != nil {
*reply = ResourceProviderInputResponse{
Error: NewBasicError(err),
}
return nil
}
client := rpc.NewClient(conn)
defer client.Close()
input := &UIInput{
Client: client,
Name: "UIInput",
}
config, err := s.Provider.Input(input, args.Config)
*reply = ResourceProviderInputResponse{
Config: config,
Error: NewBasicError(err),
}
return nil
}
示例8: Client
// Client returns an RPC client for the plugin.
//
// Subsequent calls to this will return the same RPC client.
func (c *Client) Client() (*rpc.Client, error) {
addr, err := c.Start()
if err != nil {
return nil, err
}
c.l.Lock()
defer c.l.Unlock()
if c.client != nil {
return c.client, nil
}
conn, err := net.Dial(addr.Network(), addr.String())
if err != nil {
return nil, err
}
if tcpConn, ok := conn.(*net.TCPConn); ok {
// Make sure to set keep alive so that the connection doesn't die
tcpConn.SetKeepAlive(true)
}
c.client = rpc.NewClient(conn)
return c.client, nil
}
示例9: NewClient
// NewClient returns a client connection
func NewClient(dsn string, timeout time.Duration) (*Client, error) {
connection, err := net.DialTimeout("tcp", dsn, timeout)
if err != nil {
return nil, err
}
return &Client{connection: rpc.NewClient(connection)}, nil
}
示例10: StartProvider
// StartProvider start a provider-style plugin application at the given path and
// args, and returns an RPC client that communicates with the plugin using gob
// encoding over the plugin's Stdin and Stdout. The writer passed to output
// will receive output from the plugin's stderr. Closing the RPC client
// returned from this function will shut down the plugin application.
func StartProvider(output io.Writer, path string, args ...string) (*rpc.Client, error) {
pipe, err := start(makeCommand(output, path, args))
if err != nil {
return nil, err
}
return rpc.NewClient(pipe), nil
}
示例11: Dial
func Dial(socketPath string) (*rpc.Client, error) {
conn, err := npipe.Dial(socketPath)
if err != nil {
return nil, err
}
return rpc.NewClient(conn), nil
}
示例12: NewClient
// NewClient returns a new rpc.Client that communicates with the given command over stdin/stdout.
func NewClient(cmd *exec.Cmd) (*rpc.Client, error) {
conn, err := newConn(cmd)
if err != nil {
return nil, err
}
return rpc.NewClient(conn), nil
}
示例13: Start
func (pc *PluginClient) Start() error {
if pc.proc != nil {
return common.NewPluginError("plugin is already started")
}
pc.proc = exec.Command(pc.File)
stderr, _ := pc.proc.StderrPipe()
stdin, _ := pc.proc.StdinPipe()
stdout, _ := pc.proc.StdoutPipe()
fmt.Printf("Starting plugin %s\n", pc.File)
err := pc.proc.Start()
if err != nil {
return err
}
go common.StdErrForward(stderr)
pio := common.NewPluginIO(stdout, stdin)
pc.mpx = ipc.NewMultiplex(pio)
rpcwriter := pc.mpx.RawWriterChannel("rpcw")
rpcreader := pc.mpx.RawReaderChannel("rpcr")
pc.RPC = rpc.NewClient(common.NewPluginIO(rpcreader, rpcwriter))
if !pc.VerifyRPC() {
return common.NewPluginError("plugin is not a valid plugin or is not responding")
}
return nil
}
示例14: DialRPC
// connects to the RPC service on remote server
func DialRPC(addr *net.TCPAddr) (*rpc.Client, error) {
conn, err := DialHandler(addr, rpcHandlerId)
if err != nil {
return nil, err
}
return rpc.NewClient(conn), nil
}
示例15: Shuffle
// Shuffle initiates a random exchange of peer profiles with a neighbour of
// greatest age.
func (c *Cyclon) Shuffle() {
c.neighbsmu.Lock()
if len(c.neighbs) == 0 {
c.neighbsmu.Unlock()
return
}
// Increase the age of all neighbours and our service
for _, p := range c.neighbs {
p.Put(age, p.Get(age).(int)+1)
}
c.serviceAge++
// Pop the neighbour that we're going to shuffle with
q := c.neighbs.PopOldest()
// Construct the offer. This doesn't remove entries from c.neighbs
offer := c.neighbs.Sample(c.shuflen - 1)
c.neighbsmu.Unlock()
// Calling another cyclon over the network can take a while
// so we keep our cache unlocked while doing this.
var answer []pnet.Peer
conn, err := c.protonet.Dial(q)
if err == nil {
cl := rpc.NewClient(conn)
cl.Call("CyclonRPC.HandleShuffle", append(offer, c.me), &answer)
conn.Close()
}
c.neighbsmu.Lock()
c.updateCache(answer, offer)
c.neighbsmu.Unlock()
}