Skip to content

Commit

Permalink
removed the need for the drain buffer. renamed bufferPoolEntry.buf to…
Browse files Browse the repository at this point in the history
… br for clarity. (thanks bradfitz)
  • Loading branch information
dgrijalva committed Apr 23, 2012
1 parent 9187ae6 commit 14a91d4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
12 changes: 5 additions & 7 deletions buffer_pool.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package falcore
import ( import (
"bufio" "bufio"
"io" "io"
"io/ioutil"
) )


// uses a chan as a leaky bucket buffer pool // uses a chan as a leaky bucket buffer pool
Expand All @@ -11,15 +12,13 @@ type bufferPool struct {
bufSize int bufSize int
// the actual pool of buffers ready for reuse // the actual pool of buffers ready for reuse
pool chan *bufferPoolEntry 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 // This is what's stored in the buffer. It allows
// for the underlying io.Reader to be changed out // for the underlying io.Reader to be changed out
// inside a bufio.Reader. This is required for reuse. // inside a bufio.Reader. This is required for reuse.
type bufferPoolEntry struct { type bufferPoolEntry struct {
buf *bufio.Reader br *bufio.Reader
source io.Reader source io.Reader
} }


Expand All @@ -32,7 +31,6 @@ func newBufferPool(poolSize, bufferSize int) *bufferPool {
return &bufferPool{ return &bufferPool{
bufSize: bufferSize, bufSize: bufferSize,
pool: make(chan *bufferPoolEntry, poolSize), pool: make(chan *bufferPoolEntry, poolSize),
drain: make([]byte, bufferSize),
} }
} }


Expand All @@ -42,16 +40,16 @@ func (p *bufferPool) take(r io.Reader) (bpe *bufferPoolEntry) {
select { select {
case bpe = <-p.pool: case bpe = <-p.pool:
// prepare for reuse // prepare for reuse
if a := bpe.buf.Buffered(); a > 0 { if a := bpe.br.Buffered(); a > 0 {
// drain the internal buffer // drain the internal buffer
bpe.buf.Read(p.drain[0:a]) io.CopyN(ioutil.Discard, bpe.br, int64(a))
} }
// swap out the underlying reader // swap out the underlying reader
bpe.source = r bpe.source = r
default: default:
// none available. create a new one // none available. create a new one
bpe = &bufferPoolEntry{nil, r} bpe = &bufferPoolEntry{nil, r}
bpe.buf = bufio.NewReaderSize(bpe, p.bufSize) bpe.br = bufio.NewReaderSize(bpe, p.bufSize)
} }
return return
} }
Expand Down
8 changes: 4 additions & 4 deletions buffer_pool_test.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestBufferPool(t *testing.T) {
bpe := pool.take(bytes.NewBuffer(text)) bpe := pool.take(bytes.NewBuffer(text))
// read all // read all
out := make([]byte, 1024) out := make([]byte, 1024)
l, _ := bpe.buf.Read(out) l, _ := bpe.br.Read(out)
if bytes.Compare(out[0:l], text) != 0 { if bytes.Compare(out[0:l], text) != 0 {
t.Errorf("Read invalid data: %v", out) t.Errorf("Read invalid data: %v", out)
} }
Expand All @@ -28,7 +28,7 @@ func TestBufferPool(t *testing.T) {
bpe = pool.take(bytes.NewBuffer(text)) bpe = pool.take(bytes.NewBuffer(text))
// read all // read all
out = make([]byte, 1024) out = make([]byte, 1024)
l, _ = bpe.buf.Read(out) l, _ = bpe.br.Read(out)
if bytes.Compare(out[0:l], text) != 0 { if bytes.Compare(out[0:l], text) != 0 {
t.Errorf("Read invalid data: %v", out) t.Errorf("Read invalid data: %v", out)
} }
Expand All @@ -41,14 +41,14 @@ func TestBufferPool(t *testing.T) {
bpe = pool.take(bytes.NewBuffer(text)) bpe = pool.take(bytes.NewBuffer(text))
// read 1 byte // read 1 byte
out = make([]byte, 1) out = make([]byte, 1)
bpe.buf.Read(out) bpe.br.Read(out)
pool.give(bpe) pool.give(bpe)


// get one // get one
bpe = pool.take(bytes.NewBuffer(text)) bpe = pool.take(bytes.NewBuffer(text))
// read all // read all
out = make([]byte, 1024) out = make([]byte, 1024)
l, _ = bpe.buf.Read(out) l, _ = bpe.br.Read(out)
if bytes.Compare(out[0:l], text) != 0 { if bytes.Compare(out[0:l], text) != 0 {
t.Errorf("Read invalid data: %v", out) t.Errorf("Read invalid data: %v", out)
} }
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (srv *Server) handler(c net.Conn) {
reqCount := 0 reqCount := 0
keepAlive := true keepAlive := true
for err == nil && keepAlive { 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" { if req.Header.Get("Connection") != "Keep-Alive" {
keepAlive = false keepAlive = false
} }
Expand Down

0 comments on commit 14a91d4

Please sign in to comment.