kesavand | c71914f | 2022-03-25 11:19:03 +0530 | [diff] [blame^] | 1 | package logrus |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "sync" |
| 6 | ) |
| 7 | |
| 8 | var ( |
| 9 | bufferPool BufferPool |
| 10 | ) |
| 11 | |
| 12 | type BufferPool interface { |
| 13 | Put(*bytes.Buffer) |
| 14 | Get() *bytes.Buffer |
| 15 | } |
| 16 | |
| 17 | type defaultPool struct { |
| 18 | pool *sync.Pool |
| 19 | } |
| 20 | |
| 21 | func (p *defaultPool) Put(buf *bytes.Buffer) { |
| 22 | p.pool.Put(buf) |
| 23 | } |
| 24 | |
| 25 | func (p *defaultPool) Get() *bytes.Buffer { |
| 26 | return p.pool.Get().(*bytes.Buffer) |
| 27 | } |
| 28 | |
| 29 | func getBuffer() *bytes.Buffer { |
| 30 | return bufferPool.Get() |
| 31 | } |
| 32 | |
| 33 | func putBuffer(buf *bytes.Buffer) { |
| 34 | buf.Reset() |
| 35 | bufferPool.Put(buf) |
| 36 | } |
| 37 | |
| 38 | // SetBufferPool allows to replace the default logrus buffer pool |
| 39 | // to better meets the specific needs of an application. |
| 40 | func SetBufferPool(bp BufferPool) { |
| 41 | bufferPool = bp |
| 42 | } |
| 43 | |
| 44 | func init() { |
| 45 | SetBufferPool(&defaultPool{ |
| 46 | pool: &sync.Pool{ |
| 47 | New: func() interface{} { |
| 48 | return new(bytes.Buffer) |
| 49 | }, |
| 50 | }, |
| 51 | }) |
| 52 | } |