Skip to content

Commit

Permalink
Paginated queries where Branches are retrieved - introducing a hardco…
Browse files Browse the repository at this point in the history
…ded limit as a single variable
  • Loading branch information
jsdbroughton committed Apr 22, 2023
1 parent fdbdc95 commit 5459af4
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions DesktopUI2/DesktopUI2/ViewModels/StreamViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ public int SelectedTab

internal Client Client { get; }

/// The limit of branches to be displayed in the stream, Given a limit to prevent runaway Branch DOS attacks
const int BranchLimit = 500;

public void GoBack()
{
PreviewOn = false;
Expand Down Expand Up @@ -696,17 +699,17 @@ internal async void GetBranchesAndRestoreState()
AvailableFilters = new List<FilterViewModel>(Bindings.GetSelectionFilters().Select(x => new FilterViewModel(x)));
SelectedFilter = AvailableFilters[0];

// Get all branches created in the stream, not limited to the server limits
string nextCursor = null;
var branchCount = 0;
const int branchLimit = 500;

do
{
var branchResponse = await Client.PagedStreamGetBranches(Stream.id, 100, 0, nextCursor).ConfigureAwait(false);
Branches.AddRange(branchResponse.items);
nextCursor = branchResponse.cursor;
branchCount += branchResponse.items.Count;
} while (!string.IsNullOrEmpty(nextCursor) && branchCount <= branchLimit);
} while (!string.IsNullOrEmpty(nextCursor) && branchCount <= BranchLimit);

var index = Branches.FindIndex(x => x.name == StreamState.BranchName);
if (index != -1)
Expand Down Expand Up @@ -758,7 +761,7 @@ private void GetReport()
Report = report;

//do not switch to report tab automatically
//if (HasReportItems)
//if (HasReportItems)
//{
// // activate report tab
// SelectedTab = 4;
Expand Down Expand Up @@ -860,7 +863,18 @@ private void UpdateStreamState()
private async Task GetBranches()
{
var prevBranchName = SelectedBranch != null ? SelectedBranch.Branch.name : StreamState.BranchName;
Branches = await Client.StreamGetBranches(Stream.id, 1000, 0);

// Get all branches created in the stream, not limited to the server limits
string nextCursor = null;
var branchCount = 0;

do
{
var branchResponse = await Client.PagedStreamGetBranches(Stream.id, 100, 0, nextCursor).ConfigureAwait(false);
Branches.AddRange(branchResponse.items);
nextCursor = branchResponse.cursor;
branchCount += branchResponse.items.Count;
} while (!string.IsNullOrEmpty(nextCursor) && branchCount <= BranchLimit);

var index = Branches.FindIndex(x => x.name == prevBranchName);
if (index != -1)
Expand Down Expand Up @@ -1610,6 +1624,5 @@ public void Dispose()
Client?.Dispose();
}
#endregion

}
}

0 comments on commit 5459af4

Please sign in to comment.