示例#1
0
文件: rpc.go 项目: tartavull/dvid
// SendRPC sends a request to a remote DVID.
func SendRPC(addr string, req datastore.Request) error {
	c := gorpc.NewTCPClient(addr)
	c.Start()
	defer c.Stop()

	dc := rpc.Dispatcher().NewFuncClient(c)
	resp, err := dc.Call(commandMsg, req)
	if err != nil {
		return fmt.Errorf("RPC error for %q: %v", req.Command, err)
	}

	reply, ok := resp.(*datastore.Response)
	if !ok {
		return fmt.Errorf("bad response to request %s: %v", req, resp)
	}
	return reply.Write(os.Stdout)
}
示例#2
0
文件: client.go 项目: tartavull/dvid
// NewSession returns a new session to the remote address where the
// type of session is reflected by the MessageID.
func NewSession(addr string, mid MessageID) (Session, error) {
	c := gorpc.NewTCPClient(addr)
	c.Start()
	dc := dispatcher.NewFuncClient(c)
	if dc == nil {
		return Session{}, fmt.Errorf("can't create dispatcher client")
	}

	// Start session create RPC using the given message, which
	// should remotely use the registered session maker and return
	// a session ID.
	resp, err := dc.Call(sendNewSession, mid)
	if err != nil {
		return Session{}, err
	}
	sid, ok := resp.(SessionID)
	if !ok {
		return Session{}, fmt.Errorf("remote server returned %v instead of session id", resp)
	}

	// Create the session.
	return Session{c: c, dc: dc, id: sid}, nil
}
示例#3
0
// Benchmark valyala/gorpc TCP connection for 2MB payload.
func BenchmarkPingPong_2MB_valyala(b *testing.B) {
	gorpc.RegisterType(&PostingList{})

	s := gorpc.NewTCPServer(":12345", PingPong)
	if err := s.Start(); err != nil {
		b.Fatal("While starting server on port 12345")
		return
	}
	defer s.Stop()

	req := NewRequest(mb2)
	c := gorpc.NewTCPClient(":12345")
	c.Start()
	defer c.Stop()

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_, err := c.Call(req)
		if err != nil {
			b.Fatalf("While running request: %v", err)
			return
		}
	}
}