1 回答

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
您的代碼有 2 個(gè)競(jìng)爭(zhēng)條件,并且可能永遠(yuǎn)不會(huì)正確報(bào)告錯(cuò)誤。
您需要等待 goroutines 完成,然后才能讀取它們操作的值,這很容易完成,如下所示sync.WaitGroup:
var nodeMetricsList *v1beta1.NodeMetricsList
var podMetricsList *v1beta1.PodMetricsList
var nodeMetricsListError, podMetricsListError error
var wg sync.WaitGroup
// Get node resource usage metrics
wg.Add(1)
go func() {
defer wg.Done()
nodeMetricsList, nodeMetricsListError = c.kubernetesClient.NodeMetricses()
}()
// Get pod resource usage metrics
wg.Add(1)
go func() {
defer wg.Done()
podMetricsList, podMetricsListError = c.kubernetesClient.PodMetricses()
}()
wg.Wait()
if nodeMetricsListError != nil {
log.Warn("Failed to get podList from Kubernetes", err)
return err
}
if podMetricsListError != nil {
log.Warn("Failed to get pod usage list from Kubernetes", err)
return err
}
fmt.Println("Hello, playground")
- 1 回答
- 0 關(guān)注
- 181 瀏覽
添加回答
舉報(bào)