Skip to content

Commit

Permalink
fix: close connection on any return from azure-vnet-telemetry (#2711)
Browse files Browse the repository at this point in the history
* move error handling code to defer

* invert conditional

* address linter issues

* add close connection on telemetry unmarshal error unit test

* Revert "add close connection on telemetry unmarshal error unit test"

This reverts commit e8447f9.
  • Loading branch information
QxBytes authored Apr 29, 2024
1 parent b8677ff commit 48801a1
Showing 1 changed file with 48 additions and 41 deletions.
89 changes: 48 additions & 41 deletions telemetry/telemetrybuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,55 +116,62 @@ func (tb *TelemetryBuffer) StartServer() error {
tb.connections = append(tb.connections, conn)
tb.mutex.Unlock()
go func() {
defer func() {
var index int
var value net.Conn
var found bool

tb.mutex.Lock()
defer tb.mutex.Unlock()

for index, value = range tb.connections {
if value == conn {
conn.Close()
found = true
break
}
}

if found {
tb.connections = remove(tb.connections, index)
}
}()

for {
reportStr, err := read(conn)
if err == nil {
var tmp map[string]interface{}
err = json.Unmarshal(reportStr, &tmp)
if err != nil {
return
}
var tmp map[string]interface{}
err = json.Unmarshal(reportStr, &tmp)
if err != nil {
if tb.logger != nil {
tb.logger.Error("StartServer: unmarshal error", zap.Error(err))
} else {
log.Logf("StartServer: unmarshal error:%v", err)
}
return
}
if _, ok := tmp["CniSucceeded"]; ok {
var cniReport CNIReport
err = json.Unmarshal([]byte(reportStr), &cniReport)
if err != nil {
if tb.logger != nil {
tb.logger.Error("StartServer: unmarshal error", zap.Error(err))
} else {
log.Logf("StartServer: unmarshal error:%v", err)
}
return
}
if _, ok := tmp["CniSucceeded"]; ok {
var cniReport CNIReport
json.Unmarshal([]byte(reportStr), &cniReport)
tb.data <- cniReport
} else if _, ok := tmp["Metric"]; ok {
var aiMetric AIMetric
json.Unmarshal([]byte(reportStr), &aiMetric)
tb.data <- aiMetric
} else {
if tb.logger != nil {
tb.logger.Info("StartServer: default", zap.Any("case", tmp))
} else {
log.Logf("StartServer: default case:%+v...", tmp)
}
tb.data <- cniReport
} else if _, ok := tmp["Metric"]; ok {
var aiMetric AIMetric
err = json.Unmarshal([]byte(reportStr), &aiMetric)
if err != nil {
return
}
tb.data <- aiMetric
} else {
var index int
var value net.Conn
var found bool

tb.mutex.Lock()
defer tb.mutex.Unlock()

for index, value = range tb.connections {
if value == conn {
conn.Close()
found = true
break
}
}

if found {
tb.connections = remove(tb.connections, index)
if tb.logger != nil {
tb.logger.Info("StartServer: default", zap.Any("case", tmp))
} else {
log.Logf("StartServer: default case:%+v...", tmp)
}

return
}
}
}()
Expand Down

0 comments on commit 48801a1

Please sign in to comment.