Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscription performance improvements #352

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/DataCore.Adapter/PatternMatchingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ namespace DataCore.Adapter {
/// </summary>
public static class PatternMatchingExtensions {

/// <summary>
/// The wildcard characters that can be used in a pattern.
/// </summary>
private static readonly char[] s_wildcardChars = new char[] { '*', '?' };

/// <summary>
/// Determines if the string matches the specified regular expression.
/// </summary>
Expand All @@ -35,7 +40,7 @@ public static bool Like(this string? s, Regex expression) {
}

if (s == null) {
return expression.Match(string.Empty).Success;
return false;
}

return expression.Match(s).Success;
Expand Down Expand Up @@ -89,6 +94,14 @@ public static bool Like(this string? s, string pattern) {
throw new ArgumentNullException(nameof(pattern));
}

if (s == null) {
return false;
}

if (pattern.IndexOfAny(s_wildcardChars) < 0) {
return pattern.Equals(s, StringComparison.OrdinalIgnoreCase);
}

// Construct a regex that can be used to search the string using the specified pattern
// as its base.
//
Expand Down
5 changes: 4 additions & 1 deletion src/DataCore.Adapter/SubscriptionChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,20 @@ public SubscriptionChannel(
// to _outChannel by a dedicated background task.
_inChannel = channelCapacity <= 0
? Channel.CreateUnbounded<(TValue, bool)>(new UnboundedChannelOptions() {
AllowSynchronousContinuations = false,
SingleReader = true,
SingleWriter = false
})
: Channel.CreateBounded<(TValue, bool)>(new BoundedChannelOptions(channelCapacity) {
AllowSynchronousContinuations = false,
SingleReader = true,
SingleWriter = false,
FullMode = BoundedChannelFullMode.DropWrite
});

// This is the actual channel exposed via the Reader property.
_outChannel = Channel.CreateUnbounded<TValue>(new UnboundedChannelOptions() {
AllowSynchronousContinuations = false,
SingleReader = true,
SingleWriter = false,
});
Expand Down Expand Up @@ -247,7 +250,7 @@ public bool Publish(TValue value, bool immediate = false) {
private async Task RunIngressLoop(CancellationToken cancellationToken) {
try {
while (await _inChannel.Reader.WaitToReadAsync(cancellationToken).ConfigureAwait(false)) {
while (_inChannel.Reader.TryRead(out var val)) {
while (!cancellationToken.IsCancellationRequested && _inChannel.Reader.TryRead(out var val)) {
if (val.Immediate || PublishInterval <= TimeSpan.Zero) {
if (PublishInterval > TimeSpan.Zero) {
// Cancel next publish if one is already pending.
Expand Down
8 changes: 5 additions & 3 deletions src/DataCore.Adapter/SubscriptionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,10 @@ public virtual async ValueTask<bool> ValueReceived(TValue message, CancellationT

try {
using (var ctSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _disposedTokenSource.Token)) {
await _masterChannel.Writer.WaitToWriteAsync(ctSource.Token).ConfigureAwait(false);
return _masterChannel.Writer.TryWrite((message, subscribers));
if (await _masterChannel.Writer.WaitToWriteAsync(ctSource.Token).ConfigureAwait(false)) {
return _masterChannel.Writer.TryWrite((message, subscribers));
}
return false;
}
}
catch (OperationCanceledException) {
Expand Down Expand Up @@ -585,7 +587,7 @@ public string? AdapterId {
/// capacity, attempts to write additional values into the channel will fail. A value
/// less than one indicates no limit.
/// </summary>
public int ChannelCapacity { get; set; } = 100;
public int ChannelCapacity { get; set; } = 10000;

}

Expand Down