Skip to content

Commit 01b4810

Browse files
committed
refactor: simplify ClientSet and add comments
1 parent 07d3565 commit 01b4810

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

pkg/agent/clientset.go

+11-15
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,12 @@ func (cs *ClientSet) HealthyClientsCount() int {
9696

9797
}
9898

99-
func (cs *ClientSet) hasIDLocked(serverID string) bool {
100-
_, ok := cs.clients[serverID]
101-
return ok
102-
}
103-
99+
// HasID returns true if the ClientSet has a client to the specified serverID.
104100
func (cs *ClientSet) HasID(serverID string) bool {
105101
cs.mu.Lock()
106102
defer cs.mu.Unlock()
107-
return cs.hasIDLocked(serverID)
103+
_, exists := cs.clients[serverID]
104+
return exists
108105
}
109106

110107
type DuplicateServerError struct {
@@ -115,20 +112,19 @@ func (dse *DuplicateServerError) Error() string {
115112
return "duplicate server: " + dse.ServerID
116113
}
117114

118-
func (cs *ClientSet) addClientLocked(serverID string, c *Client) error {
119-
if cs.hasIDLocked(serverID) {
115+
// AddClient adds the specified client to our set of clients.
116+
// If we already have a connection with the same serverID, we will return *DuplicateServerError.
117+
func (cs *ClientSet) AddClient(serverID string, c *Client) error {
118+
cs.mu.Lock()
119+
defer cs.mu.Unlock()
120+
121+
_, exists := cs.clients[serverID]
122+
if exists {
120123
return &DuplicateServerError{ServerID: serverID}
121124
}
122125
cs.clients[serverID] = c
123126
metrics.Metrics.SetServerConnectionsCount(len(cs.clients))
124127
return nil
125-
126-
}
127-
128-
func (cs *ClientSet) AddClient(serverID string, c *Client) error {
129-
cs.mu.Lock()
130-
defer cs.mu.Unlock()
131-
return cs.addClientLocked(serverID, c)
132128
}
133129

134130
func (cs *ClientSet) RemoveClient(serverID string) {

0 commit comments

Comments
 (0)