@@ -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.
129125func (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
135132type 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
159155func (cs * ClientSet ) RemoveClient (serverID string ) {
0 commit comments