about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2025-01-23 14:54:12 +0100
committerAlan Pearce2025-01-24 13:04:41 +0100
commit200857159766d3c6c5831450048348444c5666f4 (patch)
tree36fb6ebbbc91e609106c8f199e01cf00ab830c3f
parent7496bb7e0652d86e2afd05d35e6632dab27c954a (diff)
downloadsearchix-200857159766d3c6c5831450048348444c5666f4.tar.lz
searchix-200857159766d3c6c5831450048348444c5666f4.tar.zst
searchix-200857159766d3c6c5831450048348444c5666f4.zip
perf: pass context to importer for better tracing
-rw-r--r--cmd/searchix-web/main.go10
-rw-r--r--go.mod1
-rw-r--r--go.sum4
-rw-r--r--gomod2nix.toml3
-rw-r--r--internal/importer/main.go3
-rw-r--r--internal/importer/main_test.go10
-rw-r--r--searchix.go14
7 files changed, 33 insertions, 12 deletions
diff --git a/cmd/searchix-web/main.go b/cmd/searchix-web/main.go
index 63b1ec5..2cffb6b 100644
--- a/cmd/searchix-web/main.go
+++ b/cmd/searchix-web/main.go
@@ -53,12 +53,15 @@ func main() {
 
 	log.SetLevel(cfg.LogLevel)
 
+	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
+	defer cancel()
+
 	s, err := searchix.New(cfg, logger)
 	if err != nil {
 		logger.Fatal("Failed to initialise searchix", "error", err)
 	}
 
-	err = s.SetupIndex(&searchix.IndexOptions{
+	err = s.SetupIndex(ctx, &searchix.IndexOptions{
 		Update:    *update,
 		Replace:   *replace,
 		LowMemory: cfg.Importer.LowMemory,
@@ -69,12 +72,9 @@ func main() {
 	}
 
 	if *replace || *update {
-		os.Exit(0)
+		return
 	}
 
-	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
-	defer cancel()
-
 	go func() {
 		err = s.Start(ctx, *dev)
 		if err != nil {
diff --git a/go.mod b/go.mod
index b4e535f..9ecf80a 100644
--- a/go.mod
+++ b/go.mod
@@ -57,6 +57,7 @@ require (
 	github.com/mschoch/smat v0.2.0 // indirect
 	github.com/ncruces/go-strftime v0.1.9 // indirect
 	github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
+	github.com/stretchr/testify v1.10.0 // indirect
 	github.com/sykesm/zap-logfmt v0.0.4 // indirect
 	github.com/thessem/zap-prettyconsole v0.5.2 // indirect
 	go.etcd.io/bbolt v1.3.11 // indirect
diff --git a/go.sum b/go.sum
index e5bc8ba..eac36a3 100644
--- a/go.sum
+++ b/go.sum
@@ -127,8 +127,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
 github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
 github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
-github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
-github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
+github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
+github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
 github.com/sykesm/zap-logfmt v0.0.4 h1:U2WzRvmIWG1wDLCFY3sz8UeEmsdHQjHFNlIdmroVFaI=
 github.com/sykesm/zap-logfmt v0.0.4/go.mod h1:AuBd9xQjAe3URrWT1BBDk2v2onAZHkZkWRMiYZXiZWA=
 github.com/tailscale/depaware v0.0.0-20210622194025-720c4b409502/go.mod h1:p9lPsd+cx33L3H9nNoecRRxPssFKUwwI50I3pZ0yT+8=
diff --git a/gomod2nix.toml b/gomod2nix.toml
index 5d3f8e4..c952c86 100644
--- a/gomod2nix.toml
+++ b/gomod2nix.toml
@@ -139,6 +139,9 @@ schema = 3
   [mod."github.com/stoewer/go-strcase"]
     version = "v1.3.0"
     hash = "sha256-X0ilcefeqVQ44B9WT6euCMcigs7oLFypOQaGI33kGr8="
+  [mod."github.com/stretchr/testify"]
+    version = "v1.10.0"
+    hash = "sha256-fJ4gnPr0vnrOhjQYQwJ3ARDKPsOtA7d4olQmQWR+wpI="
   [mod."github.com/sykesm/zap-logfmt"]
     version = "v0.0.4"
     hash = "sha256-KXVFtOU54chusK8AhZrzrvbbNmzq1mNrhs/7OmO+huE="
diff --git a/internal/importer/main.go b/internal/importer/main.go
index dfa2477..2af1666 100644
--- a/internal/importer/main.go
+++ b/internal/importer/main.go
@@ -144,6 +144,7 @@ func createSourceImporter(
 }
 
 func Start(
+	ctx context.Context,
 	cfg *config.Config,
 	log *log.Logger,
 	indexer *index.WriteIndex,
@@ -158,7 +159,7 @@ func Start(
 
 	log.Debug("starting importer", "timeout", cfg.Importer.Timeout.Duration)
 	importCtx, cancelImport := context.WithTimeout(
-		context.Background(),
+		ctx,
 		cfg.Importer.Timeout.Duration,
 	)
 	defer cancelImport()
diff --git a/internal/importer/main_test.go b/internal/importer/main_test.go
index 0bc438d..9e19b8c 100644
--- a/internal/importer/main_test.go
+++ b/internal/importer/main_test.go
@@ -1,6 +1,7 @@
 package importer
 
 import (
+	"context"
 	"testing"
 
 	"go.alanpearce.eu/searchix/internal/config"
@@ -21,7 +22,14 @@ func BenchmarkImporterLowMemory(b *testing.B) {
 		b.Fatal(err)
 	}
 
-	err = Start(&cfg, logger.Named("importer"), write, false, &[]string{"nixpkgs"})
+	err = Start(
+		context.Background(),
+		&cfg,
+		logger.Named("importer"),
+		write,
+		false,
+		&[]string{"nixpkgs"},
+	)
 	if err != nil {
 		b.Fatal(err)
 	}
diff --git a/searchix.go b/searchix.go
index 031db31..615afc6 100644
--- a/searchix.go
+++ b/searchix.go
@@ -44,7 +44,7 @@ type IndexOptions struct {
 	Logger    *log.Logger
 }
 
-func (s *Server) SetupIndex(options *IndexOptions) error {
+func (s *Server) SetupIndex(ctx context.Context, options *IndexOptions) error {
 	var i uint
 	cfgEnabledSources := make([]string, len(s.cfg.Importer.Sources))
 	for key := range s.cfg.Importer.Sources {
@@ -78,6 +78,7 @@ func (s *Server) SetupIndex(options *IndexOptions) error {
 			options.Update,
 		)
 		err = importer.Start(
+			ctx,
 			s.cfg,
 			s.log.Named("importer"),
 			write,
@@ -105,7 +106,14 @@ func (s *Server) SetupIndex(options *IndexOptions) error {
 			})
 			if len(newSources) > 0 {
 				s.log.Info("adding new sources", "sources", newSources)
-				err := importer.Start(s.cfg, options.Logger.Named("importer"), write, false, &newSources)
+				err := importer.Start(
+					ctx,
+					s.cfg,
+					options.Logger.Named("importer"),
+					write,
+					false,
+					&newSources,
+				)
 				if err != nil {
 					return errors.Wrap(err, "Failed to update index with new sources")
 				}
@@ -189,7 +197,7 @@ func (s *Server) startUpdateTimer(
 				Status:      sentry.CheckInStatusInProgress,
 			}, monitorConfig)
 
-			err = importer.Start(s.cfg, s.log.Named("importer"), s.writeIndex, false, nil)
+			err = importer.Start(ctx, s.cfg, s.log.Named("importer"), s.writeIndex, false, nil)
 			s.wg.Done()
 			if err != nil {
 				s.log.Warn("error updating index", "error", err)