diff --git a/broker.go b/broker.go index cd629b72b..fd5979391 100644 --- a/broker.go +++ b/broker.go @@ -1,6 +1,7 @@ package sarama import ( + "crypto/tls" "fmt" "io" "net" @@ -68,7 +69,15 @@ func (b *Broker) Open(conf *Config) error { go withRecover(func() { defer b.lock.Unlock() - b.conn, b.connErr = net.DialTimeout("tcp", b.addr, conf.Net.DialTimeout) + dialer := &net.Dialer{ + Timeout: conf.Net.DialTimeout, + } + + if conf.Net.TLS.Enable { + b.conn, b.connErr = tls.DialWithDialer(dialer, "tcp", b.addr, conf.Net.TLS.Config) + } else { + b.conn, b.connErr = dialer.Dial("tcp", b.addr) + } if b.connErr != nil { b.conn = nil atomic.StoreInt32(&b.opened, 0) diff --git a/config.go b/config.go index 0be23e0ef..21cd0d44f 100644 --- a/config.go +++ b/config.go @@ -1,6 +1,9 @@ package sarama -import "time" +import ( + "crypto/tls" + "time" +) // Config is used to pass multiple configuration options to Sarama's constructors. type Config struct { @@ -12,6 +15,11 @@ type Config struct { DialTimeout time.Duration // How long to wait for the initial connection to succeed before timing out and returning an error (default 30s). 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). + + TLS struct { + Enable bool // Whether or not to use TLS when connecting to the broker (defaults to false). + Config *tls.Config // The TLS configuration to use for secure connections if specified by UseTLS (defaults to nil). + } } // Metadata is the namespace for metadata management properties used by the Client, and shared by the Producer/Consumer.