-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This also adds a new summary for deserialization timings, together with summaries for compression and decompression timings. Fixes #200 Co-authored-by: Florian Grieskamp <[email protected]> Co-authored-by: Phillip Kemkes <[email protected]> Co-authored-by: Florian Hockmann <[email protected]> Co-authored-by: Phillip Kemkes <[email protected]>
- Loading branch information
1 parent
15d9372
commit 5765e00
Showing
35 changed files
with
718 additions
and
63 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
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
64 changes: 64 additions & 0 deletions
64
src/Motor.Extensions.Compression.Abstractions/CompressionTypeExtension.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,64 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using CloudNative.CloudEvents; | ||
|
||
namespace Motor.Extensions.Compression.Abstractions | ||
{ | ||
public class CompressionTypeExtension : ICloudEventExtension | ||
{ | ||
private const string CompressionTypeAttributeName = "compressionType"; | ||
private IDictionary<string, object> _attributes = new Dictionary<string, object>(); | ||
|
||
public CompressionTypeExtension(string compressionType) | ||
{ | ||
CompressionType = compressionType; | ||
} | ||
|
||
public string? CompressionType | ||
{ | ||
get => (string?)_attributes[CompressionTypeAttributeName]; | ||
private init => _attributes[CompressionTypeAttributeName] = | ||
value ?? throw new ArgumentNullException(nameof(CompressionType)); | ||
} | ||
|
||
public void Attach(CloudEvent cloudEvent) | ||
{ | ||
var eventAttributes = cloudEvent.GetAttributes(); | ||
if (_attributes == eventAttributes) | ||
{ | ||
// already done | ||
return; | ||
} | ||
|
||
foreach (var (key, value) in _attributes) | ||
{ | ||
eventAttributes[key] = value; | ||
} | ||
|
||
_attributes = eventAttributes; | ||
} | ||
|
||
public bool ValidateAndNormalize(string key, ref object value) | ||
{ | ||
if (key is CompressionTypeAttributeName) | ||
{ | ||
return value switch | ||
{ | ||
null => true, | ||
string _ => true, | ||
_ => throw new InvalidOperationException("ErrorCompressionTypeValueIsNotAString") | ||
}; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
// Disabled null check because CloudEvent SDK doesn't | ||
// implement null-checks | ||
#pragma warning disable CS8603 | ||
public Type GetAttributeType(string name) | ||
{ | ||
return name is CompressionTypeAttributeName ? typeof(string) : null; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Motor.Extensions.Compression.Abstractions/IMessageCompressor.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.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Motor.Extensions.Compression.Abstractions | ||
{ | ||
public interface IMessageCompressor | ||
{ | ||
string CompressionType { get; } | ||
Task<byte[]> CompressAsync(byte[] rawMessage, CancellationToken cancellationToken); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Motor.Extensions.Compression.Abstractions/IMessageDecompressor.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.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Motor.Extensions.Compression.Abstractions | ||
{ | ||
public interface IMessageDecompressor | ||
{ | ||
string CompressionType { get; } | ||
Task<byte[]> DecompressAsync(byte[] compressedMessage, CancellationToken cancellationToken); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...otor.Extensions.Compression.Abstractions/Motor.Extensions.Compression.Abstractions.csproj
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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CloudNative.CloudEvents" Version="1.3.80" /> | ||
</ItemGroup> | ||
|
||
<Import Project="$(MSBuildThisFileDirectory)../../shared.csproj" /> | ||
|
||
</Project> |
18 changes: 18 additions & 0 deletions
18
src/Motor.Extensions.Compression.Abstractions/NoOpMessageCompressor.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,18 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Motor.Extensions.Compression.Abstractions | ||
{ | ||
|
||
public class NoOpMessageCompressor : IMessageCompressor | ||
{ | ||
public string CompressionType => NoOpCompressionType; | ||
|
||
public const string NoOpCompressionType = "uncompressed"; | ||
|
||
public Task<byte[]> CompressAsync(byte[] rawMessage, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult(rawMessage); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Motor.Extensions.Compression.Abstractions/NoOpMessageDecompressor.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,15 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Motor.Extensions.Compression.Abstractions | ||
{ | ||
public class NoOpMessageDecompressor : IMessageDecompressor | ||
{ | ||
public string CompressionType => NoOpMessageCompressor.NoOpCompressionType; | ||
|
||
public Task<byte[]> DecompressAsync(byte[] compressedMessage, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult(compressedMessage); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Motor.Extensions.Compression.Gzip/GzipHostBuilderExtensions.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 Motor.Extensions.Hosting.Abstractions; | ||
|
||
namespace Motor.Extensions.Compression.Gzip | ||
{ | ||
public static class GzipHostBuilderExtensions | ||
{ | ||
public static IPublisherBuilder<TOut> AddGzipCompression<TOut>(this IPublisherBuilder<TOut> hostBuilder) | ||
where TOut : notnull | ||
{ | ||
hostBuilder.AddCompressor<GzipMessageCompressor>(); | ||
return hostBuilder; | ||
} | ||
|
||
public static IConsumerBuilder<TOut> AddGzipDecompression<TOut>(this IConsumerBuilder<TOut> hostBuilder) | ||
where TOut : notnull | ||
{ | ||
hostBuilder.AddDecompressor<GzipMessageDecompressor>(); | ||
return hostBuilder; | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Motor.Extensions.Compression.Gzip/GzipMessageCompressor.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,26 @@ | ||
using System.IO; | ||
using System.IO.Compression; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Motor.Extensions.Compression.Abstractions; | ||
|
||
namespace Motor.Extensions.Compression.Gzip | ||
{ | ||
public class GzipMessageCompressor : IMessageCompressor | ||
{ | ||
public string CompressionType => GzipCompressionType; | ||
|
||
public const string GzipCompressionType = "gzip"; | ||
|
||
public async Task<byte[]> CompressAsync(byte[] rawMessage, CancellationToken cancellationToken) | ||
{ | ||
await using var compressedStream = new MemoryStream(); | ||
//use using with explicit scope to close/flush the GZipStream before using the outputstream | ||
await using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress)) | ||
{ | ||
await zipStream.WriteAsync(rawMessage, cancellationToken); | ||
} | ||
return compressedStream.ToArray(); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Motor.Extensions.Compression.Gzip/GzipMessageDecompressor.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,27 @@ | ||
using System.IO; | ||
using System.IO.Compression; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Motor.Extensions.Compression.Abstractions; | ||
|
||
namespace Motor.Extensions.Compression.Gzip | ||
{ | ||
public class GzipMessageDecompressor : IMessageDecompressor | ||
{ | ||
public string CompressionType => GzipMessageCompressor.GzipCompressionType; | ||
|
||
public async Task<byte[]> DecompressAsync(byte[] compressedMessage, CancellationToken cancellationToken) | ||
{ | ||
var compressedStream = new MemoryStream(compressedMessage); | ||
|
||
var output = new MemoryStream(); | ||
//use using with explicit scope to close/flush the GZipStream before using the outputstream | ||
await using (var decompressionStream = new GZipStream(compressedStream, CompressionMode.Decompress)) | ||
{ | ||
await decompressionStream.CopyToAsync(output, cancellationToken); | ||
} | ||
|
||
return output.ToArray(); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Motor.Extensions.Compression.Gzip/Motor.Extensions.Compression.Gzip.csproj
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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Motor.Extensions.Compression.Abstractions\Motor.Extensions.Compression.Abstractions.csproj" /> | ||
<ProjectReference Include="..\Motor.Extensions.Hosting.Abstractions\Motor.Extensions.Hosting.Abstractions.csproj" /> | ||
</ItemGroup> | ||
|
||
<Import Project="$(MSBuildThisFileDirectory)../../shared.csproj" /> | ||
|
||
</Project> |
Oops, something went wrong.