Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix shutdown problems in ping application. #2141

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions cmd/ping/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ var (
func main() {
ctlConf := parseEnvFlags()
if err := ctlConf.validate(); err != nil {
logger.WithError(err).Fatal("could not create controller from environment or flags")
logger.WithError(err).Fatal("Could not create controller from environment or flags")
}

logger.WithField("version", pkg.Version).WithField("featureGates", runtime.EncodeFeatures()).
WithField("ctlConf", ctlConf).Info("starting ping...")
WithField("ctlConf", ctlConf).Info("Starting ping...")

ctx := signals.NewSigKillContext()

Expand All @@ -61,7 +61,7 @@ func main() {
defer cancel()

<-ctx.Done()
logger.Info("shutting down...")
logger.Info("Shutting down...")
}

func serveUDP(ctx context.Context, ctlConf config) *udpServer {
Expand All @@ -85,22 +85,25 @@ func serveHTTP(ctlConf config, h healthcheck.Handler) func() {
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if _, err := w.Write([]byte(ctlConf.HTTPResponse)); err != nil {
w.WriteHeader(http.StatusInternalServerError)
logger.WithError(err).Error("error responding to http request")
logger.WithError(err).Error("Error responding to http request")
}
})

go func() {
logger.Info("starting HTTP Server...")
logger.WithError(srv.ListenAndServe()).Fatal("could not start HTTP server")
logger.Info("Starting HTTP Server...")
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
logger.WithError(err).Fatal("Could not start HTTP server")
}
}()

return func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

if err := srv.Shutdown(ctx); err != nil {
logger.WithError(err).Fatal("could not shut down HTTP server")
logger.WithError(err).Fatal("Could not shut down HTTP server")
}
logger.Info("HTTP server was gracefully shut down")
}
}

Expand Down
16 changes: 10 additions & 6 deletions cmd/ping/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"math"
"net"
"os"
"sync"
"time"

Expand Down Expand Up @@ -71,11 +72,11 @@ func newUDPServer(rateLimit rate.Limit) *udpServer {
func (u *udpServer) run(ctx context.Context) {
u.healthy()

logger.Info("starting UDP server")
logger.Info("Starting UDP server")
var err error
u.conn, err = net.ListenPacket("udp", ":8080")
if err != nil {
logger.WithError(err).Fatal("could not start udp server")
logger.WithError(err).Fatal("Could not start udp server")
}

go func() {
Expand Down Expand Up @@ -111,7 +112,10 @@ func (u *udpServer) readWriteLoop(ctx context.Context) {
b := make([]byte, 1024)
_, sender, err := u.conn.ReadFrom(b)
if err != nil {
u.logger.WithError(err).Error("error reading udp packet")
if ctx.Err() != nil && err == os.ErrClosed {
return
}
u.logger.WithError(err).Error("Error reading udp packet")
continue
}
go u.rateLimitedEchoResponse(b, sender)
Expand All @@ -137,17 +141,17 @@ func (u *udpServer) rateLimitedEchoResponse(b []byte, sender net.Addr) {
if vis.limit.Allow() {
b = bytes.TrimRight(b, "\x00")
if _, err := u.conn.WriteTo(b, sender); err != nil {
u.logger.WithError(err).Error("error sending returning udp packet")
u.logger.WithError(err).Error("Error sending returning udp packet")
}
} else {
logger.WithField("addr", sender.String()).Warn("rate limited. No response sent.")
logger.WithField("addr", sender.String()).Warn("Rate limited. No response sent")
roberthbailey marked this conversation as resolved.
Show resolved Hide resolved
}
}

// close closes and shutdown the udp server
func (u *udpServer) close() {
if err := u.conn.Close(); err != nil {
logger.WithError(err).Error("error closing udp connection")
logger.WithError(err).Error("Error closing udp connection")
}
}

Expand Down