github.com/nyan233/littlerpc@v0.4.6-0.20230316182519-0c8d5c48abaf/core/common/msgparser/alloctor.go (about)

     1  package msgparser
     2  
     3  import (
     4  	"github.com/nyan233/littlerpc/core/protocol/message"
     5  	"sync"
     6  )
     7  
     8  type AllocTor interface {
     9  	AllocMessage() *message.Message
    10  	FreeMessage(message *message.Message)
    11  }
    12  
    13  type SimpleAllocTor struct {
    14  	SharedPool *sync.Pool
    15  }
    16  
    17  func (s *SimpleAllocTor) AllocMessage() *message.Message {
    18  	return s.SharedPool.Get().(*message.Message)
    19  }
    20  
    21  func (s *SimpleAllocTor) FreeMessage(message *message.Message) {
    22  	s.SharedPool.Put(message)
    23  }
    24  
    25  func NewDefaultSimpleAllocTor() AllocTor {
    26  	return &SimpleAllocTor{
    27  		SharedPool: &sync.Pool{
    28  			New: func() interface{} {
    29  				return message.New()
    30  			},
    31  		},
    32  	}
    33  }