Skip to content

Commit

Permalink
Bugfix: enforce minimum retry interval for subscriptions with no TTL
Browse files Browse the repository at this point in the history
  • Loading branch information
sweetlandj committed Aug 31, 2017
1 parent 733fa63 commit 3088c64
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
26 changes: 23 additions & 3 deletions Source/Platibus.UnitTests/Http/HttpSubscriptionMetadataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void SubscriptionExpiresIfTTLGiven()
}

[Fact]
public void TTLAlwaysBeGreaterThanOrEqualToMinTTL()
public void TTLAlwaysNeverLessThanMinTTL()
{
GivenEndpoint();
GivenTopic();
Expand All @@ -71,7 +71,7 @@ public void TTLAlwaysBeGreaterThanOrEqualToMinTTL()
}

[Fact]
public void RenewalIntervalAlwaysLessThanMaxRenewalInterval()
public void RenewalIntervalNeverExceedsMaxRenewalInterval()
{
GivenEndpoint();
GivenTopic();
Expand All @@ -82,7 +82,7 @@ public void RenewalIntervalAlwaysLessThanMaxRenewalInterval()
}

[Fact]
public void RetrIntervalAlwaysLessThanMaxRetryInterval()
public void RetryIntervalNeverExceedsMaxRetryInterval()
{
GivenEndpoint();
GivenTopic();
Expand All @@ -92,6 +92,21 @@ public void RetrIntervalAlwaysLessThanMaxRetryInterval()
Assert.Equal(HttpSubscriptionMetadata.MaxRetryInterval, Metadata.RetryInterval);
}

[Theory]
[InlineData("00:00:00")]
[InlineData("00:00:01")]
[InlineData("00:00:00.001")]
public void RetryIntervalNeverLessThanMinRetryInterval(string ttl)
{
GivenEndpoint();
GivenTopic();
GivenTTL(TimeSpan.Parse(ttl));
WhenInitializingMetadata();

Assert.Equal(HttpSubscriptionMetadata.MinRetryInterval, Metadata.RetryInterval);
Assert.True(Metadata.RetryInterval > TimeSpan.Zero);
}

protected void GivenEndpoint()
{
Endpoint = new Endpoint(new Uri("http://localhost/platibus"));
Expand All @@ -107,6 +122,11 @@ protected void GivenTTL()
TTL = TimeSpan.FromHours(1);
}

protected void GivenTTL(TimeSpan ttl)
{
TTL = ttl;
}

protected void GivenNegativeTTL()
{
TTL = TimeSpan.FromSeconds(-1);
Expand Down
16 changes: 14 additions & 2 deletions Source/Platibus/Http/HttpSubscriptionMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ public class HttpSubscriptionMetadata
/// the TTL on the subscription
/// </summary>
public static readonly TimeSpan MaxRenewalInterval = TimeSpan.FromMinutes(5);


/// <summary>
/// The minimum amount of time that can elapse before retrying a failed subscription request
/// </summary>
public static readonly TimeSpan MinRetryInterval = TimeSpan.FromSeconds(5);

/// <summary>
/// The maximum amount of time that can elapse before retrying a failed subscription request
/// </summary>
Expand Down Expand Up @@ -91,7 +96,7 @@ public HttpSubscriptionMetadata(IEndpoint endpoint, TopicName topic, TimeSpan tt
if (ttl > TimeSpan.Zero)
{
_ttl = ttl > MinTTL ? ttl : MinTTL;
_expires = true;
_expires = true;
}

// Attempt to renew after half of the TTL or 5 minutes (whichever is less)
Expand All @@ -110,6 +115,13 @@ public HttpSubscriptionMetadata(IEndpoint endpoint, TopicName topic, TimeSpan tt
{
_retryInterval = MaxRetryInterval;
}

// Ensure a minimum amount of time elapses between retries to avoid overloading
// both the subscriber and publisher with requests
if (_retryInterval < MinRetryInterval)
{
_retryInterval = MinRetryInterval;
}
}
}
}

0 comments on commit 3088c64

Please sign in to comment.