@@ -90,37 +90,11 @@ type Client struct {
9090 importLogEncoder encoder
9191 logLock sync.Mutex
9292
93- // TODO shardNodes needs to be invalidated/updated when cluster topology changes.
9493 shardNodes shardNodes
9594 tick * time.Ticker
95+ done chan struct {}
9696}
9797
98- // func (c *Client) translateCol(index, key string) (uint64, bool) {
99- // c.tlock.RLock()
100- // v, b := c.translator.GetCol(index, key)
101- // c.tlock.RUnlock()
102- // return v, b
103- // }
104-
105- // func (c *Client) translateRow(index, field, key string) (uint64, bool) {
106- // c.tlock.RLock()
107- // v, b := c.translator.GetRow(index, field, key)
108- // c.tlock.RUnlock()
109- // return v, b
110- // }
111-
112- // func (c *Client) addTranslateCol(index, key string, value uint64) {
113- // c.tlock.Lock()
114- // c.translator.AddCol(index, key, value)
115- // c.tlock.Unlock()
116- // }
117-
118- // func (c *Client) addTranslateRow(index, field, key string, value uint64) {
119- // c.tlock.Lock()
120- // c.translator.AddRow(index, field, key, value)
121- // c.tlock.Unlock()
122- // }
123-
12498func (c * Client ) getURIsForShard (index string , shard uint64 ) ([]* URI , error ) {
12599 uris , ok := c .shardNodes .Get (index , shard )
126100 if ok {
@@ -139,13 +113,22 @@ func (c *Client) getURIsForShard(index string, shard uint64) ([]*URI, error) {
139113}
140114
141115func (c * Client ) runChangeDetection () {
142- c .tick = time .NewTicker (time .Minute )
143-
144- for range c .tick .C {
145- c .detectClusterChanges ()
116+ for {
117+ select {
118+ case <- c .tick .C :
119+ c .detectClusterChanges ()
120+ case <- c .done :
121+ return
122+ }
146123 }
147124}
148125
126+ func (c * Client ) Close () error {
127+ c .tick .Stop ()
128+ close (c .done )
129+ return nil
130+ }
131+
149132// detectClusterChanges chooses a random index and shard from the
150133// shardNodes cache and deletes it. It then looks it up from Pilosa to
151134// see if it still matches, and if not it drops the whole cache.
@@ -232,6 +215,8 @@ func newClientWithOptions(options *ClientOptions) *Client {
232215 coordinatorLock : & sync.RWMutex {},
233216
234217 shardNodes : newShardNodes (),
218+ tick : time .NewTicker (time .Minute ),
219+ done : make (chan struct {}, 0 ),
235220 }
236221 if options .importLogWriter != nil {
237222 c .importLogEncoder = newImportLogEncoder (options .importLogWriter )
@@ -245,6 +230,7 @@ func newClientWithOptions(options *ClientOptions) *Client {
245230 c .minRetrySleepTime = 1 * time .Second
246231 c .maxRetrySleepTime = 2 * time .Minute
247232 c .importManager = newRecordImportManager (c )
233+ go c .runChangeDetection ()
248234 return c
249235
250236}
0 commit comments