Skip to content

Commit

Permalink
scylla: give out random connections before pool equilibrium
Browse files Browse the repository at this point in the history
When the pool is not yet fully materialized we give the caller
a random connection to allow the session to start working.
  • Loading branch information
Henrik Johansson committed Mar 20, 2019
1 parent 0978346 commit c917adb
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions scylla.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ func isScyllaConn(conn *Conn) bool {

// scyllaConnPicker is a specialised ConnPicker that selects connections based
// on token trying to get connection to a shard containing the given token.
// A list of excess connections is maintained to allow for lazy removal of.
// excess connections. Keeping excess connections open helps reaching equilibrium
// faster since the likelihood of hitting the same shard decreases with the number
// of connections to the shard.
// A list of excess connections is maintained to allow for lazy closing of
// connections to already opened shards. Keeping excess connections open helps
// reaching equilibrium faster since the likelihood of hitting the same shard
// decreases with the number of connections to the shard.
type scyllaConnPicker struct {
conns []*Conn
excessConns []*Conn
Expand Down Expand Up @@ -149,13 +149,7 @@ func (p *scyllaConnPicker) Pick(t token) *Conn {
}

if t == nil {
idx := int(atomic.AddInt32(&p.pos, 1))
for i := 0; i < len(p.conns); i++ {
if conn := p.conns[(idx+i)%len(p.conns)]; conn != nil {
return conn
}
}
return nil
return p.randomConn()
}

mmt, ok := t.(murmur3Token)
Expand All @@ -165,7 +159,12 @@ func (p *scyllaConnPicker) Pick(t token) *Conn {
}

idx := p.shardOf(mmt)
return p.conns[idx]
if c := p.conns[idx]; c != nil {
// We have this shard's connection
// so let's give it to the caller.
return c
}
return p.randomConn()
}

func (p *scyllaConnPicker) shardOf(token murmur3Token) int {
Expand Down Expand Up @@ -230,3 +229,13 @@ func (p *scyllaConnPicker) closeExcessConns() {
}
p.excessConns = nil
}

func (p *scyllaConnPicker) randomConn() *Conn {
idx := int(atomic.AddInt32(&p.pos, 1))
for i := 0; i < len(p.conns); i++ {
if conn := p.conns[(idx+i)%len(p.conns)]; conn != nil {
return conn
}
}
return nil
}

0 comments on commit c917adb

Please sign in to comment.