-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from getlantern/ox/bound-memory
- Loading branch information
Showing
5 changed files
with
64 additions
and
17 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
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
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,29 @@ | ||
package golog | ||
|
||
import ( | ||
"bytes" | ||
|
||
"github.com/oxtoacart/bpool" | ||
) | ||
|
||
const ( | ||
// Together, these bound the buffer pool to 307 KB | ||
bufferPoolSize = 200 | ||
maxBufferSize = 1024 | ||
) | ||
|
||
var ( | ||
_bufferPool = bpool.NewBufferPool(bufferPoolSize) | ||
) | ||
|
||
func getBuffer() *bytes.Buffer { | ||
return _bufferPool.Get() | ||
} | ||
|
||
func returnBuffer(buf *bytes.Buffer) bool { | ||
if buf.Cap() <= maxBufferSize { | ||
_bufferPool.Put(buf) | ||
return true | ||
} | ||
return false | ||
} |
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,21 @@ | ||
package golog | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPool(t *testing.T) { | ||
buf := _bufferPool.Get() | ||
require.NotNil(t, buf) | ||
// Write 768 bytes (the max before the buffer's capacity exceeds ) | ||
for i := 0; i < maxBufferSize; i++ { | ||
buf.WriteByte('C') | ||
} | ||
require.True(t, returnBuffer(buf)) | ||
for i := 0; i <= maxBufferSize; i++ { | ||
buf.WriteByte('C') | ||
} | ||
require.False(t, returnBuffer(buf)) | ||
} |
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