Skip to content

Commit 8332184

Browse files
nikagradkropachev
andcommitted
refactor: update ConnPicker interface to return error from Put method
Co-authored-by: Dmitry Kropachev <[email protected]>
1 parent 426c16f commit 8332184

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

connectionpool.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,9 @@ func (pool *hostConnPool) connect() (err error) {
544544

545545
// lazily initialize the connPicker when we know the required type
546546
pool.initConnPicker(conn)
547-
pool.connPicker.Put(conn)
547+
if err := pool.connPicker.Put(conn); err != nil {
548+
return err
549+
}
548550
conn.finalizeConnection()
549551

550552
return nil

connpicker.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
type ConnPicker interface {
1010
Pick(Token, ExecutableQuery) *Conn
11-
Put(*Conn)
11+
Put(*Conn) error
1212
Remove(conn *Conn)
1313
InFlight() int
1414
Size() (int, int)
@@ -96,10 +96,11 @@ func (p *defaultConnPicker) Pick(Token, ExecutableQuery) *Conn {
9696
return leastBusyConn
9797
}
9898

99-
func (p *defaultConnPicker) Put(conn *Conn) {
99+
func (p *defaultConnPicker) Put(conn *Conn) error {
100100
p.mu.Lock()
101101
p.conns = append(p.conns, conn)
102102
p.mu.Unlock()
103+
return nil
103104
}
104105

105106
func (*defaultConnPicker) NextShard() (shardID, nrShards int) {
@@ -115,7 +116,8 @@ func (nopConnPicker) Pick(Token, ExecutableQuery) *Conn {
115116
return nil
116117
}
117118

118-
func (nopConnPicker) Put(*Conn) {
119+
func (nopConnPicker) Put(*Conn) error {
120+
return nil
119121
}
120122

121123
func (nopConnPicker) Remove(conn *Conn) {

integration_only.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (s *Session) MissingConnections() (int, error) {
4545

4646
type ConnPickerIntegration interface {
4747
Pick(Token, ExecutableQuery) *Conn
48-
Put(*Conn)
48+
Put(*Conn) error
4949
Remove(conn *Conn)
5050
InFlight() int
5151
Size() (int, int)

scylla.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,19 +452,19 @@ func (p *scyllaConnPicker) shardOf(token int64Token) int {
452452
return int(sum >> 32)
453453
}
454454

455-
func (p *scyllaConnPicker) Put(conn *Conn) {
455+
func (p *scyllaConnPicker) Put(conn *Conn) error {
456456
var (
457457
nrShards = conn.scyllaSupported.nrShards
458458
shard = conn.scyllaSupported.shard
459459
)
460460

461461
if nrShards == 0 {
462-
panic(fmt.Sprintf("scylla: %s not a sharded connection", p.address))
462+
return errors.New("server reported that it has no shards")
463463
}
464464

465465
if nrShards != len(p.conns) {
466466
if nrShards != p.nrShards {
467-
panic(fmt.Sprintf("scylla: %s invalid number of shards", p.address))
467+
return fmt.Errorf("server %s reported that number of shard changed from %d to %d", p.address, p.nrShards, nrShards)
468468
}
469469
conns := p.conns
470470
p.conns = make([]*Conn, nrShards, nrShards)
@@ -509,6 +509,8 @@ func (p *scyllaConnPicker) Put(conn *Conn) {
509509
if p.shouldCloseExcessConns() {
510510
p.closeExcessConns()
511511
}
512+
513+
return nil
512514
}
513515

514516
func (p *scyllaConnPicker) shouldCloseExcessConns() bool {

0 commit comments

Comments
 (0)