-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
163 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/Itmo.Dev.Platform.Kafka/Producer/KafkaOutboxMessageName.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Collections.Concurrent; | ||
|
||
namespace Itmo.Dev.Platform.Kafka.Producer; | ||
|
||
internal static class KafkaOutboxMessageName | ||
{ | ||
private static readonly ConcurrentDictionary<string, string> Values = []; | ||
|
||
public static string ForTopic(string topicName) | ||
=> Values.GetOrAdd(topicName, static topicName => $"_platform_kafka_outbox_{topicName}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/Itmo.Dev.Platform.Kafka/Producer/Outbox/FallbackOutboxMessageProducer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using Itmo.Dev.Platform.MessagePersistence; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Itmo.Dev.Platform.Kafka.Producer.Outbox; | ||
|
||
internal class FallbackOutboxMessageProducer<TKey, TValue> : IKafkaMessageProducer<TKey, TValue> | ||
{ | ||
private readonly string _topicName; | ||
private readonly IMessagePersistenceConsumer _consumer; | ||
private readonly IKafkaMessageProducer<TKey, TValue> _producer; | ||
|
||
public FallbackOutboxMessageProducer(string topicName, IServiceProvider serviceProvider) | ||
{ | ||
_topicName = topicName; | ||
_consumer = serviceProvider.GetRequiredService<IMessagePersistenceConsumer>(); | ||
_producer = serviceProvider.GetRequiredKeyedService<IKafkaMessageProducer<TKey, TValue>>(topicName); | ||
} | ||
|
||
public async Task ProduceAsync( | ||
IAsyncEnumerable<KafkaProducerMessage<TKey, TValue>> messages, | ||
CancellationToken cancellationToken) | ||
{ | ||
var messagesArray = await messages.ToArrayAsync(cancellationToken); | ||
|
||
try | ||
{ | ||
await _producer.ProduceAsync(messagesArray.ToAsyncEnumerable(), cancellationToken); | ||
} | ||
catch | ||
{ | ||
var persistedMessages = messagesArray | ||
.Select(x => new PersistedMessage<TKey, TValue>(x.Key, x.Value)) | ||
.ToArray(); | ||
|
||
await _consumer.ConsumeAsync( | ||
KafkaOutboxMessageName.ForTopic(_topicName), | ||
persistedMessages, | ||
cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Itmo.Dev.Platform.Kafka.Producer; | ||
|
||
public enum OutboxStrategy | ||
{ | ||
Always, | ||
Fallback, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
tests/Itmo.Dev.Platform.Kafka.Tests/Outbox/Models/KafkaOutboxConfigData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Itmo.Dev.Platform.Kafka.Producer; | ||
|
||
namespace Itmo.Dev.Platform.Kafka.Tests.Outbox.Models; | ||
|
||
public record KafkaOutboxConfigData( | ||
OutboxStrategy? OutboxStrategy, | ||
Type ExpectedProducerType, | ||
bool ShouldWriteOutboxMessages) | ||
{ | ||
public void ApplyConfig(Dictionary<string, string?> dictionary) | ||
{ | ||
if (OutboxStrategy is null) | ||
return; | ||
|
||
dictionary["Producer:Outbox:Strategy"] = OutboxStrategy.ToString(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
tests/Itmo.Dev.Platform.Kafka.Tests/Outbox/Models/KafkaOutboxTestData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Itmo.Dev.Platform.Kafka.Producer; | ||
|
||
namespace Itmo.Dev.Platform.Kafka.Tests.Outbox.Models; | ||
|
||
public record KafkaOutboxTestData(int BufferSize, KafkaProducerMessage<int, string>[] Messages) | ||
{ | ||
public string BufferSizeString => BufferSize.ToString(); | ||
|
||
public static KafkaOutboxTestData Single(int bufferSize) | ||
=> new(bufferSize, [new KafkaProducerMessage<int, string>(1, "aboba")]); | ||
|
||
public static KafkaOutboxTestData Many(int bufferSize, int messageCount) | ||
{ | ||
var messages = Enumerable | ||
.Range(0, messageCount) | ||
.Select(i => new KafkaProducerMessage<int, string>(i, i.ToString())) | ||
.ToArray(); | ||
|
||
return new KafkaOutboxTestData(bufferSize, messages); | ||
} | ||
} |