1414 * limitations under the License.
1515 */
1616
17+ using System . Collections . Concurrent ;
1718using Amazon . SecurityToken . Model ;
1819using Ardalis . GuardClauses ;
1920using Microsoft . Extensions . Options ;
@@ -25,7 +26,7 @@ namespace Monai.Deploy.Storage.MinIO
2526 public class MinIoClientFactory : IMinIoClientFactory
2627 {
2728 private static readonly string DefaultClient = "_DEFAULT_" ;
28- private readonly Dictionary < string , MinioClient > _clients ;
29+ private readonly ConcurrentDictionary < string , MinioClient > _clients ;
2930
3031 private StorageServiceConfiguration Options { get ; }
3132
@@ -38,22 +39,19 @@ public MinIoClientFactory(IOptions<StorageServiceConfiguration> options)
3839
3940 Options = configuration ;
4041
41- _clients = new Dictionary < string , MinioClient > ( ) ;
42+ _clients = new ConcurrentDictionary < string , MinioClient > ( ) ;
4243 }
4344
4445 public MinioClient GetClient ( )
4546 {
46- if ( _clients . ContainsKey ( DefaultClient ) )
47- {
48- return _clients [ DefaultClient ] ;
49- }
50-
51- var accessKey = Options . Settings [ ConfigurationKeys . AccessKey ] ;
52- var accessToken = Options . Settings [ ConfigurationKeys . AccessToken ] ;
53- var client = CreateClient ( accessKey , accessToken ) ;
54-
55- _clients [ DefaultClient ] = client . Build ( ) ;
56- return _clients [ DefaultClient ] ;
47+ return _clients . GetOrAdd ( DefaultClient , _ =>
48+ {
49+ var accessKey = Options . Settings [ ConfigurationKeys . AccessKey ] ;
50+ var accessToken = Options . Settings [ ConfigurationKeys . AccessToken ] ;
51+ var client = CreateClient ( accessKey , accessToken ) ;
52+
53+ return client . Build ( ) ;
54+ } ) ;
5755 }
5856
5957 public MinioClient GetClient ( Credentials credentials )
@@ -68,21 +66,20 @@ public MinioClient GetClient(Credentials credentials, string region)
6866 Guard . Against . NullOrWhiteSpace ( credentials . SecretAccessKey , nameof ( credentials . SecretAccessKey ) ) ;
6967 Guard . Against . NullOrWhiteSpace ( credentials . SessionToken , nameof ( credentials . SessionToken ) ) ;
7068
71- if ( _clients . ContainsKey ( credentials . SessionToken ) )
69+ return _clients . GetOrAdd ( credentials . SessionToken , _ =>
7270 {
73- return _clients [ credentials . SessionToken ] ;
74- }
71+ var client = CreateClient ( credentials . AccessKeyId , credentials . SecretAccessKey ) ;
72+ client . WithSessionToken ( credentials . SessionToken ) ;
7573
76- var client = CreateClient ( credentials . AccessKeyId , credentials . SecretAccessKey ) ;
77- client . WithSessionToken ( credentials . SessionToken ) ;
74+ if ( ! string . IsNullOrWhiteSpace ( region ) )
75+ {
76+ client . WithRegion ( region ) ;
77+ }
78+
79+ return client . Build ( ) ;
80+ } ) ;
7881
79- if ( ! string . IsNullOrWhiteSpace ( region ) )
80- {
81- client . WithRegion ( region ) ;
82- }
8382
84- _clients [ DefaultClient ] = client . Build ( ) ;
85- return _clients [ DefaultClient ] ;
8683 }
8784
8885 private MinioClient CreateClient ( string accessKey , string accessToken )
0 commit comments