pool.go 3.1 KB

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