buffer_pool.go 663 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package logrus
  2. import (
  3. "bytes"
  4. "sync"
  5. )
  6. var (
  7. bufferPool BufferPool
  8. )
  9. type BufferPool interface {
  10. Put(*bytes.Buffer)
  11. Get() *bytes.Buffer
  12. }
  13. type defaultPool struct {
  14. pool *sync.Pool
  15. }
  16. func (p *defaultPool) Put(buf *bytes.Buffer) {
  17. p.pool.Put(buf)
  18. }
  19. func (p *defaultPool) Get() *bytes.Buffer {
  20. return p.pool.Get().(*bytes.Buffer)
  21. }
  22. // SetBufferPool allows to replace the default logrus buffer pool
  23. // to better meets the specific needs of an application.
  24. func SetBufferPool(bp BufferPool) {
  25. bufferPool = bp
  26. }
  27. func init() {
  28. SetBufferPool(&defaultPool{
  29. pool: &sync.Pool{
  30. New: func() interface{} {
  31. return new(bytes.Buffer)
  32. },
  33. },
  34. })
  35. }