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

     1  package core
     2  
     3  import (
     4  	"github.com/15mga/kiwi"
     5  	"sync"
     6  
     7  	"github.com/15mga/kiwi/util"
     8  )
     9  
    10  var (
    11  	_PusPool = sync.Pool{
    12  		New: func() any {
    13  			return &SPush{}
    14  		},
    15  	}
    16  	GenHead util.FnM = func(m util.M) {
    17  
    18  	}
    19  )
    20  
    21  func newSPush(pid int64, head util.M, msg util.IMsg) *SPush {
    22  	payload, err := kiwi.Codec().PbMarshal(msg)
    23  	if err != nil {
    24  		kiwi.Fatal(err)
    25  		return nil
    26  	}
    27  
    28  	if head == nil {
    29  		head = util.M{}
    30  	}
    31  	GenHead(head)
    32  	svc, code := kiwi.Codec().MsgToSvcCode(msg)
    33  
    34  	pus := _PusPool.Get().(*SPush)
    35  	pus.msg = msg
    36  	pus.pid = pid
    37  	pus.svc, pus.code = svc, code
    38  	pus.head = head
    39  	pus.payload = payload
    40  	pus.InitHead()
    41  	pus.tid = kiwi.TC(pid, head, IsExcludeLog(svc, code))
    42  	return pus
    43  }
    44  
    45  type SPush struct {
    46  	sndPkt
    47  }
    48  
    49  func (p *SPush) Dispose() {
    50  	p.sndPkt.Dispose()
    51  	_PusPool.Put(p)
    52  }
    53  
    54  func Pus(pid int64, head util.M, msg util.IMsg) int64 {
    55  	pus := newSPush(pid, head, msg)
    56  	kiwi.Node().Push(pus)
    57  	return pus.tid
    58  }
    59  
    60  func PusNode(pid, nodeId int64, head util.M, msg util.IMsg) int64 {
    61  	pus := newSPush(pid, head, msg)
    62  	kiwi.Node().PushNode(nodeId, pus)
    63  	return pus.tid
    64  }