pool.go 732 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package utils
  2. import (
  3. "sync"
  4. )
  5. const poolSize = 64 * 1024
  6. const poolSizeSmall = 100
  7. const poolSizeUdp = 1472
  8. const poolSizeCopy = 32 * 1024
  9. var BufPool = sync.Pool{
  10. New: func() interface{} {
  11. return make([]byte, poolSize)
  12. },
  13. }
  14. var BufPoolUdp = sync.Pool{
  15. New: func() interface{} {
  16. return make([]byte, poolSizeUdp)
  17. },
  18. }
  19. var BufPoolMax = sync.Pool{
  20. New: func() interface{} {
  21. return make([]byte, poolSize)
  22. },
  23. }
  24. var BufPoolSmall = sync.Pool{
  25. New: func() interface{} {
  26. return make([]byte, poolSizeSmall)
  27. },
  28. }
  29. var BufPoolCopy = sync.Pool{
  30. New: func() interface{} {
  31. return make([]byte, poolSizeCopy)
  32. },
  33. }
  34. func PutBufPoolCopy(buf []byte) {
  35. if cap(buf) == poolSizeCopy {
  36. BufPoolCopy.Put(buf[:poolSizeCopy])
  37. }
  38. }