deps: Add bufferpool dependency
This commit is contained in:
parent
50292c7de7
commit
22e83739c9
7 changed files with 163 additions and 0 deletions
4
Godeps/Godeps.json
generated
4
Godeps/Godeps.json
generated
|
@ -14,6 +14,10 @@
|
||||||
"ImportPath": "github.com/julienschmidt/httprouter",
|
"ImportPath": "github.com/julienschmidt/httprouter",
|
||||||
"Rev": "00ce1c6a267162792c367acc43b1681a884e1872"
|
"Rev": "00ce1c6a267162792c367acc43b1681a884e1872"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "github.com/pushrax/bufferpool",
|
||||||
|
"Rev": "c8962db3ad1e65fe9a83e7298dcd213938cf02d5"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/pushrax/faststats",
|
"ImportPath": "github.com/pushrax/faststats",
|
||||||
"Rev": "0fc2c5e41a187240ffaa09320eea7df9f8071388"
|
"Rev": "0fc2c5e41a187240ffaa09320eea7df9f8071388"
|
||||||
|
|
1
Godeps/_workspace/src/github.com/pushrax/bufferpool/.travis.yml
generated
vendored
Normal file
1
Godeps/_workspace/src/github.com/pushrax/bufferpool/.travis.yml
generated
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
language: go
|
4
Godeps/_workspace/src/github.com/pushrax/bufferpool/AUTHORS
generated
vendored
Normal file
4
Godeps/_workspace/src/github.com/pushrax/bufferpool/AUTHORS
generated
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
# This is the official list of Bufferpool authors for copyright purposes.
|
||||||
|
|
||||||
|
Jimmy Zelinskie <jimmyzelinskie@gmail.com>
|
||||||
|
Justin Li <jli@j-li.net>
|
24
Godeps/_workspace/src/github.com/pushrax/bufferpool/LICENSE
generated
vendored
Normal file
24
Godeps/_workspace/src/github.com/pushrax/bufferpool/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
Bufferpool is released under a BSD 2-Clause license, reproduced below.
|
||||||
|
|
||||||
|
Copyright (c) 2013, The Bufferpool Authors
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without modification,
|
||||||
|
are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer.
|
||||||
|
* Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
||||||
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||||
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
9
Godeps/_workspace/src/github.com/pushrax/bufferpool/README.md
generated
vendored
Normal file
9
Godeps/_workspace/src/github.com/pushrax/bufferpool/README.md
generated
vendored
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
# bufferpool [![Build Status](https://secure.travis-ci.org/pushrax/bufferpool.png)](http://travis-ci.org/pushrax/bufferpool)
|
||||||
|
|
||||||
|
The bufferpool package implements a thread-safe pool of reusable, equally sized `byte.Buffer`s.
|
||||||
|
If you're allocating `byte.Buffer`s very frequently, you can use this to speed up your
|
||||||
|
program and take strain off the garbage collector.
|
||||||
|
|
||||||
|
## docs
|
||||||
|
|
||||||
|
[GoDoc](http://godoc.org/github.com/pushrax/bufferpool)
|
68
Godeps/_workspace/src/github.com/pushrax/bufferpool/bufferpool.go
generated
vendored
Normal file
68
Godeps/_workspace/src/github.com/pushrax/bufferpool/bufferpool.go
generated
vendored
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
// Copyright 2013 The Bufferpool Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by the BSD 2-Clause license,
|
||||||
|
// which can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package bufferpool implements a capacity-limited pool of reusable,
|
||||||
|
// equally-sized buffers.
|
||||||
|
package bufferpool
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A BufferPool is a capacity-limited pool of equally sized buffers.
|
||||||
|
type BufferPool struct {
|
||||||
|
bufferSize int
|
||||||
|
pool chan []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns a newly allocated BufferPool with the given maximum pool size
|
||||||
|
// and buffer size.
|
||||||
|
func New(poolSize, bufferSize int) *BufferPool {
|
||||||
|
return &BufferPool{
|
||||||
|
bufferSize,
|
||||||
|
make(chan []byte, poolSize),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Take is used to obtain a new zeroed buffer. This will allocate a new buffer
|
||||||
|
// if the pool was empty.
|
||||||
|
func (pool *BufferPool) Take() *bytes.Buffer {
|
||||||
|
return bytes.NewBuffer(pool.TakeSlice())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TakeSlice is used to obtain a new slice. This will allocate a new slice
|
||||||
|
// if the pool was empty.
|
||||||
|
func (pool *BufferPool) TakeSlice() (slice []byte) {
|
||||||
|
select {
|
||||||
|
case slice = <-pool.pool:
|
||||||
|
default:
|
||||||
|
slice = make([]byte, 0, pool.bufferSize)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Give is used to attempt to return a buffer to the pool. It may not
|
||||||
|
// be added to the pool if it was already full.
|
||||||
|
func (pool *BufferPool) Give(buf *bytes.Buffer) error {
|
||||||
|
if buf.Len() != pool.bufferSize {
|
||||||
|
return errors.New("Gave an incorrectly sized buffer to the pool.")
|
||||||
|
}
|
||||||
|
|
||||||
|
buf.Reset()
|
||||||
|
slice := buf.Bytes()
|
||||||
|
return pool.GiveSlice(slice[:buf.Len()])
|
||||||
|
}
|
||||||
|
|
||||||
|
// GiveSlice is used to attempt to return a slice to the pool. It may not
|
||||||
|
// be added to the pool if it was already full.
|
||||||
|
func (pool *BufferPool) GiveSlice(slice []byte) error {
|
||||||
|
select {
|
||||||
|
case pool.pool <- slice:
|
||||||
|
// Everything went smoothly!
|
||||||
|
default:
|
||||||
|
return errors.New("Gave a buffer to a full pool.")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
53
Godeps/_workspace/src/github.com/pushrax/bufferpool/bufferpool_test.go
generated
vendored
Normal file
53
Godeps/_workspace/src/github.com/pushrax/bufferpool/bufferpool_test.go
generated
vendored
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
// Copyright 2013 The Bufferpool Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by the BSD 2-Clause license,
|
||||||
|
// which can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package bufferpool_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/pushrax/bufferpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTakeFromEmpty(t *testing.T) {
|
||||||
|
bp := bufferpool.New(1, 1)
|
||||||
|
poolBuf := bp.Take()
|
||||||
|
if !bytes.Equal(poolBuf.Bytes(), []byte("")) {
|
||||||
|
t.Fatalf("Buffer from empty bufferpool was allocated incorrectly.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTakeFromFilled(t *testing.T) {
|
||||||
|
bp := bufferpool.New(1, 1)
|
||||||
|
|
||||||
|
origBuf := bytes.NewBuffer([]byte("X"))
|
||||||
|
bp.Give(origBuf)
|
||||||
|
|
||||||
|
reusedBuf := bp.Take()
|
||||||
|
if !bytes.Equal(reusedBuf.Bytes(), []byte("")) {
|
||||||
|
t.Fatalf("Buffer from filled bufferpool was recycled incorrectly.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compare addresses of the first element in the underlying slice.
|
||||||
|
if &origBuf.Bytes()[:1][0] != &reusedBuf.Bytes()[:1][0] {
|
||||||
|
t.Fatalf("Recycled buffer points at different address.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleNew() {
|
||||||
|
bp := bufferpool.New(10, 255)
|
||||||
|
|
||||||
|
dogBuffer := bp.Take()
|
||||||
|
dogBuffer.WriteString("Dog!")
|
||||||
|
bp.Give(dogBuffer)
|
||||||
|
|
||||||
|
catBuffer := bp.Take() // dogBuffer is reused and reset.
|
||||||
|
catBuffer.WriteString("Cat!")
|
||||||
|
|
||||||
|
fmt.Println(catBuffer)
|
||||||
|
// Output:
|
||||||
|
// Cat!
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue