浏览代码

Add a check to make debugging easier when the client has been closed previously (e.g., an misplaced defer() closed it).

Rob Rodgers 12 年之前
父节点
当前提交
26beb2e678
共有 2 个文件被更改,包括 22 次插入0 次删除
  1. 19 0
      client.go
  2. 3 0
      errors.go

+ 19 - 0
client.go

@@ -21,6 +21,9 @@ type Client struct {
 	id     string
 	id     string
 	config ClientConfig
 	config ClientConfig
 
 
+	// True if this client was closed, for error tracking
+	closed bool
+
 	// the broker addresses given to us through the constructor are not guaranteed to be returned in
 	// the broker addresses given to us through the constructor are not guaranteed to be returned in
 	// the cluster metadata (I *think* it only returns brokers who are currently leading partitions?)
 	// the cluster metadata (I *think* it only returns brokers who are currently leading partitions?)
 	// so we store them separately
 	// so we store them separately
@@ -87,6 +90,7 @@ func (client *Client) Close() error {
 	}
 	}
 	client.brokers = nil
 	client.brokers = nil
 	client.leaders = nil
 	client.leaders = nil
+	client.closed = true
 
 
 	if client.extraBroker != nil {
 	if client.extraBroker != nil {
 		go withRecover(func() { client.extraBroker.Close() })
 		go withRecover(func() { client.extraBroker.Close() })
@@ -226,7 +230,22 @@ func (client *Client) disconnectBroker(broker *Broker) {
 	go withRecover(func() { myBroker.Close() })
 	go withRecover(func() { myBroker.Close() })
 }
 }
 
 
+func (client *Client) CheckUsable() error {
+	if client.closed {
+		return ClosedClient
+	}
+	return nil
+}
+
+
 func (client *Client) refreshMetadata(topics []string, retries int) error {
 func (client *Client) refreshMetadata(topics []string, retries int) error {
+	// This function is a sort of central point for most functions that create new
+	// resources.  Check to see if we're dealing with a closed Client and error
+	// out immediately if so.
+	if err := client.CheckUsable(); err != nil {
+		return err
+	}
+
 	// Kafka will throw exceptions on an empty topic and not return a proper
 	// Kafka will throw exceptions on an empty topic and not return a proper
 	// error. This handles the case by returning an error instead of sending it
 	// error. This handles the case by returning an error instead of sending it
 	// off to Kafka. See: https://github.com/Shopify/sarama/pull/38#issuecomment-26362310
 	// off to Kafka. See: https://github.com/Shopify/sarama/pull/38#issuecomment-26362310

+ 3 - 0
errors.go

@@ -9,6 +9,9 @@ import (
 // or otherwise failed to respond.
 // or otherwise failed to respond.
 var OutOfBrokers = errors.New("kafka: Client has run out of available brokers to talk to. Is your cluster reachable?")
 var OutOfBrokers = errors.New("kafka: Client has run out of available brokers to talk to. Is your cluster reachable?")
 
 
+// ClosedClient is the error returned when the client was previously closed but used subsequently.
+var ClosedClient = errors.New("kafka: Use of a client that was previously closed")
+
 // NoSuchTopic is the error returned when the supplied topic is rejected by the Kafka servers.
 // NoSuchTopic is the error returned when the supplied topic is rejected by the Kafka servers.
 var NoSuchTopic = errors.New("kafka: Topic not recognized by brokers.")
 var NoSuchTopic = errors.New("kafka: Topic not recognized by brokers.")