Skip to content

Commit

Permalink
Add Timeout for EventListener Server
Browse files Browse the repository at this point in the history
Added Timeout to avoid running out of file descriptor and leaking connections.
  • Loading branch information
khrm committed Sep 8, 2020
1 parent 45daf11 commit 60bda57
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
13 changes: 12 additions & 1 deletion cmd/eventlistenersink/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"log"
"net/http"
"time"

"go.uber.org/zap"

Expand Down Expand Up @@ -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())
}
4 changes: 4 additions & 0 deletions config/controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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-idleimeout", "120",
"-el-timeouthandler", "30",
"-period-seconds", "10",
"-failure-threshold", "1"
]
Expand Down
16 changes: 16 additions & 0 deletions pkg/reconciler/v1alpha1/eventlistener/eventlistener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -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),
"-el-readtimeout", strconv.FormatInt(*ELReadTimeOut, 10),
"-el-writetimeout", strconv.FormatInt(*ELWriteTimeOut, 10),
"-el-idletimeout", strconv.FormatInt(*ELIdleTimeOut, 10),
"-el-timeouthandler", strconv.FormatInt(*ELTimeOutHandler, 10),
},
VolumeMounts: []corev1.VolumeMount{{
Name: "config-logging",
Expand Down
27 changes: 24 additions & 3 deletions pkg/sink/initialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package sink

import (
"flag"
"time"

triggersclientset "github.com/tektoncd/triggers/pkg/client/clientset/versioned"
"golang.org/x/xerrors"
Expand All @@ -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("el-readtimeout", 5,
"The read timeout for EventListener Server.")
elWriteTimeOut = flag.Int64("el-writetimeout", 40,
"The write timeout for EventListener Server.")
elIdleTimeOut = flag.Int64("el-idletimeout", 30,
"The idle timeout for EventListener Server.")
elTimeOutHandler = flag.Int64("el-timeouthandler", 5,
"The timeout for Timeout Handler of EventListener Server.")
)

// Args define the arguments for Sink.
Expand All @@ -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.
Expand All @@ -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
}

Expand Down

0 comments on commit 60bda57

Please sign in to comment.