Skip to content

Commit

Permalink
Add Timeout via Config 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. These timeouts are configurable via config.
  • Loading branch information
khrm committed Nov 17, 2020
1 parent 4bdeff8 commit e8cd876
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 7 deletions.
9 changes: 6 additions & 3 deletions cmd/eventlistenersink/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,12 @@ func main() {
})

srv := &http.Server{
Addr: fmt.Sprintf(":%s", sinkArgs.Port),
IdleTimeout: 120 * time.Second,
Handler: mux,
Addr: fmt.Sprintf(":%s", sinkArgs.Port),
ReadTimeout: sinkArgs.ELReadTimeOut * time.Second,
WriteTimeout: sinkArgs.ELWriteTimeOut * time.Second,
IdleTimeout: sinkArgs.ELIdleTimeOut * time.Second,
Handler: http.TimeoutHandler(mux,
sinkArgs.ELTimeOutHandler*time.Second, "EventListener Timeout!\n"),
}

if err := srv.ListenAndServe(); err != nil {
Expand Down
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-idletimeout", "120",
"-el-timeouthandler", "30",
"-period-seconds", "10",
"-failure-threshold", "1"
]
Expand Down
9 changes: 9 additions & 0 deletions docs/eventlisteners.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ using [Event Interceptors](#Interceptors).
- [EventListener Response](#eventlistener-response)
- [How does the EventListener work?](#how-does-the-eventlistener-work)
- [Examples](#examples)
- [Response Timeout](#response-timeout)
- [Multi-Tenant Concerns](#multi-tenant-concerns)
- [Multiple EventListeners (One EventListener Per Namespace)](#multiple-eventlisteners-one-eventlistener-per-namespace)
- [Multiple EventListeners (Multiple EventListeners per Namespace)](#multiple-eventlisteners-multiple-eventlisteners-per-namespace)
Expand Down Expand Up @@ -878,6 +879,14 @@ Follow [GitHub example](https://github.com/tektoncd/triggers/blob/master/example
For complete examples, see
[the examples folder](https://github.com/tektoncd/triggers/tree/master/examples).

## Response Timeout
Eventlistener sink timeout if EventListener isn't able to process the request within certain duration. The response timeout configuration is defined in [controller.yaml](../config/controller.yaml).
- `-el-readtimeout`: This define ReadTimeOut for sink server. Default value is 5 s.
- `-el-writetimeout`: This define WriteTimeout for sink server. Default value is 40s.
- `-el-idletimeout`: This define the IdleTimeout for sink server. Default value is 120s.
- `-el-timeouthandler`: This define the Timeout for Handler for sink server's route. Default value is 30s.


## Multi-Tenant Concerns

The EventListener is effectively an additional form of client into Tekton, versus what
Expand Down
1 change: 1 addition & 0 deletions pkg/interceptors/bitbucket/bitbucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"

triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
3 changes: 2 additions & 1 deletion pkg/interceptors/cel/cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ import (
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
"github.com/google/go-cmp/cmp"
triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
fakekubeclient "knative.dev/pkg/client/injection/kube/client/fake"
"knative.dev/pkg/logging"
rtesting "knative.dev/pkg/reconciler/testing"

triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
)

const testNS = "testing-ns"
Expand Down
1 change: 1 addition & 0 deletions pkg/interceptors/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"

triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
1 change: 1 addition & 0 deletions pkg/interceptors/gitlab/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"

triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
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 @@ -328,6 +340,10 @@ func (r *Reconciler) reconcileDeployment(ctx context.Context, logger *zap.Sugare
"-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",
Expand Down
4 changes: 4 additions & 0 deletions pkg/reconciler/v1alpha1/eventlistener/eventlistener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ func makeDeployment(ops ...func(d *appsv1.Deployment)) *appsv1.Deployment {
"-el-name", eventListenerName,
"-el-namespace", 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",
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("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.
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 e8cd876

Please sign in to comment.