Explorar el Código

minor optimization to Client.Friends

tags/v2.2.0-rc1
Shivaram Lingamneni hace 4 años
padre
commit
ffd00e1aff
Se han modificado 1 ficheros con 15 adiciones y 12 borrados
  1. 15
    12
      irc/client.go

+ 15
- 12
irc/client.go Ver fichero

@@ -1054,29 +1054,32 @@ func (client *Client) ModeString() (str string) {
1054 1054
 }
1055 1055
 
1056 1056
 // Friends refers to clients that share a channel with this client.
1057
-func (client *Client) Friends(capabs ...caps.Capability) (result map[*Session]bool) {
1058
-	result = make(map[*Session]bool)
1057
+func (client *Client) Friends(capabs ...caps.Capability) (result map[*Session]empty) {
1058
+	result = make(map[*Session]empty)
1059 1059
 
1060 1060
 	// look at the client's own sessions
1061
-	for _, session := range client.Sessions() {
1062
-		if session.capabilities.HasAll(capabs...) {
1063
-			result[session] = true
1064
-		}
1065
-	}
1061
+	addFriendsToSet(result, client, capabs...)
1066 1062
 
1067 1063
 	for _, channel := range client.Channels() {
1068 1064
 		for _, member := range channel.Members() {
1069
-			for _, session := range member.Sessions() {
1070
-				if session.capabilities.HasAll(capabs...) {
1071
-					result[session] = true
1072
-				}
1073
-			}
1065
+			addFriendsToSet(result, member, capabs...)
1074 1066
 		}
1075 1067
 	}
1076 1068
 
1077 1069
 	return
1078 1070
 }
1079 1071
 
1072
+// helper for Friends
1073
+func addFriendsToSet(set map[*Session]empty, client *Client, capabs ...caps.Capability) {
1074
+	client.stateMutex.RLock()
1075
+	defer client.stateMutex.RUnlock()
1076
+	for _, session := range client.sessions {
1077
+		if session.capabilities.HasAll(capabs...) {
1078
+			set[session] = empty{}
1079
+		}
1080
+	}
1081
+}
1082
+
1080 1083
 func (client *Client) SetOper(oper *Oper) {
1081 1084
 	client.stateMutex.Lock()
1082 1085
 	defer client.stateMutex.Unlock()

Loading…
Cancelar
Guardar