77using System . Linq ;
88using System . Reactive . Linq ;
99using System . Threading . Tasks ;
10- using Microsoft . Azure . AppService . Proxy . Common . Extensions ;
1110using Microsoft . Azure . AppService . Proxy . Common . Infra ;
12- using Microsoft . Azure . AppService . Proxy . Runtime ;
1311using Microsoft . Azure . WebJobs . Script . Config ;
1412using Microsoft . Azure . WebJobs . Script . Diagnostics ;
1513using Microsoft . Azure . WebJobs . Script . Eventing ;
@@ -36,7 +34,7 @@ public class WebHostRpcWorkerChannelManager : IWebHostRpcWorkerChannelManager
3634 private Action _shutdownStandbyWorkerChannels ;
3735 private IConfiguration _config ;
3836
39- private ConcurrentDictionary < string , Dictionary < string , TaskCompletionSource < IRpcWorkerChannel > > > _workerChannels = new ConcurrentDictionary < string , Dictionary < string , TaskCompletionSource < IRpcWorkerChannel > > > ( StringComparer . OrdinalIgnoreCase ) ;
37+ private ConcurrentDictionary < string , ConcurrentDictionary < string , TaskCompletionSource < IRpcWorkerChannel > > > _workerChannels = new ( StringComparer . OrdinalIgnoreCase ) ;
4038
4139 public WebHostRpcWorkerChannelManager ( IScriptEventManager eventManager ,
4240 IEnvironment environment ,
@@ -101,7 +99,7 @@ await rpcWorkerChannel.StartWorkerProcessAsync().ContinueWith(processStartTask =
10199
102100 internal Task < IRpcWorkerChannel > GetChannelAsync ( string language )
103101 {
104- if ( ! string . IsNullOrEmpty ( language ) && _workerChannels . TryGetValue ( language , out Dictionary < string , TaskCompletionSource < IRpcWorkerChannel > > workerChannels ) )
102+ if ( ! string . IsNullOrEmpty ( language ) && _workerChannels . TryGetValue ( language , out ConcurrentDictionary < string , TaskCompletionSource < IRpcWorkerChannel > > workerChannels ) )
105103 {
106104 if ( workerChannels . Count > 0 && workerChannels . TryGetValue ( workerChannels . Keys . First ( ) , out TaskCompletionSource < IRpcWorkerChannel > valueTask ) )
107105 {
@@ -111,9 +109,9 @@ internal Task<IRpcWorkerChannel> GetChannelAsync(string language)
111109 return Task . FromResult < IRpcWorkerChannel > ( null ) ;
112110 }
113111
114- public Dictionary < string , TaskCompletionSource < IRpcWorkerChannel > > GetChannels ( string language )
112+ public IDictionary < string , TaskCompletionSource < IRpcWorkerChannel > > GetChannels ( string language )
115113 {
116- if ( ! string . IsNullOrEmpty ( language ) && _workerChannels . TryGetValue ( language , out Dictionary < string , TaskCompletionSource < IRpcWorkerChannel > > workerChannels ) )
114+ if ( ! string . IsNullOrEmpty ( language ) && _workerChannels . TryGetValue ( language , out ConcurrentDictionary < string , TaskCompletionSource < IRpcWorkerChannel > > workerChannels ) )
117115 {
118116 return workerChannels ;
119117 }
@@ -237,7 +235,7 @@ public Task<bool> ShutdownChannelIfExistsAsync(string language, string workerId,
237235
238236 if ( _hostingConfigOptions . Value . RevertWorkerShutdownBehaviour )
239237 {
240- if ( _workerChannels . TryRemove ( language , out Dictionary < string , TaskCompletionSource < IRpcWorkerChannel > > rpcWorkerChannels ) )
238+ if ( _workerChannels . TryRemove ( language , out ConcurrentDictionary < string , TaskCompletionSource < IRpcWorkerChannel > > rpcWorkerChannels ) )
241239 {
242240 if ( rpcWorkerChannels . TryGetValue ( workerId , out TaskCompletionSource < IRpcWorkerChannel > value ) )
243241 {
@@ -264,7 +262,7 @@ public Task<bool> ShutdownChannelIfExistsAsync(string language, string workerId,
264262 }
265263 else
266264 {
267- if ( _workerChannels . TryGetValue ( language , out Dictionary < string , TaskCompletionSource < IRpcWorkerChannel > > rpcWorkerChannels )
265+ if ( _workerChannels . TryGetValue ( language , out ConcurrentDictionary < string , TaskCompletionSource < IRpcWorkerChannel > > rpcWorkerChannels )
268266 && rpcWorkerChannels . TryRemove ( workerId , out TaskCompletionSource < IRpcWorkerChannel > value ) )
269267 {
270268 value ? . Task . ContinueWith ( channelTask =>
@@ -304,7 +302,7 @@ internal void ScheduleShutdownStandbyChannels()
304302 using ( _metricsLogger . LatencyEvent ( string . Format ( MetricEventNames . SpecializationShutdownStandbyChannels , runtime . Key ) ) )
305303 {
306304 _logger . LogInformation ( "Disposing standby channel for runtime:{language}" , runtime . Key ) ;
307- if ( _workerChannels . TryRemove ( runtime . Key , out Dictionary < string , TaskCompletionSource < IRpcWorkerChannel > > standbyChannels ) )
305+ if ( _workerChannels . TryRemove ( runtime . Key , out ConcurrentDictionary < string , TaskCompletionSource < IRpcWorkerChannel > > standbyChannels ) )
308306 {
309307 foreach ( string workerId in standbyChannels . Keys )
310308 {
@@ -338,7 +336,7 @@ public async Task ShutdownChannelsAsync()
338336 foreach ( string runtime in _workerChannels . Keys )
339337 {
340338 _logger . LogInformation ( "Shutting down language worker channels for runtime:{runtime}" , runtime ) ;
341- if ( _workerChannels . TryRemove ( runtime , out Dictionary < string , TaskCompletionSource < IRpcWorkerChannel > > standbyChannels ) )
339+ if ( _workerChannels . TryRemove ( runtime , out ConcurrentDictionary < string , TaskCompletionSource < IRpcWorkerChannel > > standbyChannels ) )
342340 {
343341 foreach ( string workerId in standbyChannels . Keys )
344342 {
@@ -378,21 +376,21 @@ internal void AddOrUpdateWorkerChannels(string initializedRuntime, IRpcWorkerCha
378376 _workerChannels . AddOrUpdate ( initializedRuntime ,
379377 ( runtime ) =>
380378 {
381- Dictionary < string , TaskCompletionSource < IRpcWorkerChannel > > newLanguageWorkerChannels = new Dictionary < string , TaskCompletionSource < IRpcWorkerChannel > > ( ) ;
382- newLanguageWorkerChannels . Add ( initializedLanguageWorkerChannel . Id , new TaskCompletionSource < IRpcWorkerChannel > ( ) ) ;
379+ ConcurrentDictionary < string , TaskCompletionSource < IRpcWorkerChannel > > newLanguageWorkerChannels = new ( StringComparer . OrdinalIgnoreCase ) ;
380+ newLanguageWorkerChannels . TryAdd ( initializedLanguageWorkerChannel . Id , new TaskCompletionSource < IRpcWorkerChannel > ( ) ) ;
383381 return newLanguageWorkerChannels ;
384382 } ,
385383 ( runtime , existingLanguageWorkerChannels ) =>
386384 {
387- existingLanguageWorkerChannels . Add ( initializedLanguageWorkerChannel . Id , new TaskCompletionSource < IRpcWorkerChannel > ( ) ) ;
385+ existingLanguageWorkerChannels . TryAdd ( initializedLanguageWorkerChannel . Id , new TaskCompletionSource < IRpcWorkerChannel > ( ) ) ;
388386 return existingLanguageWorkerChannels ;
389387 } ) ;
390388 }
391389
392390 internal void SetInitializedWorkerChannel ( string initializedRuntime , IRpcWorkerChannel initializedLanguageWorkerChannel )
393391 {
394392 _logger . LogDebug ( "Adding webhost language worker channel for runtime: {language}. workerId:{id}" , initializedRuntime , initializedLanguageWorkerChannel . Id ) ;
395- if ( _workerChannels . TryGetValue ( initializedRuntime , out Dictionary < string , TaskCompletionSource < IRpcWorkerChannel > > channel ) )
393+ if ( _workerChannels . TryGetValue ( initializedRuntime , out ConcurrentDictionary < string , TaskCompletionSource < IRpcWorkerChannel > > channel ) )
396394 {
397395 if ( channel . TryGetValue ( initializedLanguageWorkerChannel . Id , out TaskCompletionSource < IRpcWorkerChannel > value ) )
398396 {
@@ -404,7 +402,7 @@ internal void SetInitializedWorkerChannel(string initializedRuntime, IRpcWorkerC
404402 internal void SetExceptionOnInitializedWorkerChannel ( string initializedRuntime , IRpcWorkerChannel initializedLanguageWorkerChannel , Exception exception )
405403 {
406404 _logger . LogDebug ( "Failed to initialize webhost language worker channel for runtime: {language}. workerId:{id}" , initializedRuntime , initializedLanguageWorkerChannel . Id ) ;
407- if ( _workerChannels . TryGetValue ( initializedRuntime , out Dictionary < string , TaskCompletionSource < IRpcWorkerChannel > > channel ) )
405+ if ( _workerChannels . TryGetValue ( initializedRuntime , out ConcurrentDictionary < string , TaskCompletionSource < IRpcWorkerChannel > > channel ) )
408406 {
409407 if ( channel . TryGetValue ( initializedLanguageWorkerChannel . Id , out TaskCompletionSource < IRpcWorkerChannel > value ) )
410408 {
0 commit comments