added timeout to ping

This commit is contained in:
2022-07-05 12:02:14 +02:00
parent 85a0f0a26c
commit 1599f3b537

13
main.go
View File

@@ -85,17 +85,20 @@ func (m UploadManager) clearTC() {
} }
func doPing(host string, count int, interval time.Duration) (*ping.Statistics, error) { func doPing(host string, count int, interval time.Duration) (*ping.Statistics, error) {
log.Debugf("[PING] starting to ping %s with %d packages", host, count)
pinger, err := ping.NewPinger(host) pinger, err := ping.NewPinger(host)
if err != nil { if err != nil {
return nil, err return nil, err
} }
pinger.SetPrivileged(true) pinger.SetPrivileged(true)
pinger.Timeout = time.Second * time.Duration(count) * 2
pinger.Count = count pinger.Count = count
pinger.Interval = interval pinger.Interval = interval
err = pinger.Run() err = pinger.Run()
if err != nil { if err != nil {
return nil, err return nil, err
} }
log.Debugf("[PING] %s: %s±%s", host, pinger.Statistics().AvgRtt, pinger.Statistics().StdDevRtt)
return pinger.Statistics(), nil return pinger.Statistics(), nil
} }
@@ -112,8 +115,6 @@ func (m UploadManager) pingWorker() error {
continue continue
} }
log.Debugf("[PING] %s±%s", stats.AvgRtt, stats.StdDevRtt)
if stats.AvgRtt.Milliseconds() >= conf.ThrottlePingThreshold { if stats.AvgRtt.Milliseconds() >= conf.ThrottlePingThreshold {
m.LastOver = time.Now() m.LastOver = time.Now()
up, err := m.Upload() up, err := m.Upload()
@@ -121,7 +122,7 @@ func (m UploadManager) pingWorker() error {
return err return err
} }
log.Infof("Ping %s over threshold detected, starting extensive pinging...", stats.AvgRtt) log.Infof("ping %s over threshold detected, starting extended pings...", stats.AvgRtt)
for stats.AvgRtt.Milliseconds() >= conf.ThrottlePingThreshold && m.isBWInRange(up-conf.Bandwidth.Step) { for stats.AvgRtt.Milliseconds() >= conf.ThrottlePingThreshold && m.isBWInRange(up-conf.Bandwidth.Step) {
stats, err = doPing(conf.Host, conf.ConformationPPP, time.Second) stats, err = doPing(conf.Host, conf.ConformationPPP, time.Second)
if err != nil { if err != nil {
@@ -146,13 +147,14 @@ func (m UploadManager) pingWorker() error {
return err return err
} }
if m.isBWInRange(up + conf.Bandwidth.Step) { if m.isBWInRange(up + conf.Bandwidth.Step) {
log.Infof("Short ping %s, trying to increase upload TC %d->%d", stats.AvgRtt, up, up+conf.Bandwidth.Step) log.Infof("short ping %s, trying to increase upload TC %d->%d", stats.AvgRtt, up, up+conf.Bandwidth.Step)
up, err = m.SetUpload(up + conf.Bandwidth.Step) up, err = m.SetUpload(up + conf.Bandwidth.Step)
if err != nil { if err != nil {
return err return err
} }
m.LastOver = time.Now() m.LastOver = time.Now()
log.Infof("upload TC increased to %d, measuring latency", up)
stats, err = doPing(conf.Host, conf.ConformationPPP, time.Second) stats, err = doPing(conf.Host, conf.ConformationPPP, time.Second)
if err != nil { if err != nil {
log.Warningf("ping to %s failed: %v", conf.Host, err) log.Warningf("ping to %s failed: %v", conf.Host, err)
@@ -167,11 +169,10 @@ func (m UploadManager) pingWorker() error {
return err return err
} }
} else { } else {
log.Infof("Extended ping %s seems stable", stats.AvgRtt) log.Infof("extended ping %s seems stable", stats.AvgRtt)
} }
} }
} }
log.Debugf("sleep for %s", time.Duration(conf.Interval)*time.Second)
time.Sleep(time.Duration(conf.Interval) * time.Second) time.Sleep(time.Duration(conf.Interval) * time.Second)
} }
} }