Skip to content

Commit af83033

Browse files
committed
feat: remove connection from connection manager
1 parent f701571 commit af83033

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

connection_manager.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (c ConnectionManagerTag) String() string {
5555
type ConnectionManager struct {
5656
config ConnectionManagerConfig
5757
hosts []ConnectionManagerHost
58-
connections []*ConnectionManagerConnection
58+
connections map[int]*ConnectionManagerConnection
5959
}
6060

6161
type ConnectionManagerConfig struct {
@@ -70,7 +70,8 @@ type ConnectionManagerHost struct {
7070

7171
func NewConnectionManager(cfg ConnectionManagerConfig) *ConnectionManager {
7272
return &ConnectionManager{
73-
config: cfg,
73+
config: cfg,
74+
connections: make(map[int]*ConnectionManagerConnection),
7475
}
7576
}
7677

@@ -106,13 +107,10 @@ func (c *ConnectionManager) AddHostsFromTopology(topology *TopologyConfig) {
106107
}
107108

108109
func (c *ConnectionManager) AddConnection(connId int, conn *Connection) {
109-
c.connections = append(
110-
c.connections,
111-
&ConnectionManagerConnection{
112-
Id: connId,
113-
Conn: conn,
114-
},
115-
)
110+
c.connections[connId] = &ConnectionManagerConnection{
111+
Id: connId,
112+
Conn: conn,
113+
}
116114
go func() {
117115
err, ok := <-conn.ErrorChan()
118116
if !ok {
@@ -124,13 +122,12 @@ func (c *ConnectionManager) AddConnection(connId int, conn *Connection) {
124122
}()
125123
}
126124

127-
func (c *ConnectionManager) GetConnectionById(id int) *ConnectionManagerConnection {
128-
for _, conn := range c.connections {
129-
if conn.Id == id {
130-
return conn
131-
}
132-
}
133-
return nil
125+
func (c *ConnectionManager) RemoveConnection(connId int) {
126+
delete(c.connections, connId)
127+
}
128+
129+
func (c *ConnectionManager) GetConnectionById(connId int) *ConnectionManagerConnection {
130+
return c.connections[connId]
134131
}
135132

136133
func (c *ConnectionManager) GetConnectionsByTags(tags ...ConnectionManagerTag) []*ConnectionManagerConnection {

0 commit comments

Comments
 (0)