Skip to content

Commit f3d72c7

Browse files
committed
bugfix: move Role constants into the public API
We return a role of an instance from `GetPoolInfo()` call. It is unexpected that Role constants are not a part of the public API. Part of #208
1 parent 7b7cd36 commit f3d72c7

File tree

4 files changed

+22
-23
lines changed

4 files changed

+22
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
1515
### Fixed
1616

1717
- Mode type description in the connection_pool subpackage (#208)
18+
- Missed Role type constants in the connection_pool subpackage (#208)
1819

1920
## [1.8.0] - 2022-08-17
2021

connection_pool/connection_pool.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -574,50 +574,48 @@ func (connPool *ConnectionPool) NewPrepared(expr string, userMode Mode) (*tarant
574574
func (connPool *ConnectionPool) getConnectionRole(conn *tarantool.Connection) (Role, error) {
575575
resp, err := conn.Call17("box.info", []interface{}{})
576576
if err != nil {
577-
return unknown, err
577+
return UnknownRole, err
578578
}
579579
if resp == nil {
580-
return unknown, ErrIncorrectResponse
580+
return UnknownRole, ErrIncorrectResponse
581581
}
582582
if len(resp.Data) < 1 {
583-
return unknown, ErrIncorrectResponse
583+
return UnknownRole, ErrIncorrectResponse
584584
}
585585

586586
instanceStatus, ok := resp.Data[0].(map[interface{}]interface{})["status"]
587587
if !ok {
588-
return unknown, ErrIncorrectResponse
588+
return UnknownRole, ErrIncorrectResponse
589589
}
590590
if instanceStatus != "running" {
591-
return unknown, ErrIncorrectStatus
591+
return UnknownRole, ErrIncorrectStatus
592592
}
593593

594594
replicaRole, ok := resp.Data[0].(map[interface{}]interface{})["ro"]
595595
if !ok {
596-
return unknown, ErrIncorrectResponse
596+
return UnknownRole, ErrIncorrectResponse
597597
}
598598

599599
switch replicaRole {
600600
case false:
601-
return master, nil
601+
return MasterRole, nil
602602
case true:
603-
return replica, nil
603+
return ReplicaRole, nil
604604
}
605605

606-
return unknown, nil
606+
return UnknownRole, nil
607607
}
608608

609609
func (connPool *ConnectionPool) getConnectionFromPool(addr string) (*tarantool.Connection, Role) {
610-
conn := connPool.rwPool.GetConnByAddr(addr)
611-
if conn != nil {
612-
return conn, master
610+
if conn := connPool.rwPool.GetConnByAddr(addr); conn != nil {
611+
return conn, MasterRole
613612
}
614613

615-
conn = connPool.roPool.GetConnByAddr(addr)
616-
if conn != nil {
617-
return conn, replica
614+
if conn := connPool.roPool.GetConnByAddr(addr); conn != nil {
615+
return conn, ReplicaRole
618616
}
619617

620-
return connPool.anyPool.GetConnByAddr(addr), unknown
618+
return connPool.anyPool.GetConnByAddr(addr), UnknownRole
621619
}
622620

623621
func (connPool *ConnectionPool) deleteConnectionFromPool(addr string) {
@@ -639,9 +637,9 @@ func (connPool *ConnectionPool) setConnectionToPool(addr string, conn *tarantool
639637
connPool.anyPool.AddConn(addr, conn)
640638

641639
switch role {
642-
case master:
640+
case MasterRole:
643641
connPool.rwPool.AddConn(addr, conn)
644-
case replica:
642+
case ReplicaRole:
645643
connPool.roPool.AddConn(addr, conn)
646644
}
647645

connection_pool/connection_pool_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ func TestNewPrepared(t *testing.T) {
12951295
stmt, err := connPool.NewPrepared("SELECT NAME0, NAME1 FROM SQL_TEST WHERE NAME0=:id AND NAME1=:name;", connection_pool.RO)
12961296
require.Nilf(t, err, "fail to prepare statement: %v", err)
12971297

1298-
if connPool.GetPoolInfo()[stmt.Conn.Addr()].ConnRole != connection_pool.RO {
1298+
if connPool.GetPoolInfo()[stmt.Conn.Addr()].ConnRole != connection_pool.ReplicaRole {
12991299
t.Errorf("wrong role for the statement's connection")
13001300
}
13011301

connection_pool/const.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ const (
2626
PreferRO // If there is one, otherwise fallback to a read only one (replica).
2727
)
2828

29+
// Role describes a role of an instance by its mode.
2930
type Role uint32
3031

31-
// master/replica role
3232
const (
33-
unknown = iota
34-
master
35-
replica
33+
UnknownRole Role = iota // A connection pool failed to discover mode of the instance.
34+
MasterRole // The instance is read-write mode.
35+
ReplicaRole // The instance is in read-only mode.
3636
)
3737

3838
type State uint32

0 commit comments

Comments
 (0)