-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
65 additions
and
6 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,26 @@ | ||
package iopool | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
) | ||
|
||
type buffer struct { | ||
b []byte | ||
} | ||
|
||
var _copyBufSize = 1024 * 32 | ||
var _pool = sync.Pool{ | ||
New: func() interface{} { | ||
return &buffer{make([]byte, _copyBufSize)} | ||
}, | ||
} | ||
|
||
// Copy wraps the io library's CopyBuffer func with a preallocated buffer from a | ||
// sync.Pool we maintain. | ||
func Copy(dst io.Writer, src io.Reader) (int64, error) { | ||
buf := _pool.Get().(*buffer) | ||
written, err := io.CopyBuffer(dst, src, buf.b) | ||
_pool.Put(buf) | ||
return written, err | ||
} |
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,32 @@ | ||
package iopool | ||
|
||
import ( | ||
"bytes" | ||
"math/rand" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBuffers(t *testing.T) { | ||
var wg sync.WaitGroup | ||
for g := 0; g < 10; g++ { | ||
wg.Add(1) | ||
go func() { | ||
for i := 0; i < 100; i++ { | ||
inputBytes := make([]byte, rand.Intn(5000)+20) | ||
_, err := rand.Read(inputBytes) | ||
assert.NoError(t, err, "Unexpected error from rand.Read") | ||
reader := bytes.NewReader(inputBytes) | ||
|
||
outputBytes := make([]byte, 0, len(inputBytes)) | ||
writer := bytes.NewBuffer(outputBytes) | ||
|
||
Copy(writer, reader) | ||
} | ||
wg.Done() | ||
}() | ||
} | ||
wg.Wait() | ||
} |
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