@@ -166,8 +166,7 @@ public async Task<IProducer> CreateRawSuperStreamProducer(
166
166
IDictionary < string , StreamInfo > streamInfos = new Dictionary < string , StreamInfo > ( ) ;
167
167
foreach ( var partitionsStream in partitions . Streams )
168
168
{
169
- var metaDataResponse = await _client . QueryMetadata ( new [ ] { partitionsStream } ) . ConfigureAwait ( false ) ;
170
- streamInfos [ partitionsStream ] = metaDataResponse . StreamInfos [ partitionsStream ] ;
169
+ streamInfos [ partitionsStream ] = await StreamInfo ( partitionsStream ) . ConfigureAwait ( false ) ;
171
170
}
172
171
173
172
var r = RawSuperStreamProducer . Create ( rawSuperStreamProducerConfig ,
@@ -217,8 +216,7 @@ public async Task<IConsumer> CreateSuperStreamConsumer(
217
216
IDictionary < string , StreamInfo > streamInfos = new Dictionary < string , StreamInfo > ( ) ;
218
217
foreach ( var partitionsStream in partitions . Streams )
219
218
{
220
- var metaDataResponse = await _client . QueryMetadata ( new [ ] { partitionsStream } ) . ConfigureAwait ( false ) ;
221
- streamInfos [ partitionsStream ] = metaDataResponse . StreamInfos [ partitionsStream ] ;
219
+ streamInfos [ partitionsStream ] = await StreamInfo ( partitionsStream ) . ConfigureAwait ( false ) ;
222
220
}
223
221
224
222
var s = RawSuperStreamConsumer . Create ( rawSuperStreamConsumerConfig ,
@@ -239,10 +237,7 @@ public async Task<IProducer> CreateRawProducer(RawProducerConfig rawProducerConf
239
237
throw new CreateProducerException ( "Batch Size must be bigger than 0" ) ;
240
238
}
241
239
242
- await MayBeReconnectLocator ( ) . ConfigureAwait ( false ) ;
243
- var meta = await _client . QueryMetadata ( new [ ] { rawProducerConfig . Stream } ) . ConfigureAwait ( false ) ;
244
-
245
- var metaStreamInfo = meta . StreamInfos [ rawProducerConfig . Stream ] ;
240
+ var metaStreamInfo = await StreamInfo ( rawProducerConfig . Stream ) . ConfigureAwait ( false ) ;
246
241
if ( metaStreamInfo . ResponseCode != ResponseCode . Ok )
247
242
{
248
243
throw new CreateProducerException ( $ "producer could not be created code: { metaStreamInfo . ResponseCode } ") ;
@@ -268,6 +263,47 @@ public async Task<IProducer> CreateRawProducer(RawProducerConfig rawProducerConf
268
263
}
269
264
}
270
265
266
+ private async Task < StreamInfo > StreamInfo ( string streamName )
267
+ {
268
+ // force localhost connection for single node clusters and when address resolver is not provided
269
+ // when theres 1 endpoint and an address resolver, there could be a cluster behind a load balancer
270
+ var forceLocalHost = false ;
271
+ var localPort = 0 ;
272
+ if ( _clientParameters . Endpoints . Count == 1 &&
273
+ _clientParameters . AddressResolver is null )
274
+ {
275
+ var clientParametersEndpoint = _clientParameters . Endpoints [ 0 ] ;
276
+ switch ( clientParametersEndpoint )
277
+ {
278
+ case DnsEndPoint { Host : "localhost" } dnsEndPoint :
279
+ forceLocalHost = true ;
280
+ localPort = dnsEndPoint . Port ;
281
+ break ;
282
+ case IPEndPoint ipEndPoint when Equals ( ipEndPoint . Address , IPAddress . Loopback ) :
283
+ forceLocalHost = true ;
284
+ localPort = ipEndPoint . Port ;
285
+ break ;
286
+ }
287
+ }
288
+
289
+ StreamInfo metaStreamInfo ;
290
+ if ( forceLocalHost )
291
+ {
292
+ // craft the metadata response to force using localhost
293
+ var leader = new Broker ( "localhost" , ( uint ) localPort ) ;
294
+ metaStreamInfo = new StreamInfo ( streamName , ResponseCode . Ok , leader ,
295
+ new List < Broker > ( 1 ) { leader } ) ;
296
+ }
297
+ else
298
+ {
299
+ await MayBeReconnectLocator ( ) . ConfigureAwait ( false ) ;
300
+ var meta = await _client . QueryMetadata ( new [ ] { streamName } ) . ConfigureAwait ( false ) ;
301
+ metaStreamInfo = meta . StreamInfos [ streamName ] ;
302
+ }
303
+
304
+ return metaStreamInfo ;
305
+ }
306
+
271
307
public async Task CreateStream ( StreamSpec spec )
272
308
{
273
309
var response = await _client . CreateStream ( spec . Name , spec . Args ) . ConfigureAwait ( false ) ;
@@ -350,9 +386,7 @@ public async Task<StreamStats> StreamStats(string stream)
350
386
public async Task < IConsumer > CreateRawConsumer ( RawConsumerConfig rawConsumerConfig ,
351
387
ILogger logger = null )
352
388
{
353
- await MayBeReconnectLocator ( ) . ConfigureAwait ( false ) ;
354
- var meta = await _client . QueryMetadata ( new [ ] { rawConsumerConfig . Stream } ) . ConfigureAwait ( false ) ;
355
- var metaStreamInfo = meta . StreamInfos [ rawConsumerConfig . Stream ] ;
389
+ var metaStreamInfo = await StreamInfo ( rawConsumerConfig . Stream ) . ConfigureAwait ( false ) ;
356
390
if ( metaStreamInfo . ResponseCode != ResponseCode . Ok )
357
391
{
358
392
throw new CreateConsumerException ( $ "consumer could not be created code: { metaStreamInfo . ResponseCode } ") ;
0 commit comments