Skip to content

Commit

Permalink
Fix bug causing subscription to be always validated (#19)
Browse files Browse the repository at this point in the history
* - Add warning when event is dropped due to there being no subscribers
- Reduce log level to DBG when an event is filtered out
- Fix bug in validation logic

* Add disabled topic example
  • Loading branch information
pm7y authored Nov 17, 2020
1 parent 4f83690 commit 66dcb9f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -34,11 +35,22 @@ protected override Task Handle(SendNotificationEventsToSubscriberCommand request
eventGridEvent.MetadataVersion = "1";
}

foreach (var subscription in request.Topic.Subscribers)
if (!request.Topic.Subscribers.Any())
{
_logger.LogWarning("'{TopicName}' has no subscribers so {EventCount} event(s) could not be forwarded", request.Topic.Name, request.Events.Length);
}
else if (request.Topic.Subscribers.All(o => o.Disabled))
{
_logger.LogWarning("'{TopicName}' has no enabled subscribers so {EventCount} event(s) could not be forwarded", request.Topic.Name, request.Events.Length);
}
else
{
foreach (var subscription in request.Topic.Subscribers)
{
#pragma warning disable 4014
SendToSubscriber(subscription, request.Events);
SendToSubscriber(subscription, request.Events);
#pragma warning restore 4014
}
}

return Task.CompletedTask;
Expand Down Expand Up @@ -85,7 +97,7 @@ await httpClient.PostAsync(subscription.Endpoint, content)
}
else
{
_logger.LogWarning("Event {EventId} filtered out for subscriber '{SubscriberName}'.", evt.Id, subscription.Name);
_logger.LogDebug("Event {EventId} filtered out for subscriber '{SubscriberName}'.", evt.Id, subscription.Name);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task<Unit> Handle(ValidateAllSubscriptionsCommand request, Cancella
.Where(o => !o.Disabled))
{
foreach (var subscriber in enabledTopic.Subscribers
.Where(o => !o.Disabled))
.Where(o => !o.DisableValidation && !o.Disabled))
{
await ValidateSubscription(enabledTopic, subscriber);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private async Task ValidateNotificationRequest(HttpContext context,
const int maximumAllowedOverallMessageSizeInBytes = 1536000;
const int maximumAllowedEventGridEventSizeInBytes = 66560;

logger.LogDebug("Message is {Bytes} in length.", requestBody.Length);
logger.LogTrace("Message is {Bytes} in length.", requestBody.Length);

if (requestBody.Length > maximumAllowedOverallMessageSizeInBytes)
{
Expand All @@ -97,7 +97,7 @@ private async Task ValidateNotificationRequest(HttpContext context,
// ReSharper disable once MethodHasAsyncOverload
var eventSize = JsonConvert.SerializeObject(evt, Formatting.None).Length;

logger.LogDebug("Event is {Bytes} in length.", eventSize);
logger.LogTrace("Event is {Bytes} in length.", eventSize);

if (eventSize > maximumAllowedEventGridEventSizeInBytes)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public bool IsValid(IHeaderDictionary requestHeaders, string topicKey)
return false;
}

_logger.LogDebug("'aeg-sas-key' header is valid");
_logger.LogTrace("'aeg-sas-key' header is valid");
return true;
}

Expand All @@ -42,7 +42,7 @@ public bool IsValid(IHeaderDictionary requestHeaders, string topicKey)
return false;
}

_logger.LogDebug("'aeg-sas-token' header is valid");
_logger.LogTrace("'aeg-sas-token' header is valid");
return true;
}

Expand Down
27 changes: 14 additions & 13 deletions src/AzureEventGridSimulator/example.appsettings.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
{
"topics": [

{
"name": "SupplyChain-DEV",
"port": 60111,
"key": "TheLocal+DevelopmentKey=",
"disabled": true, // this topic is disabled it's endpoint won't be created at startup
"subscribers": [
{
"name": "LocalWebApiSubscription",
"endpoint": "https://localhost:5050/api/SomeApi"
}
]
},

{
"name": "MyAwesomeTopic",
"port": 60101,
Expand All @@ -35,7 +22,21 @@
"subscribers": [
// There are no subscribers defined yet, so you can send events to this topic but they won't be delivered to anyone
]
},

{
"name": "ADisabledTopic",
"port": 60103,
"key": "TheLocal+DevelopmentKey=",
"disabled": true, // this topic is disabled so it's endpoint won't be created at startup
"subscribers": [
{
"name": "LocalWebApiSubscription",
"endpoint": "https://localhost:5050/api/SomeApi"
}
]
}

],
"Serilog": {
"MinimumLevel": {
Expand Down

0 comments on commit 66dcb9f

Please sign in to comment.