From d401b71262d3653310e2374f16edd07e39a09551 Mon Sep 17 00:00:00 2001 From: Cyril Duez Date: Fri, 27 May 2016 10:40:12 +0200 Subject: [PATCH] Only start the webhook server with at least one webhook input. --- agent/agent.go | 5 +++-- internal/webserver/webserver.go | 15 +++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index e0cc82e72..725d79bc2 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -329,8 +329,9 @@ func (a *Agent) Run(shutdown chan struct{}) error { metricC := make(chan telegraf.Metric, 10000) webserver := webserver.NewWebserver(a.Config.Agent.WebhookServiceAddress) + // Handle special input plugin: Start any ServiceInput and + // Register any WebhookInput for _, input := range a.Config.Inputs { - // Start service of any ServicePlugins switch p := input.Input.(type) { case telegraf.ServiceInput: acc := createAccumulatorForInput(a, input, metricC) @@ -347,9 +348,9 @@ func (a *Agent) Run(shutdown chan struct{}) error { input.Name, err.Error()) return err } + webserver.StartOnce() } } - webserver.Start() // Round collection to nearest interval by sleeping if a.Config.Agent.RoundInterval { diff --git a/internal/webserver/webserver.go b/internal/webserver/webserver.go index 5f68e98e0..f2008e998 100644 --- a/internal/webserver/webserver.go +++ b/internal/webserver/webserver.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "net/http" + "sync" "github.com/gorilla/mux" ) @@ -11,21 +12,23 @@ import ( type Webserver struct { ServiceAddress string Router *mux.Router + onceStart sync.Once } func NewWebserver(serviceAddress string) *Webserver { return &Webserver{Router: mux.NewRouter(), ServiceAddress: serviceAddress} } -func (wb *Webserver) Listen() { +func (wb *Webserver) listen() { + log.Printf("Started the webhook server on %s\n", wb.ServiceAddress) err := http.ListenAndServe(fmt.Sprintf("%s", wb.ServiceAddress), wb.Router) if err != nil { - log.Printf("Error starting server: %v", err) + log.Printf("Error starting webhook server: %v", err) } } -func (wb *Webserver) Start() error { - go wb.Listen() - log.Printf("Started the webhook server on %s\n", wb.ServiceAddress) - return nil +func (wb *Webserver) StartOnce() { + wb.onceStart.Do(func() { + go wb.listen() + }) }