feat: add jetstream consumer api lifecycle
This commit is contained in:
57
src/NATS.Server/JetStream/ConsumerManager.cs
Normal file
57
src/NATS.Server/JetStream/ConsumerManager.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System.Collections.Concurrent;
|
||||
using NATS.Server.JetStream.Api;
|
||||
using NATS.Server.JetStream.Models;
|
||||
using NATS.Server.JetStream.Storage;
|
||||
|
||||
namespace NATS.Server.JetStream;
|
||||
|
||||
public sealed class ConsumerManager
|
||||
{
|
||||
private readonly ConcurrentDictionary<(string Stream, string Name), ConsumerHandle> _consumers = new();
|
||||
|
||||
public int ConsumerCount => _consumers.Count;
|
||||
|
||||
public JetStreamApiResponse CreateOrUpdate(string stream, ConsumerConfig config)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(config.DurableName))
|
||||
return JetStreamApiResponse.ErrorResponse(400, "durable name required");
|
||||
|
||||
var key = (stream, config.DurableName);
|
||||
var handle = _consumers.AddOrUpdate(key,
|
||||
_ => new ConsumerHandle(stream, config),
|
||||
(_, existing) => existing with { Config = config });
|
||||
|
||||
return new JetStreamApiResponse
|
||||
{
|
||||
ConsumerInfo = new JetStreamConsumerInfo
|
||||
{
|
||||
Config = handle.Config,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
public JetStreamApiResponse GetInfo(string stream, string durableName)
|
||||
{
|
||||
if (_consumers.TryGetValue((stream, durableName), out var handle))
|
||||
{
|
||||
return new JetStreamApiResponse
|
||||
{
|
||||
ConsumerInfo = new JetStreamConsumerInfo
|
||||
{
|
||||
Config = handle.Config,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return JetStreamApiResponse.NotFound($"$JS.API.CONSUMER.INFO.{stream}.{durableName}");
|
||||
}
|
||||
|
||||
public bool TryGet(string stream, string durableName, out ConsumerHandle handle)
|
||||
=> _consumers.TryGetValue((stream, durableName), out handle!);
|
||||
}
|
||||
|
||||
public sealed record ConsumerHandle(string Stream, ConsumerConfig Config)
|
||||
{
|
||||
public ulong NextSequence { get; set; } = 1;
|
||||
public Queue<StoredMessage> Pending { get; } = new();
|
||||
}
|
||||
Reference in New Issue
Block a user