Browse Source

Forbid empty ClientID and use 'sarama' as default

Sebastien Launay 8 years ago
parent
commit
eb9150647a
3 changed files with 15 additions and 3 deletions
  1. 2 0
      client.go
  2. 4 2
      config.go
  3. 9 1
      config_test.go

+ 2 - 0
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

+ 4 - 2
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.")
 	}
 

+ 9 - 1
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)
+	}
+}