本文整理匯總了Golang中github.com/pingcap/kvproto/pkg/msgpb.Message類的典型用法代碼示例。如果您正苦於以下問題:Golang Message類的具體用法?Golang Message怎麽用?Golang Message使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Message類的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: 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())
}
示例2: 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))
}
示例3: SendCopReq
// SendCopReq sends a Request to co-processor and receives Response.
func (c *rpcClient) SendCopReq(req *coprocessor.Request) (*coprocessor.Response, error) {
conn, err := c.pool.Get()
if err != nil {
return nil, errors.Trace(err)
}
defer conn.Close()
msg := msgpb.Message{
MsgType: msgpb.MessageType_CopReq.Enum(),
CopReq: req,
}
err = c.doSend(conn, &msg)
if err == nil {
if msg.GetMsgType() != msgpb.MessageType_CopResp || msg.GetCopResp() == nil {
err = errors.Trace(errInvalidResponse)
}
}
if err != nil {
// This connection is not valid any more, so close its underlying conn.
if poolConn, ok := conn.(*pool.PoolConn); ok {
poolConn.MarkUnusable()
}
return nil, errors.Trace(err)
}
return msg.GetCopResp(), nil
}
示例4: SendKVReq
// SendKVReq sends a Request to kv server and receives Response.
func (c *rpcClient) SendKVReq(addr string, req *kvrpcpb.Request, timeout time.Duration) (*kvrpcpb.Response, error) {
sendReqCounter.WithLabelValues("kv").Inc()
start := time.Now()
defer func() { sendReqHistogram.WithLabelValues("kv").Observe(time.Since(start).Seconds()) }()
conn, err := c.p.GetConn(addr)
if err != nil {
return nil, errors.Trace(err)
}
defer c.p.PutConn(conn)
msg := msgpb.Message{
MsgType: msgpb.MessageType_KvReq,
KvReq: req,
}
err = c.doSend(conn, &msg, writeTimeout, timeout)
if err != nil {
conn.Close()
return nil, errors.Trace(err)
}
if msg.GetMsgType() != msgpb.MessageType_KvResp || msg.GetKvResp() == nil {
conn.Close()
return nil, errors.Trace(errInvalidResponse)
}
return msg.GetKvResp(), nil
}
示例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
}
示例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)
}
示例7: 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
}
示例8: doSend
func (c *rpcClient) doSend(conn *Conn, msg *msgpb.Message, writeTimeout time.Duration, readTimeout time.Duration) error {
curMsgID := atomic.AddUint64(&c.msgID, 1)
log.Debugf("Send request msgID[%d] type[%v]", curMsgID, msg.GetMsgType())
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)
}
log.Debugf("Receive response msgID[%d] type[%v]", msgID, msg.GetMsgType())
return nil
}
示例9: SendKVReq
// SendKVReq sends a Request to kv server and receives Response.
func (c *rpcClient) SendKVReq(addr string, req *kvrpcpb.Request) (*kvrpcpb.Response, error) {
conn, err := c.p.GetConn(addr)
if err != nil {
return nil, errors.Trace(err)
}
defer c.p.PutConn(conn)
msg := msgpb.Message{
MsgType: msgpb.MessageType_KvReq.Enum(),
KvReq: req,
}
err = c.doSend(conn, &msg)
if err != nil {
return nil, errors.Trace(err)
}
if msg.GetMsgType() != msgpb.MessageType_KvResp || msg.GetKvResp() == nil {
return nil, errors.Trace(errInvalidResponse)
}
return msg.GetKvResp(), nil
}
示例10: SendCopReq
// SendCopReq sends a Request to co-processor and receives Response.
func (c *rpcClient) SendCopReq(addr string, req *coprocessor.Request) (*coprocessor.Response, error) {
conn, err := c.p.GetConn(addr)
if err != nil {
return nil, errors.Trace(err)
}
defer c.p.PutConn(conn)
msg := msgpb.Message{
MsgType: msgpb.MessageType_CopReq,
CopReq: req,
}
err = c.doSend(conn, &msg)
if err != nil {
conn.Close()
return nil, errors.Trace(err)
}
if msg.GetMsgType() != msgpb.MessageType_CopResp || msg.GetCopResp() == nil {
conn.Close()
return nil, errors.Trace(errInvalidResponse)
}
return msg.GetCopResp(), nil
}