From a85d6b869f8002237686eaa4f9aa5a0d441d2591 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 15 Jun 2013 11:35:20 -0400 Subject: [PATCH] storing connections in a map instead of slice now --- cm/manager.go | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/cm/manager.go b/cm/manager.go index 156fbaa..02acf54 100644 --- a/cm/manager.go +++ b/cm/manager.go @@ -5,14 +5,14 @@ import ( ) type Manager struct { - active []net.Conn + active map[net.Conn]bool connect chan net.Conn disconnect chan net.Conn } func New() *Manager { m := &Manager{ - active: make([]net.Conn, 0, 10), + active: make(map[net.Conn]bool, 10), connect: make(chan net.Conn), disconnect: make(chan net.Conn), } @@ -24,9 +24,9 @@ func (m *Manager) run() { for { select { case conn := <-m.connect: - m.active = append(m.active, conn) + m.active[conn] = true case conn := <-m.disconnect: - m.removeConnection(conn) + delete(m.active, conn) } } } @@ -38,12 +38,3 @@ func (m *Manager) Add(conn net.Conn) { func (m *Manager) Remove(conn net.Conn) { m.disconnect <- conn } - -func (m *Manager) removeConnection(conn net.Conn) { - for i, other := range m.active { - if conn.RemoteAddr() == other.RemoteAddr() { - m.active = append(m.active[:i], m.active[i+1:]...) - return - } - } -}