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

     1  package sharedpool
     2  
     3  import (
     4  	"github.com/nyan233/littlerpc/core/container"
     5  	"github.com/nyan233/littlerpc/core/protocol/message"
     6  	"sync"
     7  )
     8  
     9  type SharedPool struct {
    10  	bytesPoolRingIndex int64
    11  	sharedBytesPool    sync.Pool
    12  	messagePoolIndex   int64
    13  	sharedMessagePool  sync.Pool
    14  }
    15  
    16  func NewSharedPool() *SharedPool {
    17  	pool := &SharedPool{}
    18  	pool.bytesPoolRingIndex = -1
    19  	pool.messagePoolIndex = -1
    20  	pool.sharedBytesPool = sync.Pool{
    21  		New: func() interface{} {
    22  			var b container.Slice[byte] = make([]byte, 0, 4096)
    23  			return &b
    24  		},
    25  	}
    26  	pool.sharedMessagePool = sync.Pool{
    27  		New: func() interface{} {
    28  			return message.New()
    29  		},
    30  	}
    31  	return pool
    32  }
    33  
    34  func (p *SharedPool) TakeBytesPool() *sync.Pool {
    35  	return &p.sharedBytesPool
    36  }
    37  
    38  func (p *SharedPool) TakeMessagePool() *sync.Pool {
    39  	return &p.sharedMessagePool
    40  }