Skip to content

Commit

Permalink
Merge pull request apache#142 from Zariel/fix-isclosed-data-race
Browse files Browse the repository at this point in the history
Fix data race when reading conn.isClosed
  • Loading branch information
Zariel committed Apr 8, 2014
2 parents bd90dfb + abb0fcd commit 6ddf075
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ type Conn struct {
auth Authenticator
addr string
version uint8
isClosed bool

closedMu sync.RWMutex
isClosed bool
}

// Connect establishes a connection to a Cassandra node.
Expand Down Expand Up @@ -110,7 +112,6 @@ func Connect(addr string, cfg ConnConfig, cluster Cluster) (*Conn, error) {
addr: conn.RemoteAddr().String(),
cluster: cluster,
compressor: cfg.Compressor,
isClosed: false,
auth: cfg.Authenticator,
}

Expand Down Expand Up @@ -449,14 +450,28 @@ func (c *Conn) executeQuery(qry *Query) *Iter {
}

func (c *Conn) Pick(qry *Query) *Conn {
if c.isClosed || len(c.uniq) == 0 {
if c.Closed() || len(c.uniq) == 0 {
return nil
}
return c
}

func (c *Conn) Closed() bool {
c.closedMu.RLock()
closed := c.isClosed
c.closedMu.RUnlock()
return closed
}

func (c *Conn) Close() {
c.closedMu.Lock()
if c.isClosed {
c.closedMu.Unlock()
return
}
c.isClosed = true
c.closedMu.Unlock()

c.conn.Close()
}

Expand Down

0 comments on commit 6ddf075

Please sign in to comment.