Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
coronabytes committed Sep 14, 2024
1 parent 7ed987e commit 1b904bb
Show file tree
Hide file tree
Showing 26 changed files with 324 additions and 314 deletions.
11 changes: 6 additions & 5 deletions Core.ServiceMesh.Abstractions/DeliverPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@
public enum DeliverPolicy
{
/// <summary>
/// Default policy. Start receiving from the earliest available message in the stream.
/// Default policy. Start receiving from the earliest available message in the stream.
/// </summary>
All,

/// <summary>
/// Start with the last message added to the stream, or the last message matching the consumer's filter subject if defined.
/// Start with the last message added to the stream, or the last message matching the consumer's filter subject if
/// defined.
/// </summary>
Last,

/// <summary>
/// Start with the latest message for each filtered subject currently in the stream.
/// Start with the latest message for each filtered subject currently in the stream.
/// </summary>
LastPerSubject,

/// <summary>
/// Start receiving messages created after the consumer was created.
/// Start receiving messages created after the consumer was created.
/// </summary>
New,
New

/*/// <summary>
/// Start at the first message with the specified sequence number. The consumer must specify OptStartSeq defining the sequence number.
Expand Down
26 changes: 17 additions & 9 deletions Core.ServiceMesh.Abstractions/DurableConsumerAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,46 @@
public class DurableConsumerAttribute(string name) : Attribute
{
/// <summary>
/// Unique name for the durable consumer
/// Unique name for the durable consumer
/// </summary>
public string Name => name;

/// <summary>
/// Stream name to subscribe to. Null means container default stream
/// Stream name to subscribe to. Null means container default stream
/// </summary>
public string? Stream { get; init; }

/// <summary>
/// The maximum number of times a specific message delivery will be attempted. Applies to any message that is re-sent due to acknowledgment policy (i.e., due to a negative acknowledgment or no acknowledgment sent by the client). The default is -1 (redeliver until acknowledged). Messages that have reached the maximum delivery count will stay in the stream.
/// The maximum number of times a specific message delivery will be attempted. Applies to any message that is re-sent
/// due to acknowledgment policy (i.e., due to a negative acknowledgment or no acknowledgment sent by the client). The
/// default is -1 (redeliver until acknowledged). Messages that have reached the maximum delivery count will stay in
/// the stream.
/// </summary>
public long MaxDeliver { get; init; } = -1;

/// <summary>
/// Defines the maximum number of messages, without acknowledgment, that can be outstanding. Once this limit is reached, message delivery will be suspended. This limit applies across all of the consumer's bound subscriptions. A value of -1 means there can be any number of pending acknowledgments (i.e., no flow control). The default is 1000.
/// Defines the maximum number of messages, without acknowledgment, that can be outstanding. Once this limit is
/// reached, message delivery will be suspended. This limit applies across all of the consumer's bound subscriptions. A
/// value of -1 means there can be any number of pending acknowledgments (i.e., no flow control). The default is 1000.
/// </summary>
public long MaxAckPending { get; init; } = 1000;

/// <summary>
/// The duration (in seconds) that the server will wait for an acknowledgment for any individual message once it has been delivered to a consumer. If an acknowledgment is not received in time, the message will be redelivered.
/// The duration (in seconds) that the server will wait for an acknowledgment for any individual message once it has
/// been delivered to a consumer. If an acknowledgment is not received in time, the message will be redelivered.
/// </summary>
public long AckWait { get; set; } = 60*5;
public long AckWait { get; set; } = 60 * 5;

/// <summary>
/// The point in the stream from which to receive messages
/// Cannot be updated.
/// The point in the stream from which to receive messages
/// Cannot be updated.
/// </summary>
public DeliverPolicy DeliverPolicy { get; init; } = DeliverPolicy.All;

/// <summary>
/// A sequence of durations (in seconds) controlling the redelivery of messages on nak or acknowledgment timeout. Overrides ackWait. The sequence length must be less than or equal to MaxDelivery. If backoff is not set, a nak will result in immediate redelivery.
/// A sequence of durations (in seconds) controlling the redelivery of messages on nak or acknowledgment timeout.
/// Overrides ackWait. The sequence length must be less than or equal to MaxDelivery. If backoff is not set, a nak will
/// result in immediate redelivery.
/// </summary>
public long[] Backoff { get; init; } = [];
}
14 changes: 5 additions & 9 deletions Core.ServiceMesh.Abstractions/IServiceMesh.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
using System.Reflection;

namespace Core.ServiceMesh.Abstractions;
namespace Core.ServiceMesh.Abstractions;

public interface IServiceMesh
{
/// <summary>
/// Create remote proxy for service interface.
/// Create remote proxy for service interface.
/// </summary>
/// <typeparam name="T">service interface</typeparam>
/// <returns>proxy service implementation</returns>
T CreateProxy<T>() where T : class;

/// <summary>
/// Publish message and await confirmation.
/// Requires at least one consumer or timeout.
/// Publish message and await confirmation.
/// Requires at least one consumer or timeout.
/// </summary>
ValueTask PublishAsync(object message, int retry = 3, TimeSpan? retryWait = null, string? id = null);

/// <summary>
/// Send message and ignore confirmation
/// Send message and ignore confirmation
/// </summary>
ValueTask SendAsync(object message);

ValueTask<T> RequestAsync<T>(string subject, object[] args, Type[] generics);
ValueTask RequestAsync(string subject, object[] args, Type[] generics);
IAsyncEnumerable<T> StreamAsync<T>(string subject, object[] args, Type[] generics);


}
1 change: 1 addition & 0 deletions Core.ServiceMesh.Abstractions/ServiceMeshActivity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;

namespace Core.ServiceMesh.Abstractions;

public static class ServiceMeshActivity
Expand Down
6 changes: 3 additions & 3 deletions Core.ServiceMesh.Abstractions/ServiceMeshAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
public class ServiceMeshAttribute(string name) : Attribute
{
/// <summary>
/// Unique service name.
/// May also include versioning information
/// e.g. SampleServiceV2
/// Unique service name.
/// May also include versioning information
/// e.g. SampleServiceV2
/// </summary>
public string Name => name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
Expand Down
6 changes: 3 additions & 3 deletions Core.ServiceMesh.SourceGen.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public Task ProxyInterface()
using System.Threading.Tasks;
using System.Numerics;
using Core.ServiceMesh.Abstractions;
namespace SampleApp;
[ServiceMesh("someservice")]
Expand All @@ -25,7 +25,7 @@ public interface ISomeService
ValueTask<T> C<T>(T a, T b) where T : INumber<T>;
IAsyncEnumerable<string> D(string d);
}
[ServiceMesh("someservice")]
public class SomeService : ISomeService
{
Expand Down Expand Up @@ -55,7 +55,7 @@ public async IAsyncEnumerable<string> D(string d)

public static class TestHelper
{
public async static Task VerifySourceGen(string source)
public static async Task VerifySourceGen(string source)
{
var syntaxTree = CSharpSyntaxTree.ParseText(source);

Expand Down
17 changes: 11 additions & 6 deletions Core.ServiceMesh.SourceGen/Core/ImmutableEquatableArray.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
using System;
using System.Collections.Generic;

namespace Core.ServiceMesh.SourceGen.Core
namespace Core.ServiceMesh.SourceGen.Core;

internal static class ImmutableEquatableArray
{
internal static class ImmutableEquatableArray
public static ImmutableEquatableArray<T> Empty<T>()
where T : IEquatable<T>
{
public static ImmutableEquatableArray<T> Empty<T>()
where T : IEquatable<T> => ImmutableEquatableArray<T>.Empty;
return ImmutableEquatableArray<T>.Empty;
}

public static ImmutableEquatableArray<T> ToImmutableEquatableArray<T>(this IEnumerable<T> values)
where T : IEquatable<T> => values == null ? Empty<T>() : new(values);
public static ImmutableEquatableArray<T> ToImmutableEquatableArray<T>(this IEnumerable<T> values)
where T : IEquatable<T>
{
return values == null ? Empty<T>() : new ImmutableEquatableArray<T>(values);
}
}
115 changes: 66 additions & 49 deletions Core.ServiceMesh.SourceGen/Core/ImmutableEquatableArray{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,76 +3,93 @@
using System.Collections.Generic;
using System.Linq;

namespace Core.ServiceMesh.SourceGen.Core
namespace Core.ServiceMesh.SourceGen.Core;

internal sealed class ImmutableEquatableArray<T> : IEquatable<ImmutableEquatableArray<T>>, IReadOnlyList<T>
where T : IEquatable<T>
{
internal sealed class ImmutableEquatableArray<T> : IEquatable<ImmutableEquatableArray<T>>, IReadOnlyList<T>
where T : IEquatable<T>
private readonly T[] _values;

public ImmutableEquatableArray(T[] values)
{
public static ImmutableEquatableArray<T> Empty { get; } = new(Array.Empty<T>());
_values = values;
}

private readonly T[] _values;
public T this[int index] => _values[index];
public int Count => _values.Length;
public ImmutableEquatableArray(IEnumerable<T> values)
{
_values = values.ToArray();
}

public ImmutableEquatableArray(T[] values) => _values = values;
public static ImmutableEquatableArray<T> Empty { get; } = new(Array.Empty<T>());

public ImmutableEquatableArray(IEnumerable<T> values) => _values = values.ToArray();
public bool Equals(ImmutableEquatableArray<T> other)
{
return other != null && ((ReadOnlySpan<T>)_values).SequenceEqual(other._values);
}

public bool Equals(ImmutableEquatableArray<T> other) =>
other != null && ((ReadOnlySpan<T>)_values).SequenceEqual(other._values);
public T this[int index] => _values[index];
public int Count => _values.Length;

public override bool Equals(object obj) => obj is ImmutableEquatableArray<T> other && Equals(other);
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return ((IEnumerable<T>)_values).GetEnumerator();
}

public override int GetHashCode()
{
var hash = 0;
foreach (T value in _values)
{
hash = Combine(hash, value.GetHashCode());
}
IEnumerator IEnumerable.GetEnumerator()
{
return _values.GetEnumerator();
}

static int Combine(int h1, int h2)
{
// RyuJIT optimizes this to use the ROL instruction
// Related GitHub pull request: https://github.com/dotnet/coreclr/pull/1830
uint rol5 = (uint)h1 << 5 | (uint)h1 >> 27;
return (int)rol5 + h1 ^ h2;
}
public override bool Equals(object obj)
{
return obj is ImmutableEquatableArray<T> other && Equals(other);
}

return hash;
public override int GetHashCode()
{
var hash = 0;
foreach (var value in _values) hash = Combine(hash, value.GetHashCode());

static int Combine(int h1, int h2)
{
// RyuJIT optimizes this to use the ROL instruction
// Related GitHub pull request: https://github.com/dotnet/coreclr/pull/1830
var rol5 = ((uint)h1 << 5) | ((uint)h1 >> 27);
return ((int)rol5 + h1) ^ h2;
}

public Enumerator GetEnumerator() => new(_values);
return hash;
}

IEnumerator<T> IEnumerable<T>.GetEnumerator() => ((IEnumerable<T>)_values).GetEnumerator();
public Enumerator GetEnumerator()
{
return new Enumerator(_values);
}

IEnumerator IEnumerable.GetEnumerator() => _values.GetEnumerator();
public struct Enumerator
{
private readonly T[] _values;
private int _index;

public struct Enumerator
internal Enumerator(T[] values)
{
private readonly T[] _values;
private int _index;
_values = values;
_index = -1;
}

internal Enumerator(T[] values)
{
_values = values;
_index = -1;
}
public bool MoveNext()
{
var newIndex = _index + 1;

public bool MoveNext()
if ((uint)newIndex < (uint)_values.Length)
{
var newIndex = _index + 1;

if ((uint)newIndex < (uint)_values.Length)
{
_index = newIndex;
return true;
}

return false;
_index = newIndex;
return true;
}

public readonly T Current => _values[_index];
return false;
}

public readonly T Current => _values[_index];
}
}
21 changes: 11 additions & 10 deletions Core.ServiceMesh.SourceGen/Model/MethodDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ namespace Core.ServiceMesh.SourceGen.Model;

internal readonly record struct MethodDescription
{
public readonly ImmutableEquatableArray<string> Constraints;
public readonly ImmutableEquatableArray<string> Generics;
public readonly string Name;
public readonly ImmutableEquatableArray<string> ParameterNames;
public readonly ImmutableEquatableArray<string> Parameters;
public readonly string Return;
public readonly ImmutableEquatableArray<string> ReturnArguments;
public readonly ImmutableEquatableArray<string> Parameters;
public readonly ImmutableEquatableArray<string> ParameterNames;
public readonly ImmutableEquatableArray<string> Generics;
public readonly ImmutableEquatableArray<string> Constraints;

public MethodDescription(string name, string ret, List<string> returnArgs, List<string> parameter, List<string> parameterNames, List<string> generics, List<string> constraints)
public MethodDescription(string name, string ret, List<string> returnArgs, List<string> parameter,
List<string> parameterNames, List<string> generics, List<string> constraints)
{
Name = name;
Return = ret;
ReturnArguments = new(returnArgs);
Parameters = new(parameter);
ParameterNames = new(parameterNames);
Generics = new(generics);
Constraints = new(constraints);
ReturnArguments = new ImmutableEquatableArray<string>(returnArgs);
Parameters = new ImmutableEquatableArray<string>(parameter);
ParameterNames = new ImmutableEquatableArray<string>(parameterNames);
Generics = new ImmutableEquatableArray<string>(generics);
Constraints = new ImmutableEquatableArray<string>(constraints);
}
}
Loading

0 comments on commit 1b904bb

Please sign in to comment.