Browse Source

client: close backgroundMetadataUpdater faster

Rather than waiting for the next timer cycle (defaults to ten minutes) have it
read from two channels in a select so it tears down right away. It was polluting
test backtraces (where we create and destroy a lot of clients) and making
debugging more difficult than it needed to be.
Evan Huus 11 years ago
parent
commit
e594cf0ae1
1 changed files with 11 additions and 6 deletions
  1. 11 6
      client.go

+ 11 - 6
client.go

@@ -21,6 +21,7 @@ type ClientConfig struct {
 type Client struct {
 	id     string
 	config ClientConfig
+	closer chan struct{}
 
 	// 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?)
@@ -55,6 +56,7 @@ func NewClient(id string, addrs []string, config *ClientConfig) (*Client, error)
 	client := &Client{
 		id:              id,
 		config:          *config,
+		closer:          make(chan struct{}),
 		seedBrokerAddrs: addrs,
 		seedBroker:      NewBroker(addrs[0]),
 		deadBrokerAddrs: make(map[string]struct{}),
@@ -108,6 +110,8 @@ func (client *Client) Close() error {
 		safeAsyncClose(client.seedBroker)
 	}
 
+	close(client.closer)
+
 	return nil
 }
 
@@ -383,15 +387,16 @@ func (client *Client) backgroundMetadataUpdater() {
 	}
 
 	ticker := time.NewTicker(client.config.BackgroundRefreshFrequency)
-	for _ = range ticker.C {
-		if client.Closed() {
+	for {
+		select {
+		case <-ticker.C:
+			if err := client.RefreshAllMetadata(); err != nil {
+				Logger.Println("Client background metadata update:", err)
+			}
+		case <-client.closer:
 			ticker.Stop()
 			return
 		}
-		err := client.RefreshAllMetadata()
-		if err != nil {
-			Logger.Print("Client background metadata update: ", err)
-		}
 	}
 }