@ -19,7 +19,6 @@ import (
"sync"
"github.com/OpenIMSDK/tools/log"
"github.com/OpenIMSDK/tools/utils"
)
type UserMap struct {
@ -71,48 +70,65 @@ func (u *UserMap) Set(key string, v *Client) {
}
func ( u * UserMap ) delete ( key string , connRemoteAddr string ) ( isDeleteUser bool ) {
// Attempt to load the clients associated with the key.
allClients , existed := u . m . Load ( key )
if existed {
if ! existed {
// Return false immediately if the key does not exist.
return false
}
// Convert allClients to a slice of *Client.
oldClients := allClients . ( [ ] * Client )
var a [ ] * Client
var rem ainingClients [ ] * Client
for _ , client := range oldClients {
// Keep clients that do not match the connRemoteAddr.
if client . ctx . GetRemoteAddr ( ) != connRemoteAddr {
a = append ( a , client )
rem ainingClients = append ( rem ainingClients , client )
}
}
if len ( a ) == 0 {
// If no clients remain after filtering, delete the key from the map.
if len ( remainingClients ) == 0 {
u . m . Delete ( key )
return true
} else {
u . m . Store ( key , a )
return false
}
// Otherwise, update the key with the remaining clients.
u . m . Store ( key , remainingClients )
return false
}
return existed
func ( u * UserMap ) deleteClients ( key string , clientsToDelete [ ] * Client ) ( isDeleteUser bool ) {
// Convert the slice of clients to delete into a map for efficient lookup.
deleteMap := make ( map [ string ] struct { } )
for _ , client := range clientsToDelete {
deleteMap [ client . ctx . GetRemoteAddr ( ) ] = struct { } { }
}
func ( u * UserMap ) deleteClients ( key string , clients [ ] * Client ) ( isDeleteUser bool ) {
m := utils . SliceToMapAny ( clients , func ( c * Client ) ( string , struct { } ) {
return c . ctx . GetRemoteAddr ( ) , struct { } { }
} )
// Load the current clients associated with the key.
allClients , existed := u . m . Load ( key )
if existed {
if ! existed {
// If the key doesn't exist, return false.
return false
}
// Filter out clients that are in the deleteMap.
oldClients := allClients . ( [ ] * Client )
var a [ ] * Client
var rem ainingClients [ ] * Client
for _ , client := range oldClients {
if _ , ok := m [ client . ctx . GetRemoteAddr ( ) ] ; ! ok {
a = append ( a , client )
if _ , shouldBeDeleted := deleteMap [ client . ctx . GetRemoteAddr ( ) ] ; ! shouldBeDeleted {
rem ainingClients = append ( rem ainingClients , client )
}
}
if len ( a ) == 0 {
// Update or delete the key based on the remaining clients.
if len ( remainingClients ) == 0 {
u . m . Delete ( key )
return true
} else {
u . m . Store ( key , a )
return false
}
}
return existed
u . m . Store ( key , remainingClients )
return false
}
func ( u * UserMap ) DeleteAll ( key string ) {