Skip to content

Commit

Permalink
Use batching pattern for cf apps command
Browse files Browse the repository at this point in the history
  • Loading branch information
Lisa Burns committed Oct 27, 2020
1 parent 87b4e27 commit a78e718
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
22 changes: 17 additions & 5 deletions actor/v7action/application_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
"code.cloudfoundry.org/cli/resources"
"code.cloudfoundry.org/cli/util/batcher"
)

type ApplicationSummary struct {
Expand Down Expand Up @@ -47,9 +48,14 @@ func (actor Actor) GetAppSummariesForSpace(spaceGUID string, labelSelector strin
if err != nil {
return nil, allWarnings, err
}
var processes []resources.Process

processes, warnings, err := actor.CloudControllerClient.GetProcesses(ccv3.Query{
Key: ccv3.AppGUIDFilter, Values: toAppGUIDs(apps),
warnings, err = batcher.RequestByGUID(toAppGUIDs(apps), func(guids []string) (ccv3.Warnings, error) {
batch, warnings, err := actor.CloudControllerClient.GetProcesses(ccv3.Query{
Key: ccv3.AppGUIDFilter, Values: guids,
})
processes = append(processes, batch...)
return warnings, err
})
allWarnings = append(allWarnings, warnings...)
if err != nil {
Expand Down Expand Up @@ -77,10 +83,16 @@ func (actor Actor) GetAppSummariesForSpace(spaceGUID string, labelSelector strin
processSummariesByAppGUID[process.AppGUID] = append(processSummariesByAppGUID[process.AppGUID], processSummary)
}

routes, warnings, err := actor.CloudControllerClient.GetRoutes(ccv3.Query{
Key: ccv3.AppGUIDFilter, Values: toAppGUIDs(apps),
var routes []resources.Route

warnings, err = batcher.RequestByGUID(toAppGUIDs(apps), func(guids []string) (ccv3.Warnings, error) {
batch, warnings, err := actor.CloudControllerClient.GetRoutes(ccv3.Query{
Key: ccv3.AppGUIDFilter, Values: guids,
})
routes = append(routes, batch...)
return warnings, err
})
allWarnings = append(allWarnings, Warnings(warnings)...)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return nil, allWarnings, err
}
Expand Down
43 changes: 43 additions & 0 deletions actor/v7action/application_summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v7action_test

import (
"errors"
"fmt"

"code.cloudfoundry.org/cli/actor/v7action"
. "code.cloudfoundry.org/cli/actor/v7action"
Expand Down Expand Up @@ -296,6 +297,48 @@ var _ = Describe("Application Summary Actions", func() {
))
})
})
When("there is long list of app guids", func() {
BeforeEach(func() {
lotsOfApps := []resources.Application{}
for i := 0; i < 600; i++ {
lotsOfApps = append(lotsOfApps, resources.Application{
Name: "some-app-name",
GUID: fmt.Sprintf("some-app-guid-%d", i),
State: constant.ApplicationStarted,
})
}
fakeCloudControllerClient.GetApplicationsReturns(
lotsOfApps,
ccv3.Warnings{"get-apps-warning"},
nil,
)
})

It("batches the calls to the API for processes", func() {
Expect(fakeCloudControllerClient.GetProcessesCallCount()).To(Equal(3))

getProcessesArgs := fakeCloudControllerClient.GetProcessesArgsForCall(0)
Expect(len(getProcessesArgs[0].Values)).To(Equal(200))
getProcessesArgs = fakeCloudControllerClient.GetProcessesArgsForCall(1)
Expect(len(getProcessesArgs[0].Values)).To(Equal(200))
getProcessesArgs = fakeCloudControllerClient.GetProcessesArgsForCall(2)
Expect(len(getProcessesArgs[0].Values)).To(Equal(200))

Expect(len(summaries)).To(Equal(600))
})
It("batches the calls to the API for routes", func() {
Expect(fakeCloudControllerClient.GetRoutesCallCount()).To(Equal(3))

getArgs := fakeCloudControllerClient.GetRoutesArgsForCall(0)
Expect(len(getArgs[0].Values)).To(Equal(200))
getArgs = fakeCloudControllerClient.GetRoutesArgsForCall(1)
Expect(len(getArgs[0].Values)).To(Equal(200))
getArgs = fakeCloudControllerClient.GetRoutesArgsForCall(2)
Expect(len(getArgs[0].Values)).To(Equal(200))

Expect(len(summaries)).To(Equal(600))
})
})
})

When("getting the application fails", func() {
Expand Down

0 comments on commit a78e718

Please sign in to comment.