Sfoglia il codice sorgente

Added closeChan to clean up ring describer when session is closed

Zach Badgett 10 anni fa
parent
commit
937f33fea6
2 ha cambiato i file con 13 aggiunte e 2 eliminazioni
  1. 5 0
      host_source.go
  2. 8 2
      session.go

+ 5 - 0
host_source.go

@@ -21,6 +21,7 @@ type ringDescriber struct {
 	prevHosts       []HostInfo
 	prevPartitioner string
 	session         *Session
+	closeChan       chan bool
 }
 
 func (r *ringDescriber) GetHosts() (
@@ -96,6 +97,10 @@ func (h *ringDescriber) run(sleep time.Duration) {
 	}
 
 	for {
+		select {
+		case <-h.closeChan:
+			return
+		}
 		// if we have 0 hosts this will return the previous list of hosts to
 		// attempt to reconnect to the cluster otherwise we would never find
 		// downed hosts again, could possibly have an optimisation to only

+ 8 - 2
session.go

@@ -35,6 +35,7 @@ type Session struct {
 	routingKeyInfoCache routingKeyInfoLRU
 	schemaDescriber     *schemaDescriber
 	trace               Tracer
+	hostSource          *ringDescriber
 	mu                  sync.RWMutex
 
 	cfg ClusterConfig
@@ -84,13 +85,14 @@ func NewSession(cfg ClusterConfig) (*Session, error) {
 		s.SetPageSize(cfg.PageSize)
 
 		if cfg.DiscoverHosts {
-			hostSource := &ringDescriber{
+			s.hostSource = &ringDescriber{
 				session:    s,
 				dcFilter:   cfg.Discovery.DcFilter,
 				rackFilter: cfg.Discovery.RackFilter,
+				closeChan:  make(chan bool),
 			}
 
-			go hostSource.run(cfg.Discovery.Sleep)
+			go s.hostSource.run(cfg.Discovery.Sleep)
 		}
 
 		return s, nil
@@ -184,6 +186,10 @@ func (s *Session) Close() {
 	s.isClosed = true
 
 	s.Pool.Close()
+
+	if s.hostSource != nil {
+		close(s.hostSource.closeChan)
+	}
 }
 
 func (s *Session) Closed() bool {