Skip to content

Commit 820bd88

Browse files
authored
Merge pull request #256 from atc0005/i245-paging-support-explicit-response-closure
Explicitly close response bodys of paged requests
2 parents ca69d3a + 2a09ce8 commit 820bd88

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

internal/rsat/organizations.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,7 @@ func GetOrganizations(ctx context.Context, client *APIClient) ([]Organization, e
112112
apiURL,
113113
client.AuthInfo.ReadLimit,
114114
)
115-
// Make sure that we close the response body once we're done with it
116-
defer func() {
117-
if closeErr := response.Body.Close(); closeErr != nil {
118-
logger.Error().Err(closeErr).Msg("error closing response body")
119-
}
120-
}()
115+
121116
var orgsQueryResp OrganizationsResponse
122117
decodeErr := decode(&orgsQueryResp, response.Body, logger, apiURL, client.AuthInfo.ReadLimit)
123118
if decodeErr != nil {
@@ -128,6 +123,14 @@ func GetOrganizations(ctx context.Context, client *APIClient) ([]Organization, e
128123
Str("api_endpoint", apiURL).
129124
Msg("Successfully decoded JSON data")
130125

126+
// Close the response body once we're done with it. We explicitly
127+
// close here vs deferring via closure to prevent accumulating client
128+
// connections to the API if we need to perform multiple paged
129+
// requests.
130+
if closeErr := response.Body.Close(); closeErr != nil {
131+
logger.Error().Err(closeErr).Msg("error closing response body")
132+
}
133+
131134
numNewOrgs := len(orgsQueryResp.Organizations)
132135
numCollectedOrgs := len(allOrgs)
133136
numOrgsRemaining := orgsQueryResp.Subtotal - numCollectedOrgs

internal/rsat/syncplans.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -436,13 +436,6 @@ func getOrgSyncPlans(ctx context.Context, client *APIClient, org Organization) (
436436
return nil, respErr
437437
}
438438

439-
// Make sure that we close the response body once we're done with it
440-
defer func() {
441-
if closeErr := response.Body.Close(); closeErr != nil {
442-
subLogger.Error().Err(closeErr).Msg("error closing response body")
443-
}
444-
}()
445-
446439
subLogger.Debug().Msgf(
447440
"Decoding JSON data from %q using a limit of %d bytes",
448441
apiURL,
@@ -455,17 +448,25 @@ func getOrgSyncPlans(ctx context.Context, client *APIClient, org Organization) (
455448
return nil, decodeErr
456449
}
457450

451+
subLogger.Debug().
452+
Str("api_endpoint", apiURL).
453+
Msg("Successfully decoded JSON data")
454+
455+
// Close the response body once we're done with it. We explicitly
456+
// close here vs deferring via closure to prevent accumulating client
457+
// connections to the API if we need to perform multiple paged
458+
// requests.
459+
if closeErr := response.Body.Close(); closeErr != nil {
460+
subLogger.Error().Err(closeErr).Msg("error closing response body")
461+
}
462+
458463
// Annotate Sync Plans with specific Org values for convenience.
459464
for i := range syncPlansQueryResp.SyncPlans {
460465
syncPlansQueryResp.SyncPlans[i].OrganizationName = org.Name
461466
syncPlansQueryResp.SyncPlans[i].OrganizationLabel = org.Label
462467
syncPlansQueryResp.SyncPlans[i].OrganizationTitle = org.Title
463468
}
464469

465-
subLogger.Debug().
466-
Str("api_endpoint", apiURL).
467-
Msg("Successfully decoded JSON data")
468-
469470
numNewSyncPlans := len(syncPlansQueryResp.SyncPlans)
470471
numCollectedSyncPlans := len(allSyncPlans)
471472
numSyncPlansRemaining := syncPlansQueryResp.Subtotal - numCollectedSyncPlans

0 commit comments

Comments
 (0)