Skip to content

Commit b0c2cf7

Browse files
authored
fix body close in heartbeat and refactor (#668)
* fix: close body after heartbeat
1 parent b7355cf commit b0c2cf7

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

metrics/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Heartbeats will be enabled by default and they cant be disabled.
4343

4444
##### Configuration
4545

46-
* **service_heartbeat_type** (mandatory. ```http|grpc```) - though All services must be using grpc, for the
46+
* **service_heartbeat_type** (optional, default `tcp`, ```http|grpc|tcp```) - though All services must be using grpc, for the
4747
simplicity of heartbeat implementation, both http and gRPC-based heartbeat end points are supported.
4848

4949
* **heartbeat_endpoint** (mandatory ```must be a valid http|https|grpc url```) - It is service heartbeat endpoint.

metrics/clients.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// All rights reserved.
33
// <<add licence terms for code reuse>>
44

5-
// package for monitoring and reporting the daemon metrics
5+
// Package metrics for monitoring and reporting the daemon metrics
66
package metrics
77

88
import (
@@ -59,11 +59,12 @@ func callHTTPServiceHeartbeat(serviceURL string) ([]byte, error) {
5959
zap.L().Info("the service request failed with an error", zap.Error(err))
6060
return nil, err
6161
}
62+
defer response.Body.Close()
6263
if response.StatusCode != http.StatusOK {
6364
zap.L().Warn("wrong status code", zap.Int("StatusCode", response.StatusCode))
6465
return nil, errors.New("unexpected error with the service")
6566
}
66-
// Read the response
67+
// Read the response but ignore
6768
serviceHeartbeat, _ := io.ReadAll(response.Body)
6869
// Check if we got an empty response
6970
if string(serviceHeartbeat) == "" {
@@ -131,6 +132,7 @@ func callRegisterService(daemonID string, serviceURL string) (status bool) {
131132
zap.L().Info("unable to reach registration service", zap.Error(err))
132133
return false
133134
}
135+
defer response.Body.Close()
134136
// process the response and set the Authorization token
135137
daemonAuthorizationToken, status = getTokenFromResponse(response)
136138
zap.L().Debug("daemon authorization token", zap.Any("value", daemonAuthorizationToken))

metrics/heartbeat.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ func (state Status) String() string {
8383
func ValidateHeartbeatConfig(heartbeatType, heartbeatEndpoint string) error {
8484
// initialize the url state to false
8585
// check if the configured type is not supported
86-
if heartbeatType != "grpc" && heartbeatType != "http" && heartbeatType != "https" && heartbeatType != "none" && heartbeatType != "" {
87-
return fmt.Errorf("unrecognized heartbet service type : '%+v'", heartbeatType)
86+
if heartbeatType != "grpc" && heartbeatType != "http" && heartbeatType != "https" && heartbeatType != "none" && heartbeatType != "" && heartbeatType != "tcp" {
87+
return fmt.Errorf("unrecognized heartbeat service type : '%+v'", heartbeatType)
8888
}
8989

9090
// if the URLs are empty, or hbtype is None or empty, consider it as not configured
@@ -165,6 +165,8 @@ func GetHeartbeat(serviceEndpoint string, serviceHeartbeatURL string, heartbeatT
165165
zap.L().Debug("Get heartbeat", zap.String("serviceHeartbeatURL", serviceHeartbeatURL), zap.String("serviceHeartbeatBytes", string(serviceHeartbeatBytes)), zap.Error(err))
166166
case "none":
167167
fallthrough
168+
case "tcp":
169+
fallthrough
168170
case "":
169171
// trying to ping the service with serviceEndpoint
170172
err = tcpPingService(serviceEndpoint)

0 commit comments

Comments
 (0)