pool.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package common
  2. import (
  3. "bytes"
  4. "github.com/panjf2000/ants/v2"
  5. "net"
  6. "sync"
  7. )
  8. const PoolSize = 64 * 1024
  9. const PoolSizeSmall = 100
  10. const PoolSizeUdp = 1472
  11. const PoolSizeCopy = 32 << 10
  12. const PoolSizeBuffer = 4096
  13. const PoolSizeWindow = PoolSizeBuffer - 16 - 32 - 32 - 8
  14. var BufPool = sync.Pool{
  15. New: func() interface{} {
  16. return make([]byte, PoolSize)
  17. },
  18. }
  19. var BufPoolUdp = sync.Pool{
  20. New: func() interface{} {
  21. return make([]byte, PoolSizeUdp)
  22. },
  23. }
  24. var BufPoolMax = sync.Pool{
  25. New: func() interface{} {
  26. return make([]byte, PoolSize)
  27. },
  28. }
  29. var BufPoolSmall = sync.Pool{
  30. New: func() interface{} {
  31. return make([]byte, PoolSizeSmall)
  32. },
  33. }
  34. var BufPoolCopy = sync.Pool{
  35. New: func() interface{} {
  36. return make([]byte, PoolSizeCopy)
  37. },
  38. }
  39. func PutBufPoolUdp(buf []byte) {
  40. if cap(buf) == PoolSizeUdp {
  41. BufPoolUdp.Put(buf[:PoolSizeUdp])
  42. }
  43. }
  44. func PutBufPoolCopy(buf []byte) {
  45. if cap(buf) == PoolSizeCopy {
  46. BufPoolCopy.Put(buf[:PoolSizeCopy])
  47. }
  48. }
  49. func GetBufPoolCopy() []byte {
  50. return (BufPoolCopy.Get().([]byte))[:PoolSizeCopy]
  51. }
  52. func PutBufPoolMax(buf []byte) {
  53. if cap(buf) == PoolSize {
  54. BufPoolMax.Put(buf[:PoolSize])
  55. }
  56. }
  57. type copyBufferPool struct {
  58. pool sync.Pool
  59. }
  60. func (Self *copyBufferPool) New() {
  61. Self.pool = sync.Pool{
  62. New: func() interface{} {
  63. return make([]byte, PoolSizeCopy, PoolSizeCopy)
  64. },
  65. }
  66. }
  67. func (Self *copyBufferPool) Get() []byte {
  68. buf := Self.pool.Get().([]byte)
  69. return buf[:PoolSizeCopy] // just like make a new slice, but data may not be 0
  70. }
  71. func (Self *copyBufferPool) Put(x []byte) {
  72. if len(x) == PoolSizeCopy {
  73. Self.pool.Put(x)
  74. } else {
  75. x = nil // buf is not full, not allowed, New method returns a full buf
  76. }
  77. }
  78. type windowBufferPool struct {
  79. pool sync.Pool
  80. }
  81. func (Self *windowBufferPool) New() {
  82. Self.pool = sync.Pool{
  83. New: func() interface{} {
  84. return make([]byte, PoolSizeWindow, PoolSizeWindow)
  85. },
  86. }
  87. }
  88. func (Self *windowBufferPool) Get() (buf []byte) {
  89. buf = Self.pool.Get().([]byte)
  90. return buf[:PoolSizeWindow]
  91. }
  92. func (Self *windowBufferPool) Put(x []byte) {
  93. Self.pool.Put(x[:PoolSizeWindow]) // make buf to full
  94. }
  95. type bufferPool struct {
  96. pool sync.Pool
  97. }
  98. func (Self *bufferPool) New() {
  99. Self.pool = sync.Pool{
  100. New: func() interface{} {
  101. return bytes.NewBuffer(make([]byte, 0, PoolSizeBuffer))
  102. },
  103. }
  104. }
  105. func (Self *bufferPool) Get() *bytes.Buffer {
  106. return Self.pool.Get().(*bytes.Buffer)
  107. }
  108. func (Self *bufferPool) Put(x *bytes.Buffer) {
  109. x.Reset()
  110. Self.pool.Put(x)
  111. }
  112. type muxPackagerPool struct {
  113. pool sync.Pool
  114. }
  115. func (Self *muxPackagerPool) New() {
  116. Self.pool = sync.Pool{
  117. New: func() interface{} {
  118. pack := MuxPackager{}
  119. return &pack
  120. },
  121. }
  122. }
  123. func (Self *muxPackagerPool) Get() *MuxPackager {
  124. return Self.pool.Get().(*MuxPackager)
  125. }
  126. func (Self *muxPackagerPool) Put(pack *MuxPackager) {
  127. Self.pool.Put(pack)
  128. }
  129. type connGroup struct {
  130. src net.Conn
  131. dst net.Conn
  132. wg *sync.WaitGroup
  133. }
  134. func newConnGroup(src net.Conn, dst net.Conn, wg *sync.WaitGroup) connGroup {
  135. return connGroup{
  136. src: src,
  137. dst: dst,
  138. wg: wg,
  139. }
  140. }
  141. func copyConnGroup(group interface{}) {
  142. cg, ok := group.(connGroup)
  143. if !ok {
  144. return
  145. }
  146. _, err := CopyBuffer(cg.src, cg.dst)
  147. if err != nil {
  148. cg.src.Close()
  149. cg.dst.Close()
  150. //logs.Warn("close npc by copy from nps", err, c.connId)
  151. }
  152. cg.wg.Done()
  153. }
  154. type Conns struct {
  155. conn1 net.Conn
  156. conn2 net.Conn
  157. }
  158. func NewConns(c1 net.Conn, c2 net.Conn) Conns {
  159. return Conns{
  160. conn1: c1,
  161. conn2: c2,
  162. }
  163. }
  164. func copyConns(group interface{}) {
  165. conns := group.(Conns)
  166. wg := new(sync.WaitGroup)
  167. wg.Add(2)
  168. _ = connCopyPool.Invoke(newConnGroup(conns.conn1, conns.conn2, wg))
  169. _ = connCopyPool.Invoke(newConnGroup(conns.conn2, conns.conn1, wg))
  170. wg.Wait()
  171. }
  172. var once = sync.Once{}
  173. var BuffPool = bufferPool{}
  174. var CopyBuff = copyBufferPool{}
  175. var MuxPack = muxPackagerPool{}
  176. var WindowBuff = windowBufferPool{}
  177. var connCopyPool, _ = ants.NewPoolWithFunc(200000, copyConnGroup, ants.WithNonblocking(false))
  178. var CopyConnsPool, _ = ants.NewPoolWithFunc(100000, copyConns, ants.WithNonblocking(false))
  179. func newPool() {
  180. BuffPool.New()
  181. CopyBuff.New()
  182. MuxPack.New()
  183. WindowBuff.New()
  184. }
  185. func init() {
  186. once.Do(newPool)
  187. }