Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a buffer around the broker's net.Conn for reads #584

Merged
merged 2 commits into from
Dec 15, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (b *Broker) Open(conf *Config) error {
Logger.Printf("Failed to connect to broker %s: %s\n", b.addr, b.connErr)
return
}
b.conn = NewBufConn(b.conn)

b.conf = conf
b.done = make(chan bool)
Expand Down
24 changes: 23 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package sarama

import "sort"
import (
"bufio"
"net"
"sort"
)

type none struct{}

Expand Down Expand Up @@ -87,3 +91,21 @@ func (b ByteEncoder) Encode() ([]byte, error) {
func (b ByteEncoder) Length() int {
return len(b)
}

// bufConn wraps a net.Conn with a buffer for reads to reduce the number of
// reads that trigger syscalls.
type bufConn struct {
net.Conn
buf *bufio.Reader
}

func NewBufConn(conn net.Conn) *bufConn {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

method should not be public

return &bufConn{
Conn: conn,
buf: bufio.NewReader(conn),
}
}

func (bc *bufConn) Read(b []byte) (n int, err error) {
return bc.buf.Read(b)
}