From 69eda8d8b5e615f112657b31d1c94064c2e36d6d Mon Sep 17 00:00:00 2001 From: Khurram Baig Date: Tue, 8 Sep 2020 16:30:07 +0530 Subject: [PATCH] Add Timeout for EventListener Server Added Timeout to avoid running out of file descriptor and leaking connections. --- cmd/eventlistenersink/main.go | 13 ++++++++- config/controller.yaml | 4 +++ .../v1alpha1/eventlistener/eventlistener.go | 16 +++++++++++ pkg/sink/initialization.go | 27 ++++++++++++++++--- 4 files changed, 56 insertions(+), 4 deletions(-) diff --git a/cmd/eventlistenersink/main.go b/cmd/eventlistenersink/main.go index 0b42c3e041..8d88bd9a29 100644 --- a/cmd/eventlistenersink/main.go +++ b/cmd/eventlistenersink/main.go @@ -20,6 +20,7 @@ import ( "fmt" "log" "net/http" + "time" "go.uber.org/zap" @@ -101,5 +102,15 @@ func main() { w.WriteHeader(200) fmt.Fprint(w, "ok") }) - logger.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", sinkArgs.Port), nil)) + + srv := &http.Server{ + Addr: fmt.Sprintf(":%s", sinkArgs.Port), + ReadTimeout: sinkArgs.ELReadTimeOut * time.Second, + WriteTimeout: sinkArgs.ELWriteTimeOut * time.Second, + IdleTimeout: sinkArgs.ELIdleTimeOut * time.Second, + Handler: http.TimeoutHandler(http.DefaultServeMux, + sinkArgs.ELTimeOutHandler*time.Second, "EventListener Timeout!\n"), + } + + logger.Fatal(srv.ListenAndServe()) } diff --git a/config/controller.yaml b/config/controller.yaml index d496df19eb..085a03a3af 100644 --- a/config/controller.yaml +++ b/config/controller.yaml @@ -57,6 +57,10 @@ spec: "-stderrthreshold", "INFO", "-el-image", "ko://github.com/tektoncd/triggers/cmd/eventlistenersink", "-el-port", "8080", + "-el-readtimeout", "5", + "-el-writetimeout", "40", + "-el-idletimeout", "120", + "-el-timeouthandler", "30", "-period-seconds", "10", "-failure-threshold", "1" ] diff --git a/pkg/reconciler/v1alpha1/eventlistener/eventlistener.go b/pkg/reconciler/v1alpha1/eventlistener/eventlistener.go index c18e1b4f9e..4e515cab62 100644 --- a/pkg/reconciler/v1alpha1/eventlistener/eventlistener.go +++ b/pkg/reconciler/v1alpha1/eventlistener/eventlistener.go @@ -67,6 +67,18 @@ var ( // ElPort defines the port for the EventListener to listen on ElPort = flag.Int("el-port", 8080, "The container port for the EventListener to listen on.") + // ELReadTimeOut defines the read timeout for EventListener Server + ELReadTimeOut = flag.Int64("el-readtimeout", 5, + "The read timeout for EventListener Server.") + // ELWriteTimeOut defines the write timeout for EventListener Server + ELWriteTimeOut = flag.Int64("el-writetimeout", 40, + "The write timeout for EventListener Server.") + // ELIdleTimeOut defines the read timeout for EventListener Server + ELIdleTimeOut = flag.Int64("el-idletimeout", 30, + "The idle timeout for EventListener Server.") + // ELTimeOutHandler defines the timeout for Timeout Handler of EventListener Server + ELTimeOutHandler = flag.Int64("el-timeouthandler", 5, + "The timeout for Timeout Handler of EventListener Server.") // PeriodSeconds defines Period Seconds for the EventListener Liveness and Readiness Probes PeriodSeconds = flag.Int("period-seconds", 10, "The Period Seconds for the EventListener Liveness and Readiness Probes.") @@ -290,6 +302,10 @@ func (r *Reconciler) reconcileDeployment(logger *zap.SugaredLogger, el *v1alpha1 "-el-name", el.Name, "-el-namespace", el.Namespace, "-port", strconv.Itoa(*ElPort), + "-readtimeout", strconv.FormatInt(*ELReadTimeOut, 10), + "-writetimeout", strconv.FormatInt(*ELWriteTimeOut, 10), + "-idletimeout", strconv.FormatInt(*ELIdleTimeOut, 10), + "-timeouthandler", strconv.FormatInt(*ELTimeOutHandler, 10), }, VolumeMounts: []corev1.VolumeMount{{ Name: "config-logging", diff --git a/pkg/sink/initialization.go b/pkg/sink/initialization.go index 8454018772..cd85366275 100644 --- a/pkg/sink/initialization.go +++ b/pkg/sink/initialization.go @@ -18,6 +18,7 @@ package sink import ( "flag" + "time" triggersclientset "github.com/tektoncd/triggers/pkg/client/clientset/versioned" "golang.org/x/xerrors" @@ -41,6 +42,14 @@ var ( "The namespace of the EventListener resource for this sink.") portFlag = flag.String("port", "", "The port for the EventListener sink to listen on.") + elReadTimeOut = flag.Int64("readtimeout", 5, + "The read timeout for EventListener Server.") + elWriteTimeOut = flag.Int64("writetimeout", 40, + "The write timeout for EventListener Server.") + elIdleTimeOut = flag.Int64("idletimeout", 30, + "The idle timeout for EventListener Server.") + elTimeOutHandler = flag.Int64("timeouthandler", 5, + "The timeout for Timeout Handler of EventListener Server.") ) // Args define the arguments for Sink. @@ -51,6 +60,14 @@ type Args struct { ElNamespace string // Port is the port the Sink should listen on. Port string + // ELReadTimeOut defines the read timeout for EventListener Server + ELReadTimeOut time.Duration + // ELWriteTimeOut defines the write timeout for EventListener Server + ELWriteTimeOut time.Duration + // ELIdleTimeOut defines the read timeout for EventListener Server + ELIdleTimeOut time.Duration + // ELTimeOutHandler defines the timeout for Timeout Handler of EventListener Server + ELTimeOutHandler time.Duration } // Clients define the set of client dependencies Sink requires. @@ -73,9 +90,13 @@ func GetArgs() (Args, error) { return Args{}, xerrors.Errorf("-%s arg not found", port) } return Args{ - ElName: *nameFlag, - ElNamespace: *namespaceFlag, - Port: *portFlag, + ElName: *nameFlag, + ElNamespace: *namespaceFlag, + Port: *portFlag, + ELReadTimeOut: time.Duration(*elReadTimeOut), + ELWriteTimeOut: time.Duration(*elWriteTimeOut), + ELIdleTimeOut: time.Duration(*elIdleTimeOut), + ELTimeOutHandler: time.Duration(*elTimeOutHandler), }, nil }