浏览代码

Do the same for ClientConfig and ProducerConfig

Willem van Bergen 11 年之前
父节点
当前提交
28b289a868
共有 3 个文件被更改,包括 52 次插入29 次删除
  1. 21 10
      client.go
  2. 1 1
      consumer.go
  3. 30 18
      producer.go

+ 21 - 10
client.go

@@ -43,16 +43,8 @@ func NewClient(id string, addrs []string, config *ClientConfig) (*Client, error)
 		config = new(ClientConfig)
 	}
 
-	if config.MetadataRetries <= 0 {
-		return nil, ConfigurationError("Invalid MetadataRetries. Try 10")
-	}
-
-	if config.WaitForElection <= time.Duration(0) {
-		return nil, ConfigurationError("Invalid WaitForElection. Try 250*time.Millisecond")
-	}
-
-	if config.ConcurrencyPerBroker < 0 {
-		return nil, ConfigurationError("Invalid ConcurrencyPerBroker")
+	if err := config.Validate(); err != nil {
+		return nil, err
 	}
 
 	if len(addrs) < 1 {
@@ -374,3 +366,22 @@ func (client *Client) update(data *MetadataResponse) ([]string, error) {
 	}
 	return ret, nil
 }
+
+// Validates a ClientConfig instance. This will change zero
+// values into sensible defaults if possible, and it will return a
+// ConfigurationError if the specified value doesn't make sense and
+// cannot be corrected.
+func (config *ClientConfig) Validate() error {
+	if config.MetadataRetries <= 0 {
+		return ConfigurationError("Invalid MetadataRetries. Try 10")
+	}
+
+	if config.WaitForElection <= time.Duration(0) {
+		return ConfigurationError("Invalid WaitForElection. Try 250*time.Millisecond")
+	}
+
+	if config.ConcurrencyPerBroker < 0 {
+		return ConfigurationError("Invalid ConcurrencyPerBroker")
+	}
+	return nil
+}

+ 1 - 1
consumer.go

@@ -320,7 +320,7 @@ func (c *Consumer) getOffset(where OffsetTime, retry bool) (int64, error) {
 	return -1, block.Err
 }
 
-// Valides a ConsumerConfig instance. This will change zero
+// Validates a ConsumerConfig instance. This will change zero
 // values into sensible defaults if possible, and it will return a
 // ConfigurationError if the specified value doesn't make sense and
 // cannot be corrected.

+ 30 - 18
producer.go

@@ -73,24 +73,8 @@ func NewProducer(client *Client, config *ProducerConfig) (*Producer, error) {
 		config = new(ProducerConfig)
 	}
 
-	if config.RequiredAcks < -1 {
-		return nil, ConfigurationError("Invalid RequiredAcks")
-	}
-
-	if config.Timeout < 0 {
-		return nil, ConfigurationError("Invalid Timeout")
-	}
-
-	if config.Partitioner == nil {
-		config.Partitioner = NewRandomPartitioner()
-	}
-
-	if config.MaxBufferedBytes == 0 {
-		return nil, ConfigurationError("Invalid MaxBufferedBytes")
-	}
-
-	if config.MaxBufferTime == 0 {
-		return nil, ConfigurationError("Invalid MaxBufferTime")
+	if err := config.Validate(); err != nil {
+		return nil, err
 	}
 
 	return &Producer{
@@ -463,3 +447,31 @@ func (p *Producer) choosePartition(topic string, key Encoder) (int32, error) {
 
 	return partitions[choice], nil
 }
+
+// Validates a ProducerConfig instance. This will change zero
+// values into sensible defaults if possible, and it will return a
+// ConfigurationError if the specified value doesn't make sense and
+// cannot be corrected.
+func (config *ProducerConfig) Validate() error {
+	if config.RequiredAcks < -1 {
+		return ConfigurationError("Invalid RequiredAcks")
+	}
+
+	if config.Timeout < 0 {
+		return ConfigurationError("Invalid Timeout")
+	}
+
+	if config.Partitioner == nil {
+		config.Partitioner = NewRandomPartitioner()
+	}
+
+	if config.MaxBufferedBytes == 0 {
+		return ConfigurationError("Invalid MaxBufferedBytes")
+	}
+
+	if config.MaxBufferTime == 0 {
+		return ConfigurationError("Invalid MaxBufferTime")
+	}
+
+	return nil
+}