From eb9150647a5537f27d7adb88add2bf113601d0ee Mon Sep 17 00:00:00 2001 From: Sebastien Launay Date: Tue, 31 May 2016 17:07:16 -0700 Subject: [PATCH 1/2] Forbid empty ClientID and use 'sarama' as default --- client.go | 2 ++ config.go | 6 ++++-- config_test.go | 10 +++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/client.go b/client.go index 54a889739..ce914668c 100644 --- a/client.go +++ b/client.go @@ -67,6 +67,8 @@ type Client interface { } const ( + // Default ClientID + DefaultClientID = "sarama" // OffsetNewest stands for the log head offset, i.e. the offset that will be // assigned to the next message that will be produced to the partition. You // can send this to a client's GetOffset method to get this offset, or when diff --git a/config.go b/config.go index 74577b57a..7c81f9805 100644 --- a/config.go +++ b/config.go @@ -6,7 +6,7 @@ import ( "time" ) -var validID *regexp.Regexp = regexp.MustCompile(`\A[A-Za-z0-9._-]*\z`) +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 { @@ -258,6 +258,8 @@ func NewConfig() *Config { c.ChannelBufferSize = 256 + c.ClientID = DefaultClientID + return c } @@ -297,7 +299,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.") } diff --git a/config_test.go b/config_test.go index 243572985..08bcaa421 100644 --- a/config_test.go +++ b/config_test.go @@ -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) + } +} From 666bedf3840c9e84854c329be723664aac2d34ef Mon Sep 17 00:00:00 2001 From: Sebastien Launay Date: Wed, 1 Jun 2016 09:38:54 -0700 Subject: [PATCH 2/2] Moving constant to client.go and making it private --- client.go | 2 -- config.go | 6 ++++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index ce914668c..54a889739 100644 --- a/client.go +++ b/client.go @@ -67,8 +67,6 @@ type Client interface { } const ( - // Default ClientID - DefaultClientID = "sarama" // OffsetNewest stands for the log head offset, i.e. the offset that will be // assigned to the next message that will be produced to the partition. You // can send this to a client's GetOffset method to get this offset, or when diff --git a/config.go b/config.go index 7c81f9805..21a3c8191 100644 --- a/config.go +++ b/config.go @@ -6,6 +6,8 @@ import ( "time" ) +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. @@ -258,7 +260,7 @@ func NewConfig() *Config { c.ChannelBufferSize = 256 - c.ClientID = DefaultClientID + c.ClientID = defaultClientID return c } @@ -299,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 == DefaultClientID { + if c.ClientID == defaultClientID { Logger.Println("ClientID is the default of 'sarama', you should consider setting it to something application-specific.") }