pool.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package core
  2. import (
  3. "bytes"
  4. "sync"
  5. )
  6. const PoolSize = 64 * 1024
  7. const PoolSizeSmall = 100
  8. const PoolSizeUdp = 1472
  9. const PoolSizeCopy = 32 << 10
  10. const PoolSizeWindow = 1<<16 - 1
  11. var BufPool = sync.Pool{
  12. New: func() interface{} {
  13. return make([]byte, PoolSize)
  14. },
  15. }
  16. var BufPoolUdp = sync.Pool{
  17. New: func() interface{} {
  18. return make([]byte, PoolSizeUdp)
  19. },
  20. }
  21. var BufPoolMax = sync.Pool{
  22. New: func() interface{} {
  23. return make([]byte, PoolSize)
  24. },
  25. }
  26. var BufPoolSmall = sync.Pool{
  27. New: func() interface{} {
  28. return make([]byte, PoolSizeSmall)
  29. },
  30. }
  31. var BufPoolCopy = sync.Pool{
  32. New: func() interface{} {
  33. return make([]byte, PoolSizeCopy)
  34. },
  35. }
  36. func PutBufPoolUdp(buf []byte) {
  37. if cap(buf) == PoolSizeUdp {
  38. BufPoolUdp.Put(buf[:PoolSizeUdp])
  39. }
  40. }
  41. func PutBufPoolCopy(buf []byte) {
  42. if cap(buf) == PoolSizeCopy {
  43. BufPoolCopy.Put(buf[:PoolSizeCopy])
  44. }
  45. }
  46. func GetBufPoolCopy() []byte {
  47. return (BufPoolCopy.Get().([]byte))[:PoolSizeCopy]
  48. }
  49. func PutBufPoolMax(buf []byte) {
  50. if cap(buf) == PoolSize {
  51. BufPoolMax.Put(buf[:PoolSize])
  52. }
  53. }
  54. type copyBufferPool struct {
  55. pool sync.Pool
  56. }
  57. func (Self *copyBufferPool) New() {
  58. Self.pool = sync.Pool{
  59. New: func() interface{} {
  60. return make([]byte, PoolSizeCopy, PoolSizeCopy)
  61. },
  62. }
  63. }
  64. func (Self *copyBufferPool) Get() []byte {
  65. buf := Self.pool.Get().([]byte)
  66. return buf[:PoolSizeCopy] // just like make a new slice, but data may not be 0
  67. }
  68. func (Self *copyBufferPool) Put(x []byte) {
  69. if len(x) == PoolSizeCopy {
  70. Self.pool.Put(x)
  71. } else {
  72. x = nil // buf is not full, not allowed, New method returns a full buf
  73. }
  74. }
  75. type windowBufferPool struct {
  76. pool sync.Pool
  77. }
  78. func (Self *windowBufferPool) New() {
  79. Self.pool = sync.Pool{
  80. New: func() interface{} {
  81. return make([]byte, 0, PoolSizeWindow)
  82. },
  83. }
  84. }
  85. func (Self *windowBufferPool) Get() (buf []byte) {
  86. buf = Self.pool.Get().([]byte)
  87. return buf[:0]
  88. }
  89. func (Self *windowBufferPool) Put(x []byte) {
  90. if cap(x) == PoolSizeWindow {
  91. Self.pool.Put(x[:PoolSizeWindow]) // make buf to full
  92. } else {
  93. x = nil
  94. }
  95. }
  96. type bufferPool struct {
  97. pool sync.Pool
  98. }
  99. func (Self *bufferPool) New() {
  100. Self.pool = sync.Pool{
  101. New: func() interface{} {
  102. return new(bytes.Buffer)
  103. },
  104. }
  105. }
  106. func (Self *bufferPool) Get() *bytes.Buffer {
  107. return Self.pool.Get().(*bytes.Buffer)
  108. }
  109. func (Self *bufferPool) Put(x *bytes.Buffer) {
  110. x.Reset()
  111. Self.pool.Put(x)
  112. }
  113. type muxPackagerPool struct {
  114. pool sync.Pool
  115. }
  116. func (Self *muxPackagerPool) New() {
  117. Self.pool = sync.Pool{
  118. New: func() interface{} {
  119. pack := MuxPackager{}
  120. return &pack
  121. },
  122. }
  123. }
  124. func (Self *muxPackagerPool) Get() *MuxPackager {
  125. pack := Self.pool.Get().(*MuxPackager)
  126. buf := CopyBuff.Get()
  127. pack.Content = buf
  128. return pack
  129. }
  130. func (Self *muxPackagerPool) Put(pack *MuxPackager) {
  131. CopyBuff.Put(pack.Content)
  132. Self.pool.Put(pack)
  133. }
  134. var once = sync.Once{}
  135. var BuffPool = bufferPool{}
  136. var CopyBuff = copyBufferPool{}
  137. var MuxPack = muxPackagerPool{}
  138. var WindowBuff = windowBufferPool{}
  139. func newPool() {
  140. BuffPool.New()
  141. CopyBuff.New()
  142. MuxPack.New()
  143. WindowBuff.New()
  144. }
  145. func init() {
  146. once.Do(newPool)
  147. }