From 9061d3d695da4668adbb6337ae15f66b9c61ae32 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Sun, 22 Oct 2017 00:33:05 +0300 Subject: [PATCH] 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) }