@@ -77,17 +77,7 @@ func GenProxyStrategiesFromStr(proxyStrategies string) ([]ProxyStrategy, error)
77
77
// In the only currently supported case (gRPC), it wraps an
78
78
// agent.AgentService_ConnectServer, provides synchronization and
79
79
// emits common stream metrics.
80
- type Backend interface {
81
- Send (p * client.Packet ) error
82
- Recv () (* client.Packet , error )
83
- Context () context.Context
84
- GetAgentID () string
85
- GetAgentIdentifiers () header.Identifiers
86
- }
87
-
88
- var _ Backend = & backend {}
89
-
90
- type backend struct {
80
+ type Backend struct {
91
81
sendLock sync.Mutex
92
82
recvLock sync.Mutex
93
83
conn agent.AgentService_ConnectServer
@@ -97,7 +87,7 @@ type backend struct {
97
87
idents header.Identifiers
98
88
}
99
89
100
- func (b * backend ) Send (p * client.Packet ) error {
90
+ func (b * Backend ) Send (p * client.Packet ) error {
101
91
b .sendLock .Lock ()
102
92
defer b .sendLock .Unlock ()
103
93
@@ -110,7 +100,7 @@ func (b *backend) Send(p *client.Packet) error {
110
100
return err
111
101
}
112
102
113
- func (b * backend ) Recv () (* client.Packet , error ) {
103
+ func (b * Backend ) Recv () (* client.Packet , error ) {
114
104
b .recvLock .Lock ()
115
105
defer b .recvLock .Unlock ()
116
106
@@ -126,16 +116,16 @@ func (b *backend) Recv() (*client.Packet, error) {
126
116
return pkt , nil
127
117
}
128
118
129
- func (b * backend ) Context () context.Context {
119
+ func (b * Backend ) Context () context.Context {
130
120
// TODO: does Context require lock protection?
131
121
return b .conn .Context ()
132
122
}
133
123
134
- func (b * backend ) GetAgentID () string {
124
+ func (b * Backend ) GetAgentID () string {
135
125
return b .id
136
126
}
137
127
138
- func (b * backend ) GetAgentIdentifiers () header.Identifiers {
128
+ func (b * Backend ) GetAgentIdentifiers () header.Identifiers {
139
129
return b .idents
140
130
}
141
131
@@ -168,7 +158,7 @@ func getAgentIdentifiers(conn agent.AgentService_ConnectServer) (header.Identifi
168
158
return header .GenAgentIdentifiers (agentIdent [0 ])
169
159
}
170
160
171
- func NewBackend (conn agent.AgentService_ConnectServer ) (Backend , error ) {
161
+ func NewBackend (conn agent.AgentService_ConnectServer ) (* Backend , error ) {
172
162
agentID , err := getAgentID (conn )
173
163
if err != nil {
174
164
return nil , err
@@ -177,16 +167,16 @@ func NewBackend(conn agent.AgentService_ConnectServer) (Backend, error) {
177
167
if err != nil {
178
168
return nil , err
179
169
}
180
- return & backend {conn : conn , id : agentID , idents : agentIdentifiers }, nil
170
+ return & Backend {conn : conn , id : agentID , idents : agentIdentifiers }, nil
181
171
}
182
172
183
173
// BackendStorage is an interface to manage the storage of the backend
184
174
// connections, i.e., get, add and remove
185
175
type BackendStorage interface {
186
176
// addBackend adds a backend.
187
- addBackend (identifier string , idType header.IdentifierType , backend Backend )
177
+ addBackend (identifier string , idType header.IdentifierType , backend * Backend )
188
178
// removeBackend removes a backend.
189
- removeBackend (identifier string , idType header.IdentifierType , backend Backend )
179
+ removeBackend (identifier string , idType header.IdentifierType , backend * Backend )
190
180
// NumBackends returns the number of backends.
191
181
NumBackends () int
192
182
}
@@ -199,11 +189,11 @@ type BackendManager interface {
199
189
// context instead of a request-scoped context, as the backend manager will
200
190
// pick a backend for every tunnel session and each tunnel session may
201
191
// contains multiple requests.
202
- Backend (ctx context.Context ) (Backend , error )
192
+ Backend (ctx context.Context ) (* Backend , error )
203
193
// AddBackend adds a backend.
204
- AddBackend (backend Backend )
194
+ AddBackend (backend * Backend )
205
195
// RemoveBackend adds a backend.
206
- RemoveBackend (backend Backend )
196
+ RemoveBackend (backend * Backend )
207
197
BackendStorage
208
198
ReadinessManager
209
199
}
@@ -215,18 +205,18 @@ type DefaultBackendManager struct {
215
205
* DefaultBackendStorage
216
206
}
217
207
218
- func (dbm * DefaultBackendManager ) Backend (_ context.Context ) (Backend , error ) {
208
+ func (dbm * DefaultBackendManager ) Backend (_ context.Context ) (* Backend , error ) {
219
209
klog .V (5 ).InfoS ("Get a random backend through the DefaultBackendManager" )
220
210
return dbm .DefaultBackendStorage .GetRandomBackend ()
221
211
}
222
212
223
- func (dbm * DefaultBackendManager ) AddBackend (backend Backend ) {
213
+ func (dbm * DefaultBackendManager ) AddBackend (backend * Backend ) {
224
214
agentID := backend .GetAgentID ()
225
215
klog .V (5 ).InfoS ("Add the agent to DefaultBackendManager" , "agentID" , agentID )
226
216
dbm .addBackend (agentID , header .UID , backend )
227
217
}
228
218
229
- func (dbm * DefaultBackendManager ) RemoveBackend (backend Backend ) {
219
+ func (dbm * DefaultBackendManager ) RemoveBackend (backend * Backend ) {
230
220
agentID := backend .GetAgentID ()
231
221
klog .V (5 ).InfoS ("Remove the agent from the DefaultBackendManager" , "agentID" , agentID )
232
222
dbm .removeBackend (agentID , header .UID , backend )
@@ -242,7 +232,7 @@ type DefaultBackendStorage struct {
242
232
//
243
233
// TODO: fix documentation. This is not always agentID, e.g. in
244
234
// the case of DestHostBackendManager.
245
- backends map [string ][]Backend
235
+ backends map [string ][]* Backend
246
236
// agentID is tracked in this slice to enable randomly picking an
247
237
// agentID in the Backend() method. There is no reliable way to
248
238
// randomly pick a key from a map (in this case, the backends) in
@@ -270,7 +260,7 @@ func NewDefaultBackendStorage(idTypes []header.IdentifierType) *DefaultBackendSt
270
260
// no agent ever successfully connects.
271
261
metrics .Metrics .SetBackendCount (0 )
272
262
return & DefaultBackendStorage {
273
- backends : make (map [string ][]Backend ),
263
+ backends : make (map [string ][]* Backend ),
274
264
random : rand .New (rand .NewSource (time .Now ().UnixNano ())),
275
265
idTypes : idTypes ,
276
266
} /* #nosec G404 */
@@ -281,7 +271,7 @@ func containIDType(idTypes []header.IdentifierType, idType header.IdentifierType
281
271
}
282
272
283
273
// addBackend adds a backend.
284
- func (s * DefaultBackendStorage ) addBackend (identifier string , idType header.IdentifierType , backend Backend ) {
274
+ func (s * DefaultBackendStorage ) addBackend (identifier string , idType header.IdentifierType , backend * Backend ) {
285
275
if ! containIDType (s .idTypes , idType ) {
286
276
klog .V (4 ).InfoS ("fail to add backend" , "backend" , identifier , "error" , & ErrWrongIDType {idType , s .idTypes })
287
277
return
@@ -300,13 +290,13 @@ func (s *DefaultBackendStorage) addBackend(identifier string, idType header.Iden
300
290
s .backends [identifier ] = append (s .backends [identifier ], backend )
301
291
return
302
292
}
303
- s .backends [identifier ] = []Backend {backend }
293
+ s .backends [identifier ] = []* Backend {backend }
304
294
metrics .Metrics .SetBackendCount (len (s .backends ))
305
295
s .agentIDs = append (s .agentIDs , identifier )
306
296
}
307
297
308
298
// removeBackend removes a backend.
309
- func (s * DefaultBackendStorage ) removeBackend (identifier string , idType header.IdentifierType , backend Backend ) {
299
+ func (s * DefaultBackendStorage ) removeBackend (identifier string , idType header.IdentifierType , backend * Backend ) {
310
300
if ! containIDType (s .idTypes , idType ) {
311
301
klog .ErrorS (& ErrWrongIDType {idType , s .idTypes }, "fail to remove backend" )
312
302
return
@@ -377,7 +367,7 @@ func ignoreNotFound(err error) error {
377
367
}
378
368
379
369
// GetRandomBackend returns a random backend connection from all connected agents.
380
- func (s * DefaultBackendStorage ) GetRandomBackend () (Backend , error ) {
370
+ func (s * DefaultBackendStorage ) GetRandomBackend () (* Backend , error ) {
381
371
s .mu .Lock ()
382
372
defer s .mu .Unlock ()
383
373
if len (s .backends ) == 0 {
0 commit comments