浏览代码

removing the wrapper function for creating tcp connection

eric 10 年之前
父节点
当前提交
3a630028fc
共有 3 个文件被更改,包括 8 次插入19 次删除
  1. 6 1
      broker.go
  2. 1 2
      config.go
  3. 1 16
      utils.go

+ 6 - 1
broker.go

@@ -68,7 +68,12 @@ func (b *Broker) Open(conf *Config) error {
 	go withRecover(func() {
 		defer b.lock.Unlock()
 
-		b.conn, b.connErr = DialWithTiming("tcp", b.addr, conf.Net.DialTimeout, conf.Net.KeepAlive)
+		dialer := net.Dialer{
+			Timeout:   conf.Net.DialTimeout,
+			KeepAlive: conf.Net.KeepAlive,
+		}
+
+		b.conn, b.connErr = dialer.Dial("tcp", b.addr)
 		if b.connErr != nil {
 			b.conn = nil
 			atomic.StoreInt32(&b.opened, 0)

+ 1 - 2
config.go

@@ -13,8 +13,7 @@ type Config struct {
 		ReadTimeout  time.Duration // How long to wait for a response before timing out and returning an error (default 30s).
 		WriteTimeout time.Duration // How long to wait for a transmit to succeed before timing out and returning an error (default 30s).
 
-		// KeepAlive specifies the keep-alive period for an active
-		// network connection.
+		// KeepAlive specifies the keep-alive period for an active network connection.
 		// If zero, keep-alives are not enabled.
 		KeepAlive time.Duration
 	}

+ 1 - 16
utils.go

@@ -1,10 +1,6 @@
 package sarama
 
-import (
-	"net"
-	"sort"
-	"time"
-)
+import "sort"
 
 type none struct{}
 
@@ -91,14 +87,3 @@ func (b ByteEncoder) Encode() ([]byte, error) {
 func (b ByteEncoder) Length() int {
 	return len(b)
 }
-
-//DialWithTiming is exactly like net.DialTimeout from the net package, but adds support for tcp keepalives
-//  Some cloud providers, like google compute engine, kill idle connections after a few mins.  Adding the keepalive
-//  keeps the connection open.
-func DialWithTiming(network, address string, timeout time.Duration, keepalive time.Duration) (net.Conn, error) {
-	d := net.Dialer{
-		Timeout:   timeout,
-		KeepAlive: keepalive,
-	}
-	return d.Dial(network, address)
-}