buffer.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package alloc
  2. import (
  3. "time"
  4. )
  5. // Buffer is a recyclable allocation of a byte array. Buffer.Release() recycles
  6. // the buffer into an internal buffer pool, in order to recreate a buffer more
  7. // quickly.
  8. type Buffer struct {
  9. head []byte
  10. pool *bufferPool
  11. Value []byte
  12. }
  13. // Release recycles the buffer into an internal buffer pool.
  14. func (b *Buffer) Release() {
  15. b.pool.free(b)
  16. b.head = nil
  17. b.Value = nil
  18. b.pool = nil
  19. }
  20. // Clear clears the content of the buffer, results an empty buffer with
  21. // Len() = 0.
  22. func (b *Buffer) Clear() *Buffer {
  23. b.Value = b.head[:0]
  24. return b
  25. }
  26. // AppendBytes appends one or more bytes to the end of the buffer.
  27. func (b *Buffer) AppendBytes(bytes ...byte) *Buffer {
  28. b.Value = append(b.Value, bytes...)
  29. return b
  30. }
  31. // Append appends a byte array to the end of the buffer.
  32. func (b *Buffer) Append(data []byte) *Buffer {
  33. b.Value = append(b.Value, data...)
  34. return b
  35. }
  36. // Slice cuts the buffer at the given position.
  37. func (b *Buffer) Slice(from, to int) *Buffer {
  38. b.Value = b.Value[from:to]
  39. return b
  40. }
  41. // SliceFrom cuts the buffer at the given position.
  42. func (b *Buffer) SliceFrom(from int) *Buffer {
  43. b.Value = b.Value[from:]
  44. return b
  45. }
  46. // Len returns the length of the buffer content.
  47. func (b *Buffer) Len() int {
  48. return len(b.Value)
  49. }
  50. // IsFull returns true if the buffer has no more room to grow.
  51. func (b *Buffer) IsFull() bool {
  52. return len(b.Value) == cap(b.Value)
  53. }
  54. // Write implements Write method in io.Writer.
  55. func (b *Buffer) Write(data []byte) (int, error) {
  56. b.Append(data)
  57. return len(data), nil
  58. }
  59. type bufferPool struct {
  60. chain chan []byte
  61. bufferSize int
  62. buffers2Keep int
  63. }
  64. func newBufferPool(bufferSize, buffers2Keep, poolSize int) *bufferPool {
  65. pool := &bufferPool{
  66. chain: make(chan []byte, poolSize),
  67. bufferSize: bufferSize,
  68. buffers2Keep: buffers2Keep,
  69. }
  70. for i := 0; i < buffers2Keep; i++ {
  71. pool.chain <- make([]byte, bufferSize)
  72. }
  73. go pool.cleanup(time.Tick(1 * time.Second))
  74. return pool
  75. }
  76. func (p *bufferPool) allocate() *Buffer {
  77. var b []byte
  78. select {
  79. case b = <-p.chain:
  80. default:
  81. b = make([]byte, p.bufferSize)
  82. }
  83. return &Buffer{
  84. head: b,
  85. pool: p,
  86. Value: b,
  87. }
  88. }
  89. func (p *bufferPool) free(buffer *Buffer) {
  90. select {
  91. case p.chain <- buffer.head:
  92. default:
  93. }
  94. }
  95. func (p *bufferPool) cleanup(tick <-chan time.Time) {
  96. for range tick {
  97. pSize := len(p.chain)
  98. if pSize > p.buffers2Keep {
  99. <-p.chain
  100. continue
  101. }
  102. for delta := p.buffers2Keep - pSize; delta > 0; delta-- {
  103. select {
  104. case p.chain <- make([]byte, p.bufferSize):
  105. default:
  106. }
  107. }
  108. }
  109. }
  110. var smallPool = newBufferPool(1024, 16, 64)
  111. var mediumPool = newBufferPool(8*1024, 256, 2048)
  112. var largePool = newBufferPool(64*1024, 128, 1024)
  113. // NewSmallBuffer creates a Buffer with 1K bytes of arbitrary content.
  114. func NewSmallBuffer() *Buffer {
  115. return smallPool.allocate()
  116. }
  117. // NewBuffer creates a Buffer with 8K bytes of arbitrary content.
  118. func NewBuffer() *Buffer {
  119. return mediumPool.allocate()
  120. }
  121. // NewLargeBuffer creates a Buffer with 64K bytes of arbitrary content.
  122. func NewLargeBuffer() *Buffer {
  123. return largePool.allocate()
  124. }