-
Notifications
You must be signed in to change notification settings - Fork 623
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hostConnPool: introduced ConnPicker interface to abstract pool storage
The hostConnPool logic around conns slice is moved to defaultConnPicker. Co-authored-by: Henrik Johansson <[email protected]> Co-authored-by: Michał Matczuk <[email protected]>
- Loading branch information
Showing
4 changed files
with
152 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package gocql | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
) | ||
|
||
type ConnPicker interface { | ||
Pick(token) *Conn | ||
Put(*Conn) | ||
Remove(conn *Conn) | ||
Size() (int, int) | ||
Close() | ||
} | ||
|
||
type defaultConnPicker struct { | ||
conns []*Conn | ||
pos uint32 | ||
size int | ||
mu sync.RWMutex | ||
} | ||
|
||
func newDefaultConnPicker(size int) *defaultConnPicker { | ||
return &defaultConnPicker{ | ||
size: size, | ||
} | ||
} | ||
|
||
func (p *defaultConnPicker) Remove(conn *Conn) { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
|
||
for i, candidate := range p.conns { | ||
if candidate == conn { | ||
p.conns[i] = nil | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (p *defaultConnPicker) Close() { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
|
||
conns := p.conns | ||
p.conns = nil | ||
for _, conn := range conns { | ||
if conn != nil { | ||
conn.Close() | ||
} | ||
} | ||
} | ||
|
||
func (p *defaultConnPicker) Size() (int, int) { | ||
size := len(p.conns) | ||
return size, p.size - size | ||
} | ||
|
||
func (p *defaultConnPicker) Pick(token) *Conn { | ||
pos := int(atomic.AddUint32(&p.pos, 1) - 1) | ||
size := len(p.conns) | ||
|
||
var ( | ||
leastBusyConn *Conn | ||
streamsAvailable int | ||
) | ||
|
||
// find the conn which has the most available streams, this is racy | ||
for i := 0; i < size; i++ { | ||
conn := p.conns[(pos+i)%size] | ||
if conn == nil { | ||
continue | ||
} | ||
if streams := conn.AvailableStreams(); streams > streamsAvailable { | ||
leastBusyConn = conn | ||
streamsAvailable = streams | ||
} | ||
} | ||
|
||
return leastBusyConn | ||
} | ||
|
||
func (p *defaultConnPicker) Put(conn *Conn) { | ||
p.mu.Lock() | ||
p.conns = append(p.conns, conn) | ||
p.mu.Unlock() | ||
} | ||
|
||
// nopConnPicker is a no-operation implementation of ConnPicker. | ||
type nopConnPicker struct{} | ||
|
||
func (nopConnPicker) Pick(token) *Conn { | ||
return nil | ||
} | ||
|
||
func (nopConnPicker) Put(*Conn) { | ||
} | ||
|
||
func (nopConnPicker) Remove(conn *Conn) { | ||
} | ||
|
||
func (nopConnPicker) Size() (int, int) { | ||
return 0, 1 // 1 makes hostConnPool want to fill the pool | ||
} | ||
|
||
func (nopConnPicker) Close() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters