Skip to content

Commit

Permalink
filehandles: closed resp.body in cloud sync
Browse files Browse the repository at this point in the history
  • Loading branch information
24inch committed Jan 23, 2024
1 parent afd2d6a commit 2d054f7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions clouds/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ func (resp *fetchResponse) json(data interface{}) error {
return err
}

func (resp *fetchResponse) Close() error {
if resp.body == nil {
return nil
}
return resp.body.Close()
}

func (resp *fetchResponse) text() string {
if resp.body == nil {
return ""
Expand Down
8 changes: 8 additions & 0 deletions clouds/initial_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func (cloud *Cloud) authenticate() int {
},
body: bytes.NewReader(body),
})
defer resp.Close()

if !resp.ok {
if resp.status <= 0 {
cloud.Printf("Can not connect to server.\n%s", resp.status, resp.statusText)
Expand Down Expand Up @@ -89,6 +91,8 @@ func (cloud *Cloud) initialSync() int {
},
body: bytes.NewReader(body),
})
defer resp.Close()

if resp.status == http.StatusUnprocessableEntity {
log.Printf("[UP ] Gateway already registered.")
// cloud.Printf("Gateway already registered.", 200)
Expand Down Expand Up @@ -124,10 +128,12 @@ func (cloud *Cloud) initialSync() int {
"Authorization": cloud.auth,
},
})

switch resp.status {
case http.StatusNotFound:
// log.Printf("[UP ] Device %q not found.", device.ID)
cloud.flag(Entity{device.ID, "", ""}, ActionCreate, noTime, meta)
resp.Close()

case http.StatusOK:
var device2 v2Device
Expand Down Expand Up @@ -202,6 +208,8 @@ func (cloud *Cloud) initialSync() int {

default:
cloud.Printf("Communication Error\nUnexpected response: %s:\n%s", 500, resp.statusText, resp.text())
resp.Close()

return resp.status
}
}
Expand Down
11 changes: 11 additions & 0 deletions clouds/persistent_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ func (cloud *Cloud) postDevice(device *edge.Device) (int, error) {
},
body: bytes.NewReader(body),
})
defer resp.Close()

if !resp.ok {
err := fmt.Errorf("Unable to push device.\nStatus: %s\n%s", resp.statusText, strings.TrimSpace(resp.text()))
Expand All @@ -365,6 +366,7 @@ func (cloud *Cloud) postDeviceName(deviceID string, name string) (int, error) {
},
body: bytes.NewReader([]byte(name)),
})
defer resp.Close()

if !resp.ok {
err := fmt.Errorf("Unable to change device name.\nStatus: %s\n%s", resp.statusText, strings.TrimSpace(resp.text()))
Expand All @@ -380,6 +382,7 @@ func (cloud *Cloud) postDeviceName(deviceID string, name string) (int, error) {
},
body: bytes.NewReader([]byte(name)),
})
defer resp.Close()

if !resp.ok {
err := fmt.Errorf("Unable to change device name.\nStatus: %s\n%s", resp.statusText, strings.TrimSpace(resp.text()))
Expand All @@ -406,6 +409,8 @@ func (cloud *Cloud) postSensor(deviceID string, sensor *edge.Sensor) (int, error
},
body: bytes.NewReader(body),
})
defer resp.Close()

if !resp.ok {
err := fmt.Errorf("Unable to push sensor.\nStatus: %s\n%s", resp.statusText, strings.TrimSpace(resp.text()))
return resp.status, err
Expand All @@ -425,6 +430,7 @@ func (cloud *Cloud) postSensorName(deviceID string, sensorID string, name string
},
body: bytes.NewReader([]byte(name)),
})
defer resp.Close()

if !resp.ok {
err := fmt.Errorf("Unable to change sensor name.\nStatus: %s\n%s", resp.statusText, strings.TrimSpace(resp.text()))
Expand All @@ -449,6 +455,8 @@ func (cloud *Cloud) postActuator(deviceID string, actuator *edge.Actuator) (int,
},
body: bytes.NewReader(body),
})
defer resp.Close()

if !resp.ok {
err := fmt.Errorf("Unable to push actuator.\nStatus: %s\n%s", resp.statusText, strings.TrimSpace(resp.text()))
return resp.status, err
Expand All @@ -468,6 +476,7 @@ func (cloud *Cloud) postActuatorName(deviceID string, actuatorID string, name st
},
body: bytes.NewReader([]byte(name)),
})
defer resp.Close()

if !resp.ok {
err := fmt.Errorf("Unable to change actuator name.\nStatus: %s\n%s", resp.statusText, strings.TrimSpace(resp.text()))
Expand Down Expand Up @@ -517,6 +526,8 @@ func (cloud *Cloud) postValues(deviceID string, sensorID string, values edge.Val
},
body: &buf,
})
defer resp.Close()

if !resp.ok {
err := fmt.Errorf("Unable to push values.\nStatus: %s\n%s", resp.statusText, strings.TrimSpace(resp.text()))
return noTime, 0, resp.status, err
Expand Down

0 comments on commit 2d054f7

Please sign in to comment.