Skip to content

Commit

Permalink
(GH-369) Fix a bug in IpAddreses.GetUnassignedAsync that caused only …
Browse files Browse the repository at this point in the history
…the first 10 addresses to be returned
  • Loading branch information
Jericho committed Jan 11, 2022
1 parent dadb4b4 commit 8d76915
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Source/StrongGrid.UnitTests/Resources/ApiKeysTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public async Task GetAllAsync()
var apiKeys = new ApiKeys(client);

// Act
var result = await apiKeys.GetAllAsync(null, CancellationToken.None).ConfigureAwait(false);
var result = await apiKeys.GetAllAsync(cancellationToken: CancellationToken.None).ConfigureAwait(false);

// Assert
mockHttp.VerifyNoOutstandingExpectation();
Expand Down
18 changes: 12 additions & 6 deletions Source/StrongGrid/Extensions/Public.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,14 +1150,20 @@ public static Task<Segment> UpdateAsync(this ISegments segments, string segmentI
/// </returns>
public static async Task<IpAddress[]> GetUnassignedAsync(this IIpAddresses ipAddresses, CancellationToken cancellationToken = default)
{
var allIpAddresses = await ipAddresses.GetAllAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
var unassignedIpAddresses = new List<IpAddress>();
var currentOffset = 0;

var unassignedIpAddresses = allIpAddresses.Records
.Where(ip => ip.Pools == null || !ip.Pools.Any())
.ToArray();
while (true)
{
var allIpAddresses = await ipAddresses.GetAllAsync(limit: Utils.MaxSendGridPagingLimit, offset: currentOffset, cancellationToken: cancellationToken).ConfigureAwait(false);
unassignedIpAddresses.AddRange(allIpAddresses.Where(ip => ip.Pools == null || !ip.Pools.Any()));

return unassignedIpAddresses;
}
if (allIpAddresses.Length < Utils.MaxSendGridPagingLimit) break;

currentOffset += Utils.MaxSendGridPagingLimit;
}

return unassignedIpAddresses.ToArray();
}
}
}
2 changes: 2 additions & 0 deletions Source/StrongGrid/Utilities/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ internal static class Utils
{
private static readonly byte[] Secp256R1Prefix = Convert.FromBase64String("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE");

public const int MaxSendGridPagingLimit = 500;

public static RecyclableMemoryStreamManager MemoryStreamManager { get; } = new RecyclableMemoryStreamManager();

/// <summary>
Expand Down

0 comments on commit 8d76915

Please sign in to comment.