diff --git a/cmd/components.go b/cmd/components.go index 6f9febfc..d326e0a1 100644 --- a/cmd/components.go +++ b/cmd/components.go @@ -36,10 +36,8 @@ func StartStopComponents(r *reporter.Reporter, daemonComponent daemon.Component, Str("version", Version).Str("build-date", BuildDate). Msg("akvorado has started") - select { - case <-daemonComponent.Terminated(): - r.Info().Msg("stopping all components") - } + <-daemonComponent.Terminated() + r.Info().Msg("stopping all components") return nil } diff --git a/common/daemon/root.go b/common/daemon/root.go index e1509e4c..4caec74e 100644 --- a/common/daemon/root.go +++ b/common/daemon/root.go @@ -57,19 +57,17 @@ func (c *realComponent) Start() error { // Listen for tombs for _, t := range c.tombs { go func(t tombWithOrigin) { - select { - case <-t.tomb.Dying(): - if t.tomb.Err() == nil { - c.r.Debug(). - Str("component", t.origin). - Msg("component shutting down, quitting") - } else { - c.r.Err(t.tomb.Err()). - Str("component", t.origin). - Msg("component error, quitting") - } - c.Terminate() + <-t.tomb.Dying() + if t.tomb.Err() == nil { + c.r.Debug(). + Str("component", t.origin). + Msg("component shutting down, quitting") + } else { + c.r.Err(t.tomb.Err()). + Str("component", t.origin). + Msg("component error, quitting") } + c.Terminate() }(t) } // On signal, terminate diff --git a/common/daemon/tests.go b/common/daemon/tests.go index a60fa7dd..f9ea916f 100644 --- a/common/daemon/tests.go +++ b/common/daemon/tests.go @@ -40,5 +40,4 @@ func (c *MockComponent) Stop() error { // Track does nothing func (c *MockComponent) Track(t *tomb.Tomb, who string) { - return } diff --git a/common/http/root.go b/common/http/root.go index 07d60d5c..b7859b10 100644 --- a/common/http/root.go +++ b/common/http/root.go @@ -100,26 +100,6 @@ func New(r *reporter.Reporter, configuration Configuration, dependencies Depende return &c, nil } -type responseWriter struct { - http.ResponseWriter - status int - wroteHeader bool -} - -func (rw *responseWriter) Status() int { - return rw.status -} - -func (rw *responseWriter) WriteHeader(code int) { - if rw.wroteHeader { - return - } - rw.status = code - rw.ResponseWriter.WriteHeader(code) - rw.wroteHeader = true - return -} - // AddHandler registers a new handler for the web server func (c *Component) AddHandler(location string, handler http.Handler) { l := c.r.With().Str("handler", location).Logger() @@ -175,16 +155,14 @@ func (c *Component) Start() error { // Gracefully stop when asked to c.t.Go(func() error { - select { - case <-c.t.Dying(): - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - if err := server.Shutdown(ctx); err != nil { - c.r.Err(err).Msg("unable to shutdown HTTP server") - return fmt.Errorf("unable to shutdown HTTP server: %w", err) - } - return nil + <-c.t.Dying() + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + if err := server.Shutdown(ctx); err != nil { + c.r.Err(err).Msg("unable to shutdown HTTP server") + return fmt.Errorf("unable to shutdown HTTP server: %w", err) } + return nil }) return nil } diff --git a/common/reporter/healthcheck_test.go b/common/reporter/healthcheck_test.go index 2e48eb86..68401988 100644 --- a/common/reporter/healthcheck_test.go +++ b/common/reporter/healthcheck_test.go @@ -73,10 +73,8 @@ func TestHealthcheckCancelContext(t *testing.T) { return reporter.HealthcheckResult{reporter.HealthcheckOK, "all well"} }) r.RegisterHealthcheck("hc2", func(ctx context.Context) reporter.HealthcheckResult { - select { - case <-ctx.Done(): - return reporter.HealthcheckResult{reporter.HealthcheckError, "I am late, sorry"} - } + <-ctx.Done() + return reporter.HealthcheckResult{reporter.HealthcheckError, "I am late, sorry"} }) ctx, cancel := context.WithCancel(context.Background()) go func() { diff --git a/console/authentication/handlers.go b/console/authentication/handlers.go index fa889308..8fc1faed 100644 --- a/console/authentication/handlers.go +++ b/console/authentication/handlers.go @@ -14,16 +14,12 @@ import ( "io/fs" "math/rand" "net/http" - "regexp" "github.com/gin-gonic/gin" ) -var ( - //go:embed data/avatars - avatarParts embed.FS - avatarRegexp = regexp.MustCompile(`^([a-z]+)_([0-9]+)\.png$`) -) +//go:embed data/avatars +var avatarParts embed.FS // UserInfoHandlerFunc returns the information about the currently logged user. func (c *Component) UserInfoHandlerFunc(gc *gin.Context) { diff --git a/console/clickhouse.go b/console/clickhouse.go index 2920c208..6699ba9d 100644 --- a/console/clickhouse.go +++ b/console/clickhouse.go @@ -120,11 +120,6 @@ func templateEscape(input string) string { return strings.ReplaceAll(input, `{{`, `{{"{{"}}`) } -// templateDate turns a date into an UTC string compatible with ClickHouse. -func templateDate(input time.Time) string { - return input.UTC().Format("2006-01-02 15:04:05") -} - // templateWhere transforms a filter to a WHERE clause func templateWhere(qf queryFilter) string { if qf.Filter == "" { diff --git a/console/docs.go b/console/docs.go index daaf2466..d106e7ec 100644 --- a/console/docs.go +++ b/console/docs.go @@ -168,7 +168,7 @@ func (r *imageEmbedder) Transform(node *ast.Document, reader text.Reader, pc par switch node := n.(type) { case *ast.Image: path := string(node.Destination) - if strings.Index(path, "/") != -1 || !strings.HasSuffix(path, ".svg") { + if strings.Contains(path, "/") || !strings.HasSuffix(path, ".svg") { break } f, err := r.root.Open(path) diff --git a/console/filter.go b/console/filter.go index 4f465f6b..81d1ae09 100644 --- a/console/filter.go +++ b/console/filter.go @@ -273,7 +273,6 @@ LIMIT 20`, column, column, column, column, column) } } gc.JSON(http.StatusOK, filterCompleteHandlerOutput{filteredCompletions}) - return } func (c *Component) filterSavedListHandlerFunc(gc *gin.Context) { diff --git a/console/graph.go b/console/graph.go index 382723c4..2a486cf2 100644 --- a/console/graph.go +++ b/console/graph.go @@ -277,11 +277,11 @@ func (c *Component) graphHandlerFunc(gc *gin.Context) { lastTimeForAxis[axis] = result.Time } rowKey := fmt.Sprintf("%d-%s", axis, result.Dimensions) - row, ok := points[axis][rowKey] + _, ok = points[axis][rowKey] if !ok { // Not points for this row yet, create it rows[axis][rowKey] = result.Dimensions - row = make([]int, len(output.Time)) + row := make([]int, len(output.Time)) points[axis][rowKey] = row sums[axis][rowKey] = 0 } diff --git a/console/query.go b/console/query.go index a82c3cc3..377b5499 100644 --- a/console/query.go +++ b/console/query.go @@ -79,6 +79,9 @@ func (qf *queryFilter) UnmarshalText(input []byte) error { } meta = &filter.Meta{ReverseDirection: true} reverse, err := filter.Parse("", input, filter.GlobalStore("meta", meta)) + if err != nil { + return fmt.Errorf("cannot parse reverse filter: %s", filter.HumanError(err)) + } *qf = queryFilter{ Filter: direct.(string), ReverseFilter: reverse.(string), diff --git a/console/root.go b/console/root.go index a5657b7f..b4c6e399 100644 --- a/console/root.go +++ b/console/root.go @@ -12,7 +12,6 @@ import ( "path/filepath" "runtime" "sync" - "text/template" "time" "github.com/benbjohnson/clock" @@ -33,9 +32,6 @@ type Component struct { t tomb.Tomb config Configuration - templates map[string]*template.Template - templatesLock sync.RWMutex - flowsTables []flowsTable flowsTablesLock sync.RWMutex diff --git a/demoexporter/snmp/server.go b/demoexporter/snmp/server.go index 229ed749..761c8a83 100644 --- a/demoexporter/snmp/server.go +++ b/demoexporter/snmp/server.go @@ -69,10 +69,8 @@ func (c *Component) startSNMPServer() error { return fmt.Errorf("unable to bind SNMP server: %w", err) } c.t.Go(func() error { - select { - case <-c.t.Dying(): - server.Shutdown() - } + <-c.t.Dying() + server.Shutdown() return nil }) diff --git a/inlet/flow/decoder/netflow/root.go b/inlet/flow/decoder/netflow/root.go index be2d03ed..1aeb341d 100644 --- a/inlet/flow/decoder/netflow/root.go +++ b/inlet/flow/decoder/netflow/root.go @@ -218,7 +218,7 @@ func (nd *Decoder) Decode(in decoder.RawFlow) []*decoder.FlowMessage { } } - flowMessageSet, err := producer.ProcessMessageNetFlow(msgDec, sampling) + flowMessageSet, _ := producer.ProcessMessageNetFlow(msgDec, sampling) for _, fmsg := range flowMessageSet { fmsg.TimeReceived = ts fmsg.SamplerAddress = in.Source diff --git a/inlet/flow/decoder/sflow/root.go b/inlet/flow/decoder/sflow/root.go index 4638b335..a8dc3c8a 100644 --- a/inlet/flow/decoder/sflow/root.go +++ b/inlet/flow/decoder/sflow/root.go @@ -117,7 +117,7 @@ func (nd *Decoder) Decode(in decoder.RawFlow) []*decoder.FlowMessage { } } - flowMessageSet, err := producer.ProcessMessageSFlow(msgDec) + flowMessageSet, _ := producer.ProcessMessageSFlow(msgDec) for _, fmsg := range flowMessageSet { fmsg.TimeReceived = ts fmsg.TimeFlowStart = ts diff --git a/inlet/snmp/cache.go b/inlet/snmp/cache.go index 95000284..ee4f5fef 100644 --- a/inlet/snmp/cache.go +++ b/inlet/snmp/cache.go @@ -195,9 +195,9 @@ func (sc *snmpCache) entriesOlderThan(older time.Duration, lastAccessed bool) ma what = &iface.LastUpdated } if atomic.LoadInt64(what) < threshold { - rifaces, ok := result[ip] + _, ok := result[ip] if !ok { - rifaces = make(map[uint]Interface) + rifaces := make(map[uint]Interface) result[ip] = rifaces } result[ip][ifindex] = iface.Interface diff --git a/orchestrator/clickhouse/http.go b/orchestrator/clickhouse/http.go index 2bcda4b6..d8cbf007 100644 --- a/orchestrator/clickhouse/http.go +++ b/orchestrator/clickhouse/http.go @@ -74,7 +74,7 @@ func (c *Component) registerHTTPHandlers() error { f, err := data.Open("data/asns.csv") if err != nil { c.r.Err(err).Msg("unable to open data/asns.csv") - http.Error(w, fmt.Sprintf("Unable to open ASN file."), + http.Error(w, "Unable to open ASN file.", http.StatusInternalServerError) return }