fixed list slice error

This commit is contained in:
2021-06-24 18:44:09 +04:00
parent 4f10936664
commit 0bdcaca087
2 changed files with 13 additions and 38 deletions

View File

@@ -2,31 +2,13 @@
"ip_address": [ "ip_address": [
{ {
"ipv4": "0.0.0.0", "ipv4": "0.0.0.0",
"latency": 637095, "latency": 453359,
"download": 0, "download": 0,
"upload": 0 "upload": 0
}, },
{ {
"ipv4": "127.0.0.1", "ipv4": "127.0.0.1",
"latency": 420466, "latency": 410267,
"download": 0,
"upload": 0
},
{
"ipv4": "86.99.25.74",
"latency": 1217902,
"download": 0,
"upload": 0
},
{
"ipv4": "172.105.58.101",
"latency": 40664198,
"download": 0,
"upload": 0
},
{
"ipv4": "172.105.58.111",
"latency": 42065364,
"download": 0, "download": 0,
"upload": 0 "upload": 0
} }

View File

@@ -2,10 +2,13 @@ package p2p
// SpeedTest Runs a speed test and does updates IP tables accordingly // SpeedTest Runs a speed test and does updates IP tables accordingly
func (ip *IpAddresses)SpeedTest() error{ func (ip *IpAddresses)SpeedTest() error{
//temp variable to store elements IP addresses and other information
// of IP addresses that are pingable
var ActiveIP IpAddresses
// Index to remove from struct // Index to remove from struct
var RemoveIndex []int for _, value := range ip.IpAddress {
for i, value := range ip.IpAddress {
var err error var err error
//if len(ip.IpAddress) == 1 { //if len(ip.IpAddress) == 1 {
@@ -16,10 +19,6 @@ func (ip *IpAddresses)SpeedTest() error{
err = value.PingTest() err = value.PingTest()
if err != nil { if err != nil {
RemoveIndex = append(RemoveIndex, i)
// Record index to remove
//ip.IpAddress = append(ip.IpAddress[:i], ip.IpAddress[i+1:]...)
// Proceed to next element in the array
continue continue
} }
@@ -35,19 +34,12 @@ func (ip *IpAddresses)SpeedTest() error{
//} //}
//Set value to the list //Set value to the list
ip.IpAddress[i] = value
ActiveIP.IpAddress = append(ActiveIP.IpAddress, value)
} }
// Remove element from struct
for _, index := range RemoveIndex { ip.IpAddress = ActiveIP.IpAddress
// If there is only 1 element and that has to be
// removed
if len(ip.IpAddress) == 1 {
ip.IpAddress = nil
break
}
ip.IpAddress = append(ip.IpAddress[:index], ip.IpAddress[index+1:]...)
}
err := ip.WriteIpTable() err := ip.WriteIpTable()
if err != nil { if err != nil {
@@ -111,6 +103,7 @@ func LocalSpeedTestIpTable() error {
return nil return nil
} }
// Helper function to remove element from an array of a struct // Helper function to remove element from an array of a struct
//func remove(s []IpAddress, i int) []IpAddress { //func remove(s []IpAddress, i int) []IpAddress {
// s[len(s)-1], s[i] = s[i], s[len(s)-1] // s[len(s)-1], s[i] = s[i], s[len(s)-1]