Permalink
Browse files

removed the need for the drain buffer. renamed bufferPoolEntry.buf to…

… br for clarity. (thanks bradfitz)
  • Loading branch information...
1 parent 9187ae6 commit 14a91d40a55d23940ef24056417e0e66a08862c7 @dgrijalva dgrijalva committed Apr 23, 2012
Showing with 10 additions and 12 deletions.
  1. +5 −7 buffer_pool.go
  2. +4 −4 buffer_pool_test.go
  3. +1 −1 server.go
View
@@ -3,6 +3,7 @@ package falcore
import (
"bufio"
"io"
+ "io/ioutil"
)
// uses a chan as a leaky bucket buffer pool
@@ -11,15 +12,13 @@ type bufferPool struct {
bufSize int
// the actual pool of buffers ready for reuse
pool chan *bufferPoolEntry
- // this is used for draining a buffer to prep for reuse
- drain []byte
}
// This is what's stored in the buffer. It allows
// for the underlying io.Reader to be changed out
// inside a bufio.Reader. This is required for reuse.
type bufferPoolEntry struct {
- buf *bufio.Reader
+ br *bufio.Reader
source io.Reader
}
@@ -32,7 +31,6 @@ func newBufferPool(poolSize, bufferSize int) *bufferPool {
return &bufferPool{
bufSize: bufferSize,
pool: make(chan *bufferPoolEntry, poolSize),
- drain: make([]byte, bufferSize),
}
}
@@ -42,16 +40,16 @@ func (p *bufferPool) take(r io.Reader) (bpe *bufferPoolEntry) {
select {
case bpe = <-p.pool:
// prepare for reuse
- if a := bpe.buf.Buffered(); a > 0 {
+ if a := bpe.br.Buffered(); a > 0 {
// drain the internal buffer
- bpe.buf.Read(p.drain[0:a])
+ io.CopyN(ioutil.Discard, bpe.br, int64(a))
}
// swap out the underlying reader
bpe.source = r
default:
// none available. create a new one
bpe = &bufferPoolEntry{nil, r}
- bpe.buf = bufio.NewReaderSize(bpe, p.bufSize)
+ bpe.br = bufio.NewReaderSize(bpe, p.bufSize)
}
return
}
View
@@ -15,7 +15,7 @@ func TestBufferPool(t *testing.T) {
bpe := pool.take(bytes.NewBuffer(text))
// read all
out := make([]byte, 1024)
- l, _ := bpe.buf.Read(out)
+ l, _ := bpe.br.Read(out)
if bytes.Compare(out[0:l], text) != 0 {
t.Errorf("Read invalid data: %v", out)
}
@@ -28,7 +28,7 @@ func TestBufferPool(t *testing.T) {
bpe = pool.take(bytes.NewBuffer(text))
// read all
out = make([]byte, 1024)
- l, _ = bpe.buf.Read(out)
+ l, _ = bpe.br.Read(out)
if bytes.Compare(out[0:l], text) != 0 {
t.Errorf("Read invalid data: %v", out)
}
@@ -41,14 +41,14 @@ func TestBufferPool(t *testing.T) {
bpe = pool.take(bytes.NewBuffer(text))
// read 1 byte
out = make([]byte, 1)
- bpe.buf.Read(out)
+ bpe.br.Read(out)
pool.give(bpe)
// get one
bpe = pool.take(bytes.NewBuffer(text))
// read all
out = make([]byte, 1024)
- l, _ = bpe.buf.Read(out)
+ l, _ = bpe.br.Read(out)
if bytes.Compare(out[0:l], text) != 0 {
t.Errorf("Read invalid data: %v", out)
}
View
@@ -203,7 +203,7 @@ func (srv *Server) handler(c net.Conn) {
reqCount := 0
keepAlive := true
for err == nil && keepAlive {
- if req, err = http.ReadRequest(bpe.buf); err == nil {
+ if req, err = http.ReadRequest(bpe.br); err == nil {
if req.Header.Get("Connection") != "Keep-Alive" {
keepAlive = false
}

0 comments on commit 14a91d4

Please sign in to comment.