Skip to content

Commit

Permalink
Merge pull request #700 from justinsb/simplify_add_client
Browse files Browse the repository at this point in the history
refactor: simplify ClientSet and add comments
  • Loading branch information
k8s-ci-robot authored Feb 21, 2025
2 parents 736cc70 + 01b4810 commit 7c2359e
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions pkg/agent/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,12 @@ func (cs *ClientSet) HealthyClientsCount() int {

}

func (cs *ClientSet) hasIDLocked(serverID string) bool {
_, ok := cs.clients[serverID]
return ok
}

// HasID returns true if the ClientSet has a client to the specified serverID.
func (cs *ClientSet) HasID(serverID string) bool {
cs.mu.Lock()
defer cs.mu.Unlock()
return cs.hasIDLocked(serverID)
_, exists := cs.clients[serverID]
return exists
}

type DuplicateServerError struct {
Expand All @@ -140,20 +137,19 @@ func (dse *DuplicateServerError) Error() string {
return "duplicate server: " + dse.ServerID
}

func (cs *ClientSet) addClientLocked(serverID string, c *Client) error {
if cs.hasIDLocked(serverID) {
// AddClient adds the specified client to our set of clients.
// If we already have a connection with the same serverID, we will return *DuplicateServerError.
func (cs *ClientSet) AddClient(serverID string, c *Client) error {
cs.mu.Lock()
defer cs.mu.Unlock()

_, exists := cs.clients[serverID]
if exists {
return &DuplicateServerError{ServerID: serverID}
}
cs.clients[serverID] = c
metrics.Metrics.SetServerConnectionsCount(len(cs.clients))
return nil

}

func (cs *ClientSet) AddClient(serverID string, c *Client) error {
cs.mu.Lock()
defer cs.mu.Unlock()
return cs.addClientLocked(serverID, c)
}

func (cs *ClientSet) RemoveClient(serverID string) {
Expand Down

0 comments on commit 7c2359e

Please sign in to comment.