fix: complete server lockup when a lot of clients connect

This commit is contained in:
evenhummus 2025-06-04 15:14:50 +02:00 committed by Castro Alhdo
parent c3214387e6
commit ea4fd08464
2 changed files with 21 additions and 6 deletions

View File

@ -519,6 +519,9 @@ func (c *Client) ToCalculated(allGroups []*cgroups.ClientGroup) *CalculatedClien
// If a given duration is nil - returns false (never obsolete).
func (c *Client) Obsolete(duration *time.Duration) bool {
disconnectedAt := c.GetDisconnectedAt()
if disconnectedAt == nil {
return false
}
return duration != nil && !c.IsConnected() && disconnectedAt.Add(*duration).Before(Now())
}
@ -627,7 +630,7 @@ func (c *Client) BelongsTo(group *cgroups.ClientGroup) bool {
return false
}
if !p.ConnectionState.MatchesOneOf(string(c.CalculateConnectionState())) {
if !p.ConnectionState.MatchesOneOf(string(c.calculateConnectionStateUnlocked())) {
return false
}
@ -641,6 +644,13 @@ func (c *Client) CalculateConnectionState() ConnectionState {
return Disconnected
}
func (c *Client) calculateConnectionStateUnlocked() ConnectionState {
if c.DisconnectedAt != nil {
return Connected
}
return Disconnected
}
// HasAccessViaUserGroups returns true if at least one of given user groups has access to a current client.
func (c *Client) HasAccessViaUserGroups(userGroups []string) bool {
for _, curUserGroup := range userGroups {

View File

@ -1,6 +1,7 @@
package clients
import (
"strings"
"time"
"github.com/openrport/openrport/server/clients/clientdata"
@ -58,14 +59,16 @@ func ConvertToClientsPayload(clientsList []*clientdata.CalculatedClient, fields
func ConvertToClientPayload(client *clientdata.CalculatedClient, fields []query.FieldsOption) ClientPayload { //nolint:gocyclo
requestedFields := query.RequestedFields(fields, "clients")
p := ClientPayload{}
//Source Fellows change
client.GetLock().RLock()
defer client.GetLock().RUnlock()
for field := range OptionsSupportedFields["clients"] {
if len(fields) > 0 && !requestedFields[field] {
continue
}
client.GetLock().RLock()
defer client.GetLock().RUnlock()
switch field {
case "id":
id := client.ID
@ -74,7 +77,8 @@ func ConvertToClientPayload(client *clientdata.CalculatedClient, fields []query.
name := client.Name
p.Name = &name
case "os":
p.OS = &client.OS
val := strings.Clone(client.OS)
p.OS = &val
case "os_arch":
p.OSArch = &client.OSArch
case "os_family":
@ -82,7 +86,8 @@ func ConvertToClientPayload(client *clientdata.CalculatedClient, fields []query.
case "os_kernel":
p.OSKernel = &client.OSKernel
case "hostname":
p.Hostname = &client.Hostname
val := client.Hostname
p.Hostname = &val
case "ipv4":
p.IPv4 = &client.IPv4
case "ipv6":