Skip to content

Commit

Permalink
Cleaned up code to streamline the connection being added to the pool …
Browse files Browse the repository at this point in the history
…and removed code related to old keyspace behaviour.
  • Loading branch information
Phillip Couto committed Apr 8, 2014
1 parent 3a8fd4c commit 5cf7b6e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 43 deletions.
51 changes: 10 additions & 41 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,35 +138,26 @@ func (c *clusterImpl) connect(addr string) {
return
}
}
c.addConn(conn, "")
c.addConn(conn)
return
}
}

func (c *clusterImpl) changeKeyspace(conn *Conn, keyspace string, connected bool) {
if err := conn.UseKeyspace(keyspace); err != nil {
conn.Close()
if connected {
c.removeConn(conn)
}
go c.connect(conn.Address())
}
if !connected {
c.addConn(conn, keyspace)
}
}

func (c *clusterImpl) addConn(conn *Conn, keyspace string) {
func (c *clusterImpl) addConn(conn *Conn) {
c.mu.Lock()
defer c.mu.Unlock()
if c.quit {
conn.Close()
return
}
if keyspace != c.keyspace && c.keyspace != "" {
// change the keyspace before adding the node to the pool
go c.changeKeyspace(conn, c.keyspace, false)
return
//Set the connection's keyspace if any before adding it to the pool
if c.keyspace != "" {
if err := conn.UseKeyspace(c.keyspace); err != nil {
log.Printf("error setting connection keyspace. %v", err)
conn.Close()
go c.connect(conn.Address())
return
}
}
connPool := c.connPool[conn.Address()]
if connPool == nil {
Expand Down Expand Up @@ -214,28 +205,6 @@ func (c *clusterImpl) HandleError(conn *Conn, err error, closed bool) {
}
}

func (c *clusterImpl) HandleKeyspace(conn *Conn, keyspace string) {
c.mu.Lock()
if c.keyspace == keyspace {
c.mu.Unlock()
return
}
c.keyspace = keyspace
conns := make([]*Conn, 0, len(c.conns))
for conn := range c.conns {
conns = append(conns, conn)
}
c.mu.Unlock()

// change the keyspace of all other connections too
for i := 0; i < len(conns); i++ {
if conns[i] == conn {
continue
}
c.changeKeyspace(conns[i], keyspace, true)
}
}

func (c *clusterImpl) Pick(qry *Query) *Conn {
return c.hostPool.Pick(qry)
}
Expand Down
2 changes: 0 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const maskVersion = 0x7F

type Cluster interface {
HandleError(conn *Conn, err error, closed bool)
HandleKeyspace(conn *Conn, keyspace string)
}

type Authenticator interface {
Expand Down Expand Up @@ -426,7 +425,6 @@ func (c *Conn) executeQuery(qry *Query) *Iter {
}
return iter
case resultKeyspaceFrame:
c.cluster.HandleKeyspace(c, x.Keyspace)
return &Iter{}
case errorFrame:
if x.Code == errUnprepared && len(qry.values) > 0 {
Expand Down

0 comments on commit 5cf7b6e

Please sign in to comment.