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

     1  package core
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/15mga/kiwi"
     7  	"github.com/15mga/kiwi/util"
     8  )
     9  
    10  var (
    11  	_NtfPool = sync.Pool{
    12  		New: func() any {
    13  			return &SNotify{}
    14  		},
    15  	}
    16  )
    17  
    18  func newNotify(pid int64, head util.M, msg util.IMsg) *SNotify {
    19  	payload, err := kiwi.Codec().PbMarshal(msg)
    20  	if err != nil {
    21  		kiwi.Fatal(err)
    22  		return nil
    23  	}
    24  	if head == nil {
    25  		head = util.M{}
    26  	}
    27  	GenHead(head)
    28  	svc, code := kiwi.Codec().MsgToSvcCode(msg)
    29  	ntf := _NtfPool.Get().(*SNotify)
    30  	ntf.pid = pid
    31  	ntf.tid = kiwi.TC(pid, head, IsExcludeLog(svc, code))
    32  	ntf.svc, ntf.code = svc, code
    33  	ntf.json = false
    34  	ntf.head = head
    35  	ntf.payload = payload
    36  	ntf.InitHead()
    37  	return ntf
    38  }
    39  
    40  type SNotify struct {
    41  	sndPkt
    42  }
    43  
    44  func (n *SNotify) Dispose() {
    45  	n.sndPkt.Dispose()
    46  	_NtfPool.Put(n)
    47  }
    48  
    49  func Ntf(pid int64, head util.M, msg util.IMsg) int64 {
    50  	ntf := newNotify(pid, head, msg)
    51  	kiwi.Node().Notify(ntf)
    52  	return ntf.tid
    53  }