@@ -121,15 +121,12 @@ func (cs *ClientSet) HealthyClientsCount() int {
121
121
122
122
}
123
123
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.
129
125
func (cs * ClientSet ) HasID (serverID string ) bool {
130
126
cs .mu .Lock ()
131
127
defer cs .mu .Unlock ()
132
- return cs .hasIDLocked (serverID )
128
+ _ , exists := cs .clients [serverID ]
129
+ return exists
133
130
}
134
131
135
132
type DuplicateServerError struct {
@@ -140,20 +137,19 @@ func (dse *DuplicateServerError) Error() string {
140
137
return "duplicate server: " + dse .ServerID
141
138
}
142
139
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 {
145
148
return & DuplicateServerError {ServerID : serverID }
146
149
}
147
150
cs .clients [serverID ] = c
148
151
metrics .Metrics .SetServerConnectionsCount (len (cs .clients ))
149
152
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 )
157
153
}
158
154
159
155
func (cs * ClientSet ) RemoveClient (serverID string ) {
0 commit comments