Skip to content

Commit 23309b4

Browse files
committed
fix: Fix interval delay on retryPing
Ping will happen before delay. This makes sure the first ping wont be delayed
1 parent 94ebcbc commit 23309b4

1 file changed

Lines changed: 29 additions & 18 deletions

File tree

‎cmd/main.go‎

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"flag"
55
"fmt"
6-
"log"
76
"os"
87
"os/signal"
98
"syscall"
@@ -44,12 +43,12 @@ func main() {
4443
pinger, err := ping.NewTimed(host, uint16(port), timeout)
4544

4645
if err != nil {
47-
log.Fatal(err)
46+
logger.Fatalw("Error creating ping client", "error", err)
4847
}
4948

5049
// Startup delay before the first ping (initial-delay)
5150
logger.Info("Starting up...")
52-
time.Sleep(intialDelay - interval)
51+
time.Sleep(intialDelay)
5352

5453
// Ping server until startup
5554
err = pingUntilStartup(attempts, interval, pinger, stop)
@@ -59,6 +58,9 @@ func main() {
5958
logger.Fatalw("Fatal Mincraft server. Exiting...", "error", err)
6059
}
6160

61+
// delay before next ping cycle
62+
time.Sleep(interval)
63+
6264
// Ping infinitely or until after a series of unsuccessful pings
6365
err = pingUntilFatal(attempts, interval, pinger, stop)
6466

@@ -72,10 +74,20 @@ func main() {
7274
// Will also signal the local Agones server with Ready()
7375
// Returns an error if the pings or singaling local Agones server fails
7476
func pingUntilStartup(attempts uint, interval time.Duration, pinger *ping.ServerPinger, stop chan struct{}) error {
75-
err := retryPing(attempts-1, interval, stop, pinger.ReadyPingWithTimeout)
77+
for {
78+
err := retryPing(attempts, interval, stop, pinger.ReadyPingWithTimeout)
7679

77-
if err != nil {
78-
return err
80+
if err == nil {
81+
break
82+
}
83+
84+
if _, ok := err.(ping.StartingUpErr); ok {
85+
logger.Errorw("Server still starting...", "attemptsLeft", attempts, "errorMessage", err.Error())
86+
} else {
87+
return err
88+
}
89+
90+
time.Sleep(interval)
7991
}
8092

8193
logger.Info("Server ready")
@@ -93,30 +105,29 @@ func pingUntilFatal(attempts uint, interval time.Duration, pinger *ping.ServerPi
93105
}
94106

95107
logger.Info("Server healthy")
108+
time.Sleep(interval)
96109
}
97110
}
98111

99112
// Retry wrapper function that will retry the given ping function with the specified attempts and intervals
100113
// until it dosen't return an error or until all attempts have been made
101114
func retryPing(attempts uint, interval time.Duration, stop chan struct{}, p func() error) error {
102-
ticker := time.NewTicker(interval)
103-
104115
for {
116+
117+
if err := p(); err != nil && attempts-1 > 0 {
118+
logger.Errorw(fmt.Sprintf("Unsuccessful ping. retrying in %v...", interval), "attemptsLeft", attempts-1, "errorMessage", err.Error())
119+
attempts--
120+
} else {
121+
return err
122+
}
123+
105124
select {
106125
case <-stop:
107126
return nil
108-
case <-ticker.C:
109-
110-
if err := p(); err != nil && attempts-1 > 0 {
111-
logger.Errorw(fmt.Sprintf("Unsuccessful ping. retrying in %v...", interval), "attemptsLeft", attempts-1, "errorMessage", err.Error())
112-
attempts--
113-
} else {
114-
return err
115-
}
116-
127+
case <-time.After(interval):
117128
}
118-
}
119129

130+
}
120131
}
121132

122133
func setupSignalHandler() chan struct{} {

0 commit comments

Comments
 (0)