blob: bc8b9323538d64ce7becbe9b49493a61de7edb41 [file] [log] [blame]
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace DotPulsar.Internal
{
using Abstractions;
using PulsarApi;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
public sealed class ReaderChannelFactory : IConsumerChannelFactory
{
private readonly Guid _correlationId;
private readonly IRegisterEvent _eventRegister;
private readonly IConnectionPool _connectionPool;
private readonly IExecute _executor;
private readonly CommandSubscribe _subscribe;
private readonly uint _messagePrefetchCount;
private readonly BatchHandler _batchHandler;
private readonly IEnumerable<IDecompressorFactory> _decompressorFactories;
public ReaderChannelFactory(
Guid correlationId,
IRegisterEvent eventRegister,
IConnectionPool connectionPool,
IExecute executor,
ReaderOptions options,
IEnumerable<IDecompressorFactory> decompressorFactories)
{
_correlationId = correlationId;
_eventRegister = eventRegister;
_connectionPool = connectionPool;
_executor = executor;
_messagePrefetchCount = options.MessagePrefetchCount;
_subscribe = new CommandSubscribe
{
ConsumerName = options.ReaderName,
Durable = false,
ReadCompacted = options.ReadCompacted,
StartMessageId = options.StartMessageId.ToMessageIdData(),
Subscription = $"Reader-{Guid.NewGuid():N}",
Topic = options.Topic
};
_batchHandler = new BatchHandler(false);
_decompressorFactories = decompressorFactories;
}
public async Task<IConsumerChannel> Create(CancellationToken cancellationToken)
=> await _executor.Execute(() => GetChannel(cancellationToken), cancellationToken).ConfigureAwait(false);
private async ValueTask<IConsumerChannel> GetChannel(CancellationToken cancellationToken)
{
var connection = await _connectionPool.FindConnectionForTopic(_subscribe.Topic, cancellationToken).ConfigureAwait(false);
var messageQueue = new AsyncQueue<MessagePackage>();
var channel = new Channel(_correlationId, _eventRegister, messageQueue);
var response = await connection.Send(_subscribe, channel, cancellationToken).ConfigureAwait(false);
return new ConsumerChannel(response.ConsumerId, _messagePrefetchCount, messageQueue, connection, _batchHandler, _decompressorFactories);
}
}
}