Browse Source

auto update script executed

Toby Chui 1 year ago
parent
commit
e43c37a7a3
1 changed files with 23 additions and 9 deletions
  1. 23 9
      mod/netstat/netstat.go

+ 23 - 9
mod/netstat/netstat.go

@@ -38,15 +38,6 @@ type NetStatBuffers struct {
 
 // Get a new network statistic buffers
 func NewNetStatBuffer(recordCount int) (*NetStatBuffers, error) {
-	//Get the initial measurements of netstats
-	rx, tx, err := GetNetworkInterfaceStats()
-	if err != nil {
-		return nil, err
-	}
-	currnetNetSpec := RawFlowStat{
-		RX: rx,
-		TX: tx,
-	}
 
 	//Flood fill the stats with 0
 	initialStats := []*FlowStat{}
@@ -63,6 +54,11 @@ func NewNetStatBuffer(recordCount int) (*NetStatBuffers, error) {
 	//Setup a stop channel
 	stopCh := make(chan bool)
 
+	currnetNetSpec := RawFlowStat{
+		RX: 0,
+		TX: 0,
+	}
+
 	thisNetBuffer := NetStatBuffers{
 		StatRecordCount: recordCount,
 		PreviousStat:    &currnetNetSpec,
@@ -71,6 +67,20 @@ func NewNetStatBuffer(recordCount int) (*NetStatBuffers, error) {
 		EventTicker:     ticker,
 	}
 
+	//Get the initial measurements of netstats
+	go func(n *NetStatBuffers) {
+		rx, tx, err := GetNetworkInterfaceStats()
+		if err != nil {
+			log.Println("Unable to get NIC stats: ", err.Error())
+		}
+
+		n.PreviousStat = &RawFlowStat{
+			RX: rx,
+			TX: tx,
+		}
+
+	}(&thisNetBuffer)
+
 	// Update the buffer every second
 	go func(n *NetStatBuffers) {
 		for {
@@ -79,6 +89,10 @@ func NewNetStatBuffer(recordCount int) (*NetStatBuffers, error) {
 				return
 
 			case <-ticker.C:
+				if n.PreviousStat.RX == 0 && n.PreviousStat.TX == 0 {
+					//Initiation state is still not done. Ignore request
+					return
+				}
 				// Get the latest network interface stats
 				rx, tx, err := GetNetworkInterfaceStats()
 				if err != nil {