Skip to content

Commit 7c2359e

Browse files
authored
Merge pull request kubernetes-sigs#700 from justinsb/simplify_add_client
refactor: simplify ClientSet and add comments
2 parents 736cc70 + 01b4810 commit 7c2359e

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
@@ -121,15 +121,12 @@ func (cs *ClientSet) HealthyClientsCount() int {
121121

122122
}
123123

124-
func (cs *ClientSet) hasIDLocked(serverID string) bool {
125-
_, ok := cs.clients[serverID]
126-
return ok
127-
}
128-
124+
// HasID returns true if the ClientSet has a client to the specified serverID.
129125
func (cs *ClientSet) HasID(serverID string) bool {
130126
cs.mu.Lock()
131127
defer cs.mu.Unlock()
132-
return cs.hasIDLocked(serverID)
128+
_, exists := cs.clients[serverID]
129+
return exists
133130
}
134131

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

143-
func (cs *ClientSet) addClientLocked(serverID string, c *Client) error {
144-
if cs.hasIDLocked(serverID) {
140+
// AddClient adds the specified client to our set of clients.
141+
// If we already have a connection with the same serverID, we will return *DuplicateServerError.
142+
func (cs *ClientSet) AddClient(serverID string, c *Client) error {
143+
cs.mu.Lock()
144+
defer cs.mu.Unlock()
145+
146+
_, exists := cs.clients[serverID]
147+
if exists {
145148
return &DuplicateServerError{ServerID: serverID}
146149
}
147150
cs.clients[serverID] = c
148151
metrics.Metrics.SetServerConnectionsCount(len(cs.clients))
149152
return nil
150-
151-
}
152-
153-
func (cs *ClientSet) AddClient(serverID string, c *Client) error {
154-
cs.mu.Lock()
155-
defer cs.mu.Unlock()
156-
return cs.addClientLocked(serverID, c)
157153
}
158154

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

0 commit comments

Comments
 (0)