Skip to content

Commit

Permalink
Merge pull request #664 from slaunay/bugfix/empty-client-id
Browse files Browse the repository at this point in the history
Forbid empty ClientID and use 'sarama' as default
  • Loading branch information
eapache committed Jun 1, 2016
2 parents 97511b2 + 666bedf commit ee044df
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 6 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"time"
)

var validID *regexp.Regexp = regexp.MustCompile(`\A[A-Za-z0-9._-]*\z`)
const defaultClientID = "sarama"

var validID *regexp.Regexp = regexp.MustCompile(`\A[A-Za-z0-9._-]+\z`)

// Config is used to pass multiple configuration options to Sarama's constructors.
type Config struct {
Expand Down Expand Up @@ -258,6 +260,8 @@ func NewConfig() *Config {

c.ChannelBufferSize = 256

c.ClientID = defaultClientID

return c
}

Expand Down Expand Up @@ -297,7 +301,7 @@ func (c *Config) Validate() error {
if c.Consumer.Offsets.Retention%time.Millisecond != 0 {
Logger.Println("Consumer.Offsets.Retention only supports millisecond precision; nanoseconds will be truncated.")
}
if c.ClientID == "sarama" {
if c.ClientID == defaultClientID {
Logger.Println("ClientID is the default of 'sarama', you should consider setting it to something application-specific.")
}

Expand Down
10 changes: 9 additions & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ func TestDefaultConfigValidates(t *testing.T) {
}
}

func TestClientIDValidates(t *testing.T) {
func TestInvalidClientIDConfigValidates(t *testing.T) {
config := NewConfig()
config.ClientID = "foo:bar"
if err := config.Validate(); string(err.(ConfigurationError)) != "ClientID is invalid" {
t.Error("Expected invalid ClientID, got ", err)
}
}

func TestEmptyClientIDConfigValidates(t *testing.T) {
config := NewConfig()
config.ClientID = ""
if err := config.Validate(); string(err.(ConfigurationError)) != "ClientID is invalid" {
t.Error("Expected invalid ClientID, got ", err)
}
}

0 comments on commit ee044df

Please sign in to comment.