@@ -74,12 +74,12 @@ private record struct SchemaGuid(string Guid, string Format);
74
74
75
75
private readonly ConcurrentDictionary < string /*subject*/ , ConcurrentDictionary < int , Task < RegisteredSchema > > > schemaByVersionBySubject =
76
76
new ConcurrentDictionary < string , ConcurrentDictionary < int , Task < RegisteredSchema > > > ( ) ;
77
-
77
+
78
78
private readonly ConcurrentDictionary < string /*subject*/ , ConcurrentDictionary < Schema , Task < RegisteredSchema > > > registeredSchemaBySchemaBySubject =
79
79
new ConcurrentDictionary < string , ConcurrentDictionary < Schema , Task < RegisteredSchema > > > ( ) ;
80
80
81
81
private readonly MemoryCache latestVersionBySubject = new MemoryCache ( new MemoryCacheOptions ( ) ) ;
82
-
82
+
83
83
private readonly MemoryCache latestWithMetadataBySubject = new MemoryCache ( new MemoryCacheOptions ( ) ) ;
84
84
85
85
private SubjectNameStrategyDelegate keySubjectNameStrategy ;
@@ -106,6 +106,11 @@ private record struct SchemaGuid(string Guid, string Format);
106
106
/// </summary>
107
107
public const int DefaultRetriesMaxWaitMs = RestService . DefaultRetriesMaxWaitMs ;
108
108
109
+ /// <summary>
110
+ /// The default maximum number of connections per server.
111
+ /// </summary>
112
+ public const int DefaultMaxConnectionsPerServer = 20 ;
113
+
109
114
/// <summary>
110
115
/// The default maximum capacity of the local schema cache.
111
116
/// </summary>
@@ -131,7 +136,6 @@ private record struct SchemaGuid(string Guid, string Format);
131
136
/// </summary>
132
137
public const SubjectNameStrategy DefaultValueSubjectNameStrategy = SubjectNameStrategy . Topic ;
133
138
134
-
135
139
/// <inheritdoc />
136
140
public IEnumerable < KeyValuePair < string , string > > Config
137
141
=> config ;
@@ -208,7 +212,7 @@ public CachedSchemaRegistryClient(IEnumerable<KeyValuePair<string, string>> conf
208
212
{
209
213
throw new ArgumentNullException ( "config" ) ;
210
214
}
211
-
215
+
212
216
this . config = config ;
213
217
this . authHeaderProvider = authenticationHeaderValueProvider ;
214
218
this . proxy = proxy ;
@@ -278,6 +282,19 @@ public CachedSchemaRegistryClient(IEnumerable<KeyValuePair<string, string>> conf
278
282
$ "Configured value for { SchemaRegistryConfig . PropertyNames . SchemaRegistryRetriesMaxWaitMs } must be an integer.") ;
279
283
}
280
284
285
+ var maxConnectionsPerServerMaybe = config . FirstOrDefault ( prop =>
286
+ prop . Key . ToLower ( ) == SchemaRegistryConfig . PropertyNames . SchemaRegistryMaxConnectionsPerServer ) ;
287
+ int maxConnectionsPerServer ;
288
+ try
289
+ {
290
+ maxConnectionsPerServer = maxConnectionsPerServerMaybe . Value == null ? DefaultMaxConnectionsPerServer : Convert . ToInt32 ( maxConnectionsPerServerMaybe . Value ) ;
291
+ }
292
+ catch ( FormatException )
293
+ {
294
+ throw new ArgumentException (
295
+ $ "Configured value for { SchemaRegistryConfig . PropertyNames . SchemaRegistryMaxConnectionsPerServer } must be an integer.") ;
296
+ }
297
+
281
298
var identityMapCapacityMaybe = config . FirstOrDefault ( prop =>
282
299
prop . Key . ToLower ( ) == SchemaRegistryConfig . PropertyNames . SchemaRegistryMaxCachedSchemas ) ;
283
300
try
@@ -305,7 +322,7 @@ public CachedSchemaRegistryClient(IEnumerable<KeyValuePair<string, string>> conf
305
322
throw new ArgumentException (
306
323
$ "Configured value for { SchemaRegistryConfig . PropertyNames . SchemaRegistryLatestCacheTtlSecs } must be an integer.") ;
307
324
}
308
-
325
+
309
326
authenticationHeaderValueProvider = RestService . AuthenticationHeaderValueProvider (
310
327
config , authenticationHeaderValueProvider , maxRetries , retriesWaitMs , retriesMaxWaitMs ) ;
311
328
@@ -321,6 +338,7 @@ public CachedSchemaRegistryClient(IEnumerable<KeyValuePair<string, string>> conf
321
338
property . Key != SchemaRegistryConfig . PropertyNames . SchemaRegistryMaxRetries &&
322
339
property . Key != SchemaRegistryConfig . PropertyNames . SchemaRegistryRetriesWaitMs &&
323
340
property . Key != SchemaRegistryConfig . PropertyNames . SchemaRegistryRetriesMaxWaitMs &&
341
+ property . Key != SchemaRegistryConfig . PropertyNames . SchemaRegistryMaxConnectionsPerServer &&
324
342
property . Key != SchemaRegistryConfig . PropertyNames . SchemaRegistryMaxCachedSchemas &&
325
343
property . Key != SchemaRegistryConfig . PropertyNames . SchemaRegistryLatestCacheTtlSecs &&
326
344
property . Key != SchemaRegistryConfig . PropertyNames . SchemaRegistryBasicAuthCredentialsSource &&
@@ -362,7 +380,7 @@ public CachedSchemaRegistryClient(IEnumerable<KeyValuePair<string, string>> conf
362
380
var sslCaLocation = config . FirstOrDefault ( prop => prop . Key . ToLower ( ) == SchemaRegistryConfig . PropertyNames . SslCaLocation ) . Value ;
363
381
var sslCaCertificate = string . IsNullOrEmpty ( sslCaLocation ) ? null : new X509Certificate2 ( sslCaLocation ) ;
364
382
this . restService = new RestService ( schemaRegistryUris , timeoutMs , authenticationHeaderValueProvider ,
365
- SetSslConfig ( config ) , sslVerify , sslCaCertificate , proxy , maxRetries , retriesWaitMs , retriesMaxWaitMs ) ;
383
+ SetSslConfig ( config ) , sslVerify , sslCaCertificate , proxy , maxRetries , retriesWaitMs , retriesMaxWaitMs , maxConnectionsPerServer ) ;
366
384
}
367
385
368
386
/// <summary>
@@ -393,10 +411,10 @@ public CachedSchemaRegistryClient(IEnumerable<KeyValuePair<string, string>> conf
393
411
394
412
/// <remarks>
395
413
/// This is to make sure memory doesn't explode in the case of incorrect usage.
396
- ///
397
- /// It's behavior is pretty extreme - remove everything and start again if the
414
+ ///
415
+ /// It's behavior is pretty extreme - remove everything and start again if the
398
416
/// cache gets full. However, in practical situations this is not expected.
399
- ///
417
+ ///
400
418
/// TODO: Implement an LRU Cache here or something instead.
401
419
/// </remarks>
402
420
private bool CleanCacheIfFull ( )
@@ -504,9 +522,9 @@ public async Task<RegisteredSchema> LookupSchemaAsync(string subject, Schema sch
504
522
registeredSchemaBySchema . TryRemove ( schema , out registeredSchema ) ;
505
523
}
506
524
}
507
-
525
+
508
526
CleanCacheIfFull ( ) ;
509
-
527
+
510
528
registeredSchemaBySchema = registeredSchemaBySchemaBySubject . GetOrAdd ( subject , _ => new ConcurrentDictionary < Schema , Task < RegisteredSchema > > ( ) ) ;
511
529
return await registeredSchemaBySchema . GetOrAddAsync ( schema , _ => restService . LookupSchemaAsync ( subject , schema , ignoreDeletedSchemas , normalize ) ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
512
530
}
@@ -519,7 +537,7 @@ public async Task<Schema> GetSchemaAsync(int id, string format = null)
519
537
{
520
538
return await schema . ConfigureAwait ( false ) ;
521
539
}
522
-
540
+
523
541
CleanCacheIfFull ( ) ;
524
542
return await schemaById . GetOrAddAsync ( schemaId , _ => restService . GetSchemaAsync ( id , format ) ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
525
543
}
@@ -546,7 +564,7 @@ public async Task<Schema> GetSchemaByGuidAsync(string guid, string format = null
546
564
{
547
565
return await schema . ConfigureAwait ( false ) ;
548
566
}
549
-
567
+
550
568
return await schemaByGuid . GetOrAddAsync ( schemaGuid , _ => restService . GetSchemaByGuidAsync ( guid , format ) ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
551
569
}
552
570
@@ -561,13 +579,13 @@ public async Task<RegisteredSchema> GetRegisteredSchemaAsync(string subject, int
561
579
return await schema . ConfigureAwait ( false ) ;
562
580
}
563
581
}
564
-
582
+
565
583
CleanCacheIfFull ( ) ;
566
584
schemaByVersion = schemaByVersionBySubject . GetOrAdd ( subject , _ => new ConcurrentDictionary < int , Task < RegisteredSchema > > ( ) ) ;
567
585
return await schemaByVersion . GetOrAddAsync ( version , async _ =>
568
586
{
569
587
var schema = await restService . GetSchemaAsync ( subject , version ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
570
-
588
+
571
589
// We already have the schema so we can add it to the cache.
572
590
var format = GetSchemaFormat ( schema . SchemaString ) ;
573
591
schemaById . TryAdd ( new SchemaId ( schema . Id , format ) , Task . FromResult ( schema . Schema ) ) ;
@@ -717,4 +735,4 @@ protected virtual void Dispose(bool disposing)
717
735
}
718
736
}
719
737
}
720
- }
738
+ }
0 commit comments