Skip to content

Commit c22b4da

Browse files
jaer-tsunYongli Chen
authored andcommitted
Closing response bodies to prevent socket exhaustion. (#160)
1 parent 91b5e63 commit c22b4da

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

cns/dockerclient/dockerclient.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ func (dockerClient *DockerClient) NetworkExists(networkName string) error {
5555
return err
5656
}
5757

58+
defer res.Body.Close()
59+
5860
// network exists
5961
if res.StatusCode == 200 {
6062
log.Debugf("[Azure CNS] Network with name %v already exists. Docker return code: %v", networkName, res.StatusCode)
@@ -122,6 +124,9 @@ func (dockerClient *DockerClient) CreateNetwork(networkName string, nicInfo *imd
122124
log.Printf("[Azure CNS] Error received from http Post for docker network create %v", networkName)
123125
return err
124126
}
127+
128+
defer res.Body.Close()
129+
125130
if res.StatusCode != 201 {
126131
var createNetworkResponse DockerErrorResponse
127132
err = json.NewDecoder(res.Body).Decode(&createNetworkResponse)
@@ -158,6 +163,12 @@ func (dockerClient *DockerClient) DeleteNetwork(networkName string) error {
158163
req.Header.Set("Content-Type", "application/json; charset=utf-8")
159164
client := &http.Client{}
160165
res, err := client.Do(req)
166+
if err != nil {
167+
log.Printf("[Azure CNS] HTTP Post returned error %v", err.Error())
168+
return err
169+
}
170+
171+
defer res.Body.Close()
161172

162173
// network successfully deleted.
163174
if res.StatusCode == 204 {

cns/imdsclient/imdsclient.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ func (imdsClient *ImdsClient) GetNetworkContainerInfoFromHost(networkContainerID
2525
return nil, err
2626
}
2727

28+
defer jsonResponse.Body.Close()
29+
2830
log.Printf("[Azure CNS] Response received from Azure Host for NetworkManagement/interfaces: %v", jsonResponse.Body)
2931

3032
var response containerVersionJsonResponse
@@ -51,6 +53,8 @@ func (imdsClient *ImdsClient) GetPrimaryInterfaceInfoFromHost() (*InterfaceInfo,
5153
return nil, err
5254
}
5355

56+
defer resp.Body.Close()
57+
5458
log.Printf("[Azure CNS] Response received from NMAgent for get interface details: %v", resp.Body)
5559

5660
var doc xmlDocument

cns/ipamclient/ipamclient.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ func (ic *IpamClient) GetAddressSpace() (string, error) {
4545
return "", err
4646
}
4747

48+
defer res.Body.Close()
49+
4850
if res.StatusCode == 200 {
4951
var resp cnmIpam.GetDefaultAddressSpacesResponse
5052
err := json.NewDecoder(res.Body).Decode(&resp)
@@ -89,6 +91,8 @@ func (ic *IpamClient) GetPoolID(asID, subnet string) (string, error) {
8991
return "", err
9092
}
9193

94+
defer res.Body.Close()
95+
9296
if res.StatusCode == 200 {
9397
var resp cnmIpam.RequestPoolResponse
9498
err := json.NewDecoder(res.Body).Decode(&resp)
@@ -135,6 +139,8 @@ func (ic *IpamClient) ReserveIPAddress(poolID string, reservationID string) (str
135139
return "", err
136140
}
137141

142+
defer res.Body.Close()
143+
138144
if res.StatusCode == 200 {
139145
var reserveResp cnmIpam.RequestAddressResponse
140146

@@ -184,6 +190,8 @@ func (ic *IpamClient) ReleaseIPAddress(poolID string, reservationID string) erro
184190
return err
185191
}
186192

193+
defer res.Body.Close()
194+
187195
if res.StatusCode == 200 {
188196
var releaseResp cnmIpam.ReleaseAddressResponse
189197
err := json.NewDecoder(res.Body).Decode(&releaseResp)
@@ -227,6 +235,8 @@ func (ic *IpamClient) GetIPAddressUtilization(poolID string) (int, int, []string
227235
return 0, 0, nil, err
228236
}
229237

238+
defer res.Body.Close()
239+
230240
if res.StatusCode == 200 {
231241
var poolInfoResp cnmIpam.GetPoolInfoResponse
232242
err := json.NewDecoder(res.Body).Decode(&poolInfoResp)

telemetry/telemetry.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ func (reportMgr *ReportManager) SendReport() error {
165165
return fmt.Errorf("[Azure CNI] HTTP Post returned error %v", err)
166166
}
167167

168+
defer res.Body.Close()
169+
168170
if res.StatusCode != 200 {
169171
if res.StatusCode == 400 {
170172
return fmt.Errorf(`"[Azure CNI] HTTP Post returned statuscode %d.

0 commit comments

Comments
 (0)