فهرست منبع

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
 	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 cluster metadata (I *think* it only returns brokers who are currently leading partitions?)
 	// so we store them separately
@@ -87,6 +90,7 @@ func (client *Client) Close() error {
 	}
 	client.brokers = nil
 	client.leaders = nil
+	client.closed = true
 
 	if client.extraBroker != nil {
 		go withRecover(func() { client.extraBroker.Close() })
@@ -226,7 +230,22 @@ func (client *Client) disconnectBroker(broker *Broker) {
 	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 {
+	// 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
 	// 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

+ 3 - 0
errors.go

@@ -9,6 +9,9 @@ import (
 // or otherwise failed to respond.
 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.
 var NoSuchTopic = errors.New("kafka: Topic not recognized by brokers.")