Skip to content

Commit

Permalink
Add configuration to support broker SSL
Browse files Browse the repository at this point in the history
Possibly implements #154, if my assumptions about the implementation are
correct.
  • Loading branch information
eapache committed Dec 22, 2014
1 parent 950d619 commit e0e2a56
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion broker.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sarama

import (
"crypto/tls"
"fmt"
"io"
"net"
Expand All @@ -15,6 +16,8 @@ type BrokerConfig struct {
DialTimeout time.Duration // How long to wait for the initial connection to succeed before timing out and returning an error.
ReadTimeout time.Duration // How long to wait for a response before timing out and returning an error.
WriteTimeout time.Duration // How long to wait for a transmit to succeed before timing out and returning an error.
UseTLS bool // Whether or not to use TLS when connecting to the broker (defaults to false).
TLSConfig *tls.Config // The TLS configuration to use for secure connections if specified by UseTLS (defaults to nil).
}

// NewBrokerConfig returns a new broker configuration with sane defaults.
Expand Down Expand Up @@ -99,7 +102,15 @@ func (b *Broker) Open(conf *BrokerConfig) error {
go withRecover(func() {
defer b.lock.Unlock()

b.conn, b.connErr = net.DialTimeout("tcp", b.addr, conf.DialTimeout)
dialer := &net.Dialer{
Timeout: conf.DialTimeout,
}

if conf.UseTLS {
b.conn, b.connErr = tls.DialWithDialer(dialer, "tcp", b.addr, conf.TLSConfig)
} else {
b.conn, b.connErr = dialer.Dial("tcp", b.addr)
}
if b.connErr != nil {
Logger.Printf("Failed to connect to broker %s\n", b.addr)
Logger.Println(b.connErr)
Expand Down

0 comments on commit e0e2a56

Please sign in to comment.