-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an API to plug a custom buffer free item mangement system
- Loading branch information
David Bariod
committed
May 28, 2020
1 parent
42baed8
commit 64a5944
Showing
2 changed files
with
57 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package logrus | ||
|
||
import ( | ||
"bytes" | ||
"sync" | ||
) | ||
|
||
var ( | ||
bufferPool BufferPool | ||
) | ||
|
||
type BufferPool interface { | ||
Put(*bytes.Buffer) | ||
Get() *bytes.Buffer | ||
} | ||
|
||
type defaultPool struct { | ||
pool *sync.Pool | ||
} | ||
|
||
func (p *defaultPool) Put(buf *bytes.Buffer) { | ||
p.pool.Put(buf) | ||
} | ||
|
||
func (p *defaultPool) Get() *bytes.Buffer { | ||
return p.pool.Get().(*bytes.Buffer) | ||
} | ||
|
||
func getBuffer() *bytes.Buffer { | ||
return bufferPool.Get() | ||
} | ||
|
||
func putBuffer(buf *bytes.Buffer) { | ||
buf.Reset() | ||
bufferPool.Put(buf) | ||
} | ||
|
||
// SetBufferPool allows to replace the default logrus buffer pool | ||
// to better meets the specific needs of an application. | ||
func SetBufferPool(bp BufferPool) { | ||
bufferPool = bp | ||
} | ||
|
||
func init() { | ||
SetBufferPool(&defaultPool{ | ||
pool: &sync.Pool{ | ||
New: func() interface{} { | ||
return new(bytes.Buffer) | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters