@@ -201,12 +201,52 @@ private async Task EnableDeployment(V1Deployment deployment)
201
201
}
202
202
}
203
203
204
+ Task logWriter ;
205
+ private void EnsureLogWriterRunning ( )
206
+ {
207
+ if ( ! BridgedServices . Any ( ) )
208
+ {
209
+ return ;
210
+ }
211
+
212
+ if ( logWriter == null )
213
+ {
214
+ logWriter = Task . Run ( async ( ) =>
215
+ {
216
+ while ( true )
217
+ {
218
+ try
219
+ {
220
+ foreach ( var s in BridgedServices . ToList ( ) )
221
+ {
222
+ try
223
+ {
224
+ await s . FlushLogs ( ) ;
225
+ }
226
+ catch
227
+ {
228
+ console . WriteErrorLine ( $ "Failed to flush logs for { s . ServiceName } bridge") ;
229
+ }
230
+ }
231
+ }
232
+ catch
233
+ {
234
+ }
235
+ await Task . Delay ( 250 ) ;
236
+ }
237
+ } ) ;
238
+ }
239
+ }
240
+
204
241
private async Task StartSshForward ( BridgeDetails bridgeDetails , ServiceDetails service )
205
242
{
243
+ EnsureLogWriterRunning ( ) ;
244
+
206
245
var logger = ( string msg ) =>
207
246
{
208
247
console . WriteLine ( msg ) ;
209
- _ = bridgeDetails . Client . SendAsync ( "log" , msg , false ) ;
248
+
249
+ bridgeDetails . Log ( msg ) ;
210
250
} ;
211
251
212
252
var pod = await FindInterceptionPod ( service ) ;
@@ -221,6 +261,7 @@ private async Task StartSshForward(BridgeDetails bridgeDetails, ServiceDetails s
221
261
ContainerPort = X . remotePort ,
222
262
Name = $ "port-{ X . remotePort } "
223
263
} ) . ToList ( ) ;
264
+
224
265
ports . Add ( new V1ContainerPort
225
266
{
226
267
ContainerPort = 2222 ,
@@ -279,7 +320,14 @@ private async Task StartSshForward(BridgeDetails bridgeDetails, ServiceDetails s
279
320
client . AddForwardedPort ( port ) ;
280
321
port . RequestReceived += ( object ? sender , Renci . SshNet . Common . PortForwardEventArgs e ) =>
281
322
{
282
- logger ( $ "Traffic redirected from { service . ServiceName } :{ mappings . remotePort } to localhost:{ mappings . localPort } ") ;
323
+ try
324
+ {
325
+ logger ( $ "Traffic redirected from { service . ServiceName } :{ mappings . remotePort } to localhost:{ mappings . localPort } ") ;
326
+ }
327
+ catch
328
+ {
329
+
330
+ }
283
331
} ;
284
332
port . Start ( ) ;
285
333
}
@@ -368,12 +416,21 @@ public async Task Intercept(ServiceDetails service, IReadOnlyList<(int remotePor
368
416
await StartSshForward ( bridgeDetails , service ) ;
369
417
}
370
418
419
+ private SemaphoreSlim semaphore = new SemaphoreSlim ( 1 ) ;
371
420
public async Task Release ( string connectionId )
372
421
{
373
- var services = this . BridgedServices . Where ( x => x . ConnectionId == connectionId ) ;
374
- foreach ( var service in services )
422
+ await semaphore . WaitAsync ( ) ;
423
+ try
424
+ {
425
+ var services = this . BridgedServices . Where ( x => x . ConnectionId == connectionId ) . ToList ( ) ;
426
+ foreach ( var service in services )
427
+ {
428
+ await Release ( service ) ;
429
+ }
430
+ }
431
+ finally
375
432
{
376
- await Release ( service ) ;
433
+ semaphore . Release ( ) ;
377
434
}
378
435
}
379
436
@@ -390,11 +447,12 @@ public Task Release(BridgeDetails bridgeDetails)
390
447
391
448
public async Task Release ( ServiceDetails service )
392
449
{
393
- var details = BridgedServices . FirstOrDefault ( x => x . ServiceName . Equals ( service . ServiceName , StringComparison . OrdinalIgnoreCase ) && x . Namespace . Equals ( service . Namespace , StringComparison . OrdinalIgnoreCase ) ) ;
394
- if ( details != null )
450
+ var details = BridgedServices . Where ( x => x . ServiceName . Equals ( service . ServiceName , StringComparison . OrdinalIgnoreCase ) && x . Namespace . Equals ( service . Namespace , StringComparison . OrdinalIgnoreCase ) ) . ToList ( ) ;
451
+
452
+ if ( details . Any ( ) )
395
453
{
396
- BridgedServices = BridgedServices . Where ( x => x != details ) . ToArray ( ) ;
397
- OnBridgedServicesChanged ? . Invoke ( this , BridgedServices ) ;
454
+ BridgedServices = BridgedServices . Where ( x => ! details . Contains ( x ) ) . ToArray ( ) ;
455
+ console . WriteLine ( $ "Shutting down bridge for { service . ServiceName } " ) ;
398
456
}
399
457
400
458
var pod = await FindInterceptionPod ( service ) ;
@@ -408,6 +466,10 @@ public async Task Release(ServiceDetails service)
408
466
{
409
467
await EnableDeployment ( dep ) ;
410
468
}
469
+ if ( details != null )
470
+ {
471
+ OnBridgedServicesChanged ? . Invoke ( this , BridgedServices ) ;
472
+ }
411
473
}
412
474
413
475
public async Task ReleaseAll ( )
0 commit comments