github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/core/rcv_request.go (about)

     1  package core
     2  
     3  import (
     4  	"github.com/15mga/kiwi"
     5  	"github.com/15mga/kiwi/util"
     6  	"sync/atomic"
     7  	"time"
     8  )
     9  
    10  type RcvReqPkt struct {
    11  	rcvPkt
    12  }
    13  
    14  func (p *RcvReqPkt) Ok(msg util.IMsg) {
    15  	if !IsExcludeLog(p.svc, p.code) {
    16  		sndTs, _ := util.MGet[int64](p.head, HeadSndTs)
    17  		kiwi.TI(p.tid, "ok", util.M{
    18  			"dur":  time.Now().UnixMilli() - sndTs,
    19  			"name": p.msg.ProtoReflect().Descriptor().Name(),
    20  			"req":  p.msg,
    21  			"res":  msg,
    22  		})
    23  	}
    24  	p.Complete()
    25  	if p.senderId == kiwi.GetNodeMeta().NodeId {
    26  		kiwi.Router().OnResponseOk(p.tid, p.head, msg)
    27  		return
    28  	}
    29  	var (
    30  		payload []byte
    31  		err     *util.Err
    32  	)
    33  	if p.json {
    34  		payload, err = kiwi.Codec().JsonMarshal(msg)
    35  	} else {
    36  		payload, err = kiwi.Codec().PbMarshal(msg)
    37  	}
    38  	if err != nil {
    39  		kiwi.Error(err)
    40  		return
    41  	}
    42  	res, err := kiwi.Packer().PackResponseOk(p.tid, p.head, payload)
    43  	if err != nil {
    44  		kiwi.Error(err)
    45  		return
    46  	}
    47  	kiwi.Node().SendToNode(p.senderId, res, p.onSendErr)
    48  }
    49  
    50  func (p *RcvReqPkt) Err(err *util.Err) {
    51  	if err.Code() < util.EcMin {
    52  		p.Fail(err.Code())
    53  		return
    54  	}
    55  	sndTs, _ := util.MGet[int64](p.head, HeadSndTs)
    56  	err.AddParam("dur", time.Now().UnixMilli()-sndTs)
    57  	err.AddParam("name", p.msg.ProtoReflect().Descriptor().Name())
    58  	err.AddParam("req", p.msg)
    59  	p.rcvPkt.Err(err)
    60  	if p.senderId == kiwi.GetNodeMeta().NodeId {
    61  		kiwi.Router().OnResponseFail(p.tid, p.head, err.Code())
    62  		return
    63  	}
    64  	payload, e := kiwi.Packer().PackResponseFail(p.tid, p.head, err.Code())
    65  	if e != nil {
    66  		kiwi.Error(e)
    67  		return
    68  	}
    69  	kiwi.Node().SendToNode(p.senderId, payload, p.onSendErr)
    70  }
    71  
    72  func (p *RcvReqPkt) Fail(code uint16) {
    73  	if !IsExcludeLog(p.svc, p.code) {
    74  		sndTs, _ := util.MGet[int64](p.head, HeadSndTs)
    75  		kiwi.TI(p.tid, "fail", util.M{
    76  			"dur":   time.Now().UnixMilli() - sndTs,
    77  			"name":  p.msg.ProtoReflect().Descriptor().Name(),
    78  			"req":   p.msg,
    79  			"error": util.ErrCodeToStr(code),
    80  		})
    81  	}
    82  	p.Complete()
    83  	if p.senderId == kiwi.GetNodeMeta().NodeId {
    84  		kiwi.Router().OnResponseFail(p.tid, p.head, code)
    85  		return
    86  	}
    87  	payload, e := kiwi.Packer().PackResponseFail(p.tid, p.head, code)
    88  	if e != nil {
    89  		kiwi.Error(e)
    90  		return
    91  	}
    92  	kiwi.Node().SendToNode(p.senderId, payload, p.onSendErr)
    93  }
    94  
    95  func (p *RcvReqPkt) onSendErr(err *util.Err) {
    96  	if err == nil {
    97  		return
    98  	}
    99  	atomic.AddUint64(&_ResponseSendFailCount, 1)
   100  	kiwi.TE(p.tid, err)
   101  }