Ver Fonte

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

Reduces the number of read syscalls caused by small, sequential reads on
the broker's net.Conn.
Ben Burkert há 10 anos atrás
pai
commit
6d716464e8
2 ficheiros alterados com 24 adições e 1 exclusões
  1. 1 0
      broker.go
  2. 23 1
      utils.go

+ 1 - 0
broker.go

@@ -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)
 			Logger.Printf("Failed to connect to broker %s: %s\n", b.addr, b.connErr)
 			return
 			return
 		}
 		}
+		b.conn = NewBufConn(b.conn)
 
 
 		b.conf = conf
 		b.conf = conf
 		b.done = make(chan bool)
 		b.done = make(chan bool)

+ 23 - 1
utils.go

@@ -1,6 +1,10 @@
 package sarama
 package sarama
 
 
-import "sort"
+import (
+	"bufio"
+	"net"
+	"sort"
+)
 
 
 type none struct{}
 type none struct{}
 
 
@@ -87,3 +91,21 @@ func (b ByteEncoder) Encode() ([]byte, error) {
 func (b ByteEncoder) Length() int {
 func (b ByteEncoder) Length() int {
 	return len(b)
 	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 {
+	return &bufConn{
+		Conn: conn,
+		buf:  bufio.NewReader(conn),
+	}
+}
+
+func (bc *bufConn) Read(b []byte) (n int, err error) {
+	return bc.buf.Read(b)
+}