@@ -182,10 +182,10 @@ func NewBackend(conn agent.AgentService_ConnectServer) (Backend, error) {
182
182
// BackendStorage is an interface to manage the storage of the backend
183
183
// connections, i.e., get, add and remove
184
184
type BackendStorage interface {
185
- // AddBackend adds a backend.
186
- AddBackend (identifier string , idType header.IdentifierType , backend Backend )
187
- // RemoveBackend removes a backend.
188
- RemoveBackend (identifier string , idType header.IdentifierType , backend Backend )
185
+ // addBackend adds a backend.
186
+ addBackend (identifier string , idType header.IdentifierType , backend Backend )
187
+ // removeBackend removes a backend.
188
+ removeBackend (identifier string , idType header.IdentifierType , backend Backend )
189
189
// NumBackends returns the number of backends.
190
190
NumBackends () int
191
191
}
@@ -199,6 +199,10 @@ type BackendManager interface {
199
199
// pick a backend for every tunnel session and each tunnel session may
200
200
// contains multiple requests.
201
201
Backend (ctx context.Context ) (Backend , error )
202
+ // AddBackend adds a backend.
203
+ AddBackend (backend Backend )
204
+ // RemoveBackend adds a backend.
205
+ RemoveBackend (backend Backend )
202
206
BackendStorage
203
207
ReadinessManager
204
208
}
@@ -215,13 +219,28 @@ func (dbm *DefaultBackendManager) Backend(_ context.Context) (Backend, error) {
215
219
return dbm .DefaultBackendStorage .GetRandomBackend ()
216
220
}
217
221
222
+ func (dbm * DefaultBackendManager ) AddBackend (backend Backend ) {
223
+ agentID := backend .GetAgentID ()
224
+ klog .V (5 ).InfoS ("Add the agent to DefaultBackendManager" , "agentID" , agentID )
225
+ dbm .addBackend (agentID , header .UID , backend )
226
+ }
227
+
228
+ func (dbm * DefaultBackendManager ) RemoveBackend (backend Backend ) {
229
+ agentID := backend .GetAgentID ()
230
+ klog .V (5 ).InfoS ("Remove the agent from the DefaultBackendManager" , "agentID" , agentID )
231
+ dbm .removeBackend (agentID , header .UID , backend )
232
+ }
233
+
218
234
// DefaultBackendStorage is the default backend storage.
219
235
type DefaultBackendStorage struct {
220
236
mu sync.RWMutex //protects the following
221
237
// A map between agentID and its grpc connections.
222
238
// For a given agent, ProxyServer prefers backends[agentID][0] to send
223
239
// traffic, because backends[agentID][1:] are more likely to be closed
224
240
// by the agent to deduplicate connections to the same server.
241
+ //
242
+ // TODO: fix documentation. This is not always agentID, e.g. in
243
+ // the case of DestHostBackendManager.
225
244
backends map [string ][]Backend
226
245
// agentID is tracked in this slice to enable randomly picking an
227
246
// agentID in the Backend() method. There is no reliable way to
@@ -267,8 +286,8 @@ func containIDType(idTypes []header.IdentifierType, idType header.IdentifierType
267
286
return false
268
287
}
269
288
270
- // AddBackend adds a backend.
271
- func (s * DefaultBackendStorage ) AddBackend (identifier string , idType header.IdentifierType , backend Backend ) {
289
+ // addBackend adds a backend.
290
+ func (s * DefaultBackendStorage ) addBackend (identifier string , idType header.IdentifierType , backend Backend ) {
272
291
if ! containIDType (s .idTypes , idType ) {
273
292
klog .V (4 ).InfoS ("fail to add backend" , "backend" , identifier , "error" , & ErrWrongIDType {idType , s .idTypes })
274
293
return
@@ -295,8 +314,8 @@ func (s *DefaultBackendStorage) AddBackend(identifier string, idType header.Iden
295
314
}
296
315
}
297
316
298
- // RemoveBackend removes a backend.
299
- func (s * DefaultBackendStorage ) RemoveBackend (identifier string , idType header.IdentifierType , backend Backend ) {
317
+ // removeBackend removes a backend.
318
+ func (s * DefaultBackendStorage ) removeBackend (identifier string , idType header.IdentifierType , backend Backend ) {
300
319
if ! containIDType (s .idTypes , idType ) {
301
320
klog .ErrorS (& ErrWrongIDType {idType , s .idTypes }, "fail to remove backend" )
302
321
return
0 commit comments