From 6f1d1d2fea31eb94bab168a08c0f9296c91f8ac2 Mon Sep 17 00:00:00 2001 From: Gerson Alexander Pardo Gamez Date: Sat, 21 Oct 2017 15:44:36 -0500 Subject: [PATCH 1/3] Websocket: added GetTotalConnections, GetConnections and GetConnection Former-commit-id: fddddcdebd2a72ca082223bd29638a13ae6c0343 --- websocket/server.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/websocket/server.go b/websocket/server.go index 02a7ecdb..ed66cfc3 100644 --- a/websocket/server.go +++ b/websocket/server.go @@ -106,7 +106,7 @@ type ( config Config connections connections rooms map[string][]string // by default a connection is joined to a room which has the connection id as its name - mu sync.Mutex // for rooms + mu sync.RWMutex // for rooms onConnectionListeners []ConnectionFunc //connectionPool sync.Pool // sadly we can't make this because the websocket connection is live until is closed. handler context.Handler @@ -305,6 +305,31 @@ func (s *Server) leave(roomName string, connID string) (left bool) { return } +// GetTotalConnections returns the number of total connections +func (s *Server) GetTotalConnections() int { + s.mu.RLock() + defer s.mu.RUnlock() + return len(s.connections) +} + +// GetConnections returns all connections +func (s *Server) GetConnections() []Connection { + s.mu.Lock() + var conns []Connection + for _, conn := range s.connections { + conns = append(conns, conn.value) + } + s.mu.Unlock() + return conns +} + +// GetConnection returns single connection +func (s *Server) GetConnection(key string) Connection { + s.mu.Lock() + defer s.mu.Unlock() + return s.connections.get(key) +} + // GetConnectionsByRoom returns a list of Connection // which are joined to this room. func (s *Server) GetConnectionsByRoom(roomName string) []Connection { From 9061d3d695da4668adbb6337ae15f66b9c61ae32 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Sun, 22 Oct 2017 00:33:05 +0300 Subject: [PATCH 2/3] Some changes for the benefit of performance for https://github.com/kataras/iris/pull/795 Former-commit-id: 4b2896381c78f35daaaf85d694c15e1cbdb78ac4 --- websocket/server.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/websocket/server.go b/websocket/server.go index ed66cfc3..b927419e 100644 --- a/websocket/server.go +++ b/websocket/server.go @@ -308,25 +308,22 @@ func (s *Server) leave(roomName string, connID string) (left bool) { // GetTotalConnections returns the number of total connections func (s *Server) GetTotalConnections() int { s.mu.RLock() - defer s.mu.RUnlock() - return len(s.connections) + l:= len(s.connections) + s.mu.RUnlock() + return l } // GetConnections returns all connections func (s *Server) GetConnections() []Connection { - s.mu.Lock() - var conns []Connection - for _, conn := range s.connections { - conns = append(conns, conn.value) - } - s.mu.Unlock() + s.mu.RLock() + conns:= make([]Connection, len(s.connections)) + copy(conns, s.connections) + s.mu.RUnlock() return conns } // GetConnection returns single connection func (s *Server) GetConnection(key string) Connection { - s.mu.Lock() - defer s.mu.Unlock() return s.connections.get(key) } From 1cd9b707910039407286ccd5810fc5368301b4b4 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Sun, 22 Oct 2017 00:41:04 +0300 Subject: [PATCH 3/3] Some fixes to the previous edit Former-commit-id: da5219084b60d820ced713feff837e3964fa1fb0 --- websocket/server.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/websocket/server.go b/websocket/server.go index b927419e..3ef38450 100644 --- a/websocket/server.go +++ b/websocket/server.go @@ -106,7 +106,7 @@ type ( config Config connections connections rooms map[string][]string // by default a connection is joined to a room which has the connection id as its name - mu sync.RWMutex // for rooms + mu sync.RWMutex // for rooms onConnectionListeners []ConnectionFunc //connectionPool sync.Pool // sadly we can't make this because the websocket connection is live until is closed. handler context.Handler @@ -308,7 +308,7 @@ func (s *Server) leave(roomName string, connID string) (left bool) { // GetTotalConnections returns the number of total connections func (s *Server) GetTotalConnections() int { s.mu.RLock() - l:= len(s.connections) + l := len(s.connections) s.mu.RUnlock() return l } @@ -316,8 +316,10 @@ func (s *Server) GetTotalConnections() int { // GetConnections returns all connections func (s *Server) GetConnections() []Connection { s.mu.RLock() - conns:= make([]Connection, len(s.connections)) - copy(conns, s.connections) + conns := make([]Connection, len(s.connections), len(s.connections)) + for i, c := range s.connections { + conns[i] = c.value + } s.mu.RUnlock() return conns }