Skip to content

Commit

Permalink
feat: make generator concurrency configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmdm committed Aug 30, 2024
1 parent 581b620 commit c65962c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions cmd/server/api_get_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func TestGetParamsE2E(t *testing.T) {
LoadJoyContext: generator.RepoLoader(repo),
Logger: logger,
ChartPuller: generator.MakeChartPuller(logger),
Concurrency: 4,
},
})

Expand Down
7 changes: 7 additions & 0 deletions cmd/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"os"
"path/filepath"
"runtime"
"time"

"github.com/davidmdm/conf"
Expand All @@ -21,6 +22,10 @@ type Config struct {

CacheRoot string

Generator struct {
Concurrency int
}

Google struct {
Repository string
CredentialsFilePath string
Expand Down Expand Up @@ -70,6 +75,8 @@ func GetConfig() Config {
conf.Var(conf.Environ, &cfg.Otel.ServiceName, "OTEL_SERVICE_NAME", conf.Default("joy-generator"))
conf.Var(conf.Environ, &cfg.Otel.ServiceVersion, "OTEL_SERVICE_VERSION")

conf.Var(conf.Environ, &cfg.Generator.Concurrency, "GENERATOR_CONCURRENCY", conf.Default(runtime.NumCPU()))

conf.Environ.MustParse()

if path := cfg.Google.CredentialsFilePath; path != "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func SpanNamer(c *gin.Context) {
if span := trace.SpanFromContext(c.Request.Context()); span.IsRecording() {
name := c.Request.Method + cmp.Or(c.FullPath(), "route_not_found")
name = nonWordCharacters.ReplaceAllString(name, "_")
name = strings.ToLower(name)
name = strings.ToLower(name)
span.SetName(name)
}
}
5 changes: 3 additions & 2 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Generator struct {
LoadJoyContext JoyLoaderFunc
Logger zerolog.Logger
ChartPuller helm.Puller
Concurrency int
}

type MutexMap sync.Map
Expand Down Expand Up @@ -159,13 +160,13 @@ func (generator *Generator) Run(ctx context.Context) ([]Result, error) {
var (
wg sync.WaitGroup
reconciledReleases = make([]Result, len(releases))
semaphore = make(chan struct{}, 8)
semaphore = make(chan struct{}, min(generator.Concurrency, 1))
)

for i, release := range releases {
semaphore <- struct{}{}

wg.Add(1)
wg.Add(1)

go func() {
defer wg.Done()
Expand Down
6 changes: 3 additions & 3 deletions internal/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ func TestGenerator(t *testing.T) {
DefaultChartRef: "custom",
Charts: func() map[string]helm.Chart {
charts := maps.Clone(baseCharts)
custom := charts["custom"]
custom := charts["custom"]
custom.Mappings = map[string]any{
"annotations.test": true,
"image": "image@{{ .Release.Spec.Version }}",
}
charts["custom"] = custom
return charts
charts["custom"] = custom
return charts
}(),
ExpectedRelease: &v1alpha1.Release{
ApiVersion: "joy.nesto.ca/v1alpha1",
Expand Down

0 comments on commit c65962c

Please sign in to comment.