本文整理汇总了Golang中github.com/pingcap/kvproto/pkg/util.ReadMessage函数的典型用法代码示例。如果您正苦于以下问题:Golang ReadMessage函数的具体用法?Golang ReadMessage怎么用?Golang ReadMessage使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ReadMessage函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: callRPC
func (w *rpcWorker) callRPC(conn *bufio.ReadWriter, req *pdpb.Request) (resp *pdpb.Response, err error) {
// Record some metrics.
start := time.Now()
label := metrics.GetCmdLabel(req)
defer func() {
if err == nil {
cmdCounter.WithLabelValues(label).Inc()
cmdDuration.WithLabelValues(label).Observe(time.Since(start).Seconds())
} else {
cmdFailedCounter.WithLabelValues(label).Inc()
cmdFailedDuration.WithLabelValues(label).Observe(time.Since(start).Seconds())
}
}()
msg := &msgpb.Message{
MsgType: msgpb.MessageType_PdReq,
PdReq: req,
}
if err = util.WriteMessage(conn, newMsgID(), msg); err != nil {
return nil, errors.Errorf("[pd] rpc failed: %v", err)
}
conn.Flush()
if _, err = util.ReadMessage(conn, msg); err != nil {
return nil, errors.Errorf("[pd] rpc failed: %v", err)
}
if msg.GetMsgType() != msgpb.MessageType_PdResp {
return nil, errors.Trace(errInvalidResponse)
}
resp = msg.GetPdResp()
if err = w.checkResponse(resp); err != nil {
return nil, errors.Trace(err)
}
return resp, nil
}
开发者ID:jmptrader,项目名称:tidb,代码行数:35,代码来源:rpc_worker.go
示例2: TestWrongMessageID
func (s *testClientSuite) TestWrongMessageID(c *C) {
l := startServer(":61237", c, func(conn net.Conn, c *C) {
var msg msgpb.Message
msgID, err := util.ReadMessage(conn, &msg)
c.Assert(err, IsNil)
resp := msgpb.Message{
MsgType: msgpb.MessageType_KvResp,
KvResp: &pb.Response{
Type: msg.GetKvReq().GetType(),
},
}
// Send the request back to client, set wrong msgID for the 1st
// request.
if msgID == 1 {
err = util.WriteMessage(conn, msgID+100, &resp)
} else {
err = util.WriteMessage(conn, msgID, &resp)
}
c.Assert(err, IsNil)
})
defer l.Close()
cli := newRPCClient()
req := &pb.Request{
Type: pb.MessageType_CmdGet,
}
// Wrong ID for the first request, correct for the rests.
_, err := cli.SendKVReq(":61237", req, readTimeoutShort)
c.Assert(err, NotNil)
resp, err := cli.SendKVReq(":61237", req, readTimeoutShort)
c.Assert(err, IsNil)
c.Assert(resp.GetType(), Equals, req.GetType())
}
开发者ID:pingcap,项目名称:tidb,代码行数:32,代码来源:client_test.go
示例3: readThenCloseRequest
func readThenCloseRequest(conn net.Conn, c *C) {
c.Assert(conn, NotNil)
defer conn.Close()
var msg msgpb.Message
msgID, err := util.ReadMessage(conn, &msg)
c.Assert(err, IsNil)
c.Assert(msg.GetKvReq(), NotNil)
c.Assert(msgID, Greater, uint64(0))
}
开发者ID:pingcap,项目名称:tidb,代码行数:9,代码来源:client_test.go
示例4: Call
// Call sends the request to conn and wait for the response.
func Call(conn net.Conn, reqID uint64, request *pdpb.Request) (*pdpb.Response, error) {
req := &msgpb.Message{
MsgType: msgpb.MessageType_PdReq,
PdReq: request,
}
if err := util.WriteMessage(conn, reqID, req); err != nil {
return nil, errors.Trace(err)
}
resp := &msgpb.Message{}
respID, err := util.ReadMessage(conn, resp)
if err != nil {
return nil, errors.Trace(err)
}
if respID != reqID {
return nil, errors.Errorf("message id mismatch: reqID %d respID %d", reqID, respID)
}
return resp.GetPdResp(), nil
}
开发者ID:jmptrader,项目名称:tidb,代码行数:19,代码来源:rpcutil.go
示例5: doSend
func (c *rpcClient) doSend(conn *Conn, msg *msgpb.Message) error {
curMsgID := atomic.AddUint64(&c.msgID, 1)
log.Debugf("Send request msgID[%d] type[%v]", curMsgID, msg.GetMsgType())
if err := util.WriteMessage(conn, curMsgID, msg); err != nil {
return errors.Trace(err)
}
if err := conn.Flush(); err != nil {
return errors.Trace(err)
}
msgID, err := util.ReadMessage(conn.BufioReader(), msg)
if err != nil {
return errors.Trace(err)
}
if curMsgID != msgID {
log.Errorf("Sent msgID[%d] mismatches recv msgID[%d]", curMsgID, msgID)
}
log.Debugf("Receive response msgID[%d] type[%v]", msgID, msg.GetMsgType())
return nil
}
开发者ID:tangfeixiong,项目名称:tidb,代码行数:19,代码来源:client.go
示例6: handleRequest
// handleRequest receive Request then send empty Response back fill with same Type.
func handleRequest(conn net.Conn, c *C) {
c.Assert(conn, NotNil)
defer conn.Close()
var msg msgpb.Message
msgID, err := util.ReadMessage(conn, &msg)
c.Assert(err, IsNil)
c.Assert(msgID, Greater, uint64(0))
c.Assert(msg.GetMsgType(), Equals, msgpb.MessageType_KvReq)
req := msg.GetKvReq()
c.Assert(req, NotNil)
var resp pb.Response
resp.Type = req.Type
msg = msgpb.Message{
MsgType: msgpb.MessageType_KvResp,
KvResp: &resp,
}
err = util.WriteMessage(conn, msgID, &msg)
c.Assert(err, IsNil)
}
开发者ID:pingcap,项目名称:tidb,代码行数:21,代码来源:client_test.go
示例7: doSend
func (c *rpcClient) doSend(conn *Conn, msg *msgpb.Message, writeTimeout time.Duration, readTimeout time.Duration) error {
curMsgID := atomic.AddUint64(&c.msgID, 1)
conn.SetWriteDeadline(time.Now().Add(writeTimeout))
if err := util.WriteMessage(conn, curMsgID, msg); err != nil {
return errors.Trace(err)
}
if err := conn.Flush(); err != nil {
return errors.Trace(err)
}
conn.SetReadDeadline(time.Now().Add(readTimeout))
msgID, err := util.ReadMessage(conn.BufioReader(), msg)
if err != nil {
return errors.Trace(err)
}
if curMsgID != msgID {
log.Errorf("Sent msgID[%d] mismatches recv msgID[%d]", curMsgID, msgID)
return errors.Trace(errInvalidResponse)
}
return nil
}
开发者ID:pingcap,项目名称:tidb,代码行数:20,代码来源:client.go
示例8: callRPC
func (w *rpcWorker) callRPC(conn *bufio.ReadWriter, req *pdpb.Request) (*pdpb.Response, error) {
msg := &msgpb.Message{
MsgType: msgpb.MessageType_PdReq.Enum(),
PdReq: req,
}
if err := util.WriteMessage(conn, newMsgID(), msg); err != nil {
return nil, errors.Errorf("[pd] rpc failed: %v", err)
}
conn.Flush()
if _, err := util.ReadMessage(conn, msg); err != nil {
return nil, errors.Errorf("[pd] rpc failed: %v", err)
}
if msg.GetMsgType() != msgpb.MessageType_PdResp {
return nil, errors.Trace(errInvalidResponse)
}
resp := msg.GetPdResp()
if err := w.checkResponse(resp); err != nil {
return nil, errors.Trace(err)
}
return resp, nil
}
开发者ID:XuHuaiyu,项目名称:tidb,代码行数:22,代码来源:rpc_worker.go
示例9: doSend
func (c *rpcClient) doSend(conn net.Conn, msg *msgpb.Message) error {
curMsgID := atomic.AddUint64(&c.msgID, 1)
log.Debugf("Send request msgID[%d] type[%v]", curMsgID, msg.GetMsgType())
if err := conn.SetWriteDeadline(time.Now().Add(writeTimeout)); err != nil {
log.Warn("Set write deadline failed, it may be blocked.")
}
if err := util.WriteMessage(conn, curMsgID, msg); err != nil {
return errors.Trace(err)
}
if err := conn.SetReadDeadline(time.Now().Add(readTimeout)); err != nil {
log.Warn("Set read deadline failed, it may be blocked.")
}
msgID, err := util.ReadMessage(conn, msg)
if err != nil {
return errors.Trace(err)
}
if curMsgID != msgID {
log.Errorf("Sent msgID[%d] mismatches recv msgID[%d]", curMsgID, msgID)
}
log.Debugf("Receive response msgID[%d] type[%v]", msgID, msg.GetMsgType())
return nil
}
开发者ID:anywhy,项目名称:tidb,代码行数:22,代码来源:client.go
注:本文中的github.com/pingcap/kvproto/pkg/util.ReadMessage函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论