Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: process environments and applications in parallel #28

Merged
merged 3 commits into from
Jun 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions cmd/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@ func init() {
log.Fatal().Err(err).Msg("Unable to initialize myks's globe")
}

log.Info().Msg("Syncing vendir configs")
if err := g.Sync(); err != nil {
log.Info().Msg("Syncing and rendering environments")
if err := g.SyncAndRender(); err != nil {
log.Fatal().Err(err).Msg("Unable to sync vendir configs")
}

log.Info().Msg("Rendering manifests")
if err := g.Render(); err != nil {
log.Fatal().Err(err).Msg("Unable to render applications")
}
},
}

Expand Down
34 changes: 25 additions & 9 deletions internal/myks/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package myks

import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -72,21 +73,36 @@ func (e *Environment) Init(applicationNames []string) error {
}

func (e *Environment) Sync() error {
for _, app := range e.applications {
if err := app.Sync(); err != nil {
return err
return processItemsInParallel(e.applications, func(item interface{}) error {
app, ok := item.(*Application)
if !ok {
return fmt.Errorf("Unable to cast item to *Application")
}
}
return nil
return app.Sync()
})
}

func (e *Environment) Render() error {
for _, app := range e.applications {
if err := app.Render(); err != nil {
return processItemsInParallel(e.applications, func(item interface{}) error {
app, ok := item.(*Application)
if !ok {
return fmt.Errorf("Unable to cast item to *Application")
}
return app.Render()
})
}

func (e *Environment) SyncAndRender() error {
return processItemsInParallel(e.applications, func(item interface{}) error {
app, ok := item.(*Application)
if !ok {
return fmt.Errorf("Unable to cast item to *Application")
}
if err := app.Sync(); err != nil {
return err
}
}
return nil
return app.Render()
})
}

func (e *Environment) setId() error {
Expand Down
45 changes: 29 additions & 16 deletions internal/myks/globe.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package myks

import (
"fmt"
"io"
"io/fs"
"os"
Expand Down Expand Up @@ -92,31 +93,43 @@ func (g *Globe) Init(searchPaths []string, applicationNames []string) error {

g.collectEnvironments(searchPaths)

for _, env := range g.environments {
if err := env.Init(applicationNames); err != nil {
return err
return processItemsInParallel(g.environments, func(item interface{}) error {
env, ok := item.(*Environment)
if !ok {
return fmt.Errorf("Unable to cast item to *Environment")
}
}

return nil
return env.Init(applicationNames)
})
}

func (g *Globe) Sync() error {
for _, env := range g.environments {
if err := env.Sync(); err != nil {
return err
return processItemsInParallel(g.environments, func(item interface{}) error {
env, ok := item.(*Environment)
if !ok {
return fmt.Errorf("Unable to cast item to *Environment")
}
}
return nil
return env.Sync()
})
}

func (g *Globe) Render() error {
for _, env := range g.environments {
if err := env.Render(); err != nil {
return err
return processItemsInParallel(g.environments, func(item interface{}) error {
env, ok := item.(*Environment)
if !ok {
return fmt.Errorf("Unable to cast item to *Environment")
}
}
return nil
return env.Render()
})
}

func (g *Globe) SyncAndRender() error {
return processItemsInParallel(g.environments, func(item interface{}) error {
env, ok := item.(*Environment)
if !ok {
return fmt.Errorf("Unable to cast item to *Environment")
}
return env.SyncAndRender()
})
}

func (g *Globe) collectEnvironments(searchPaths []string) {
Expand Down
41 changes: 41 additions & 0 deletions internal/myks/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package myks

import (
"bytes"
"fmt"
"io"
"os/exec"
"reflect"

"github.com/rs/zerolog/log"
)
Expand Down Expand Up @@ -60,3 +62,42 @@ func contains(list []string, item string) bool {
}
return false
}

func processItemsInParallel(collection interface{}, fn func(interface{}) error) error {
var items []interface{}

value := reflect.ValueOf(collection)

if value.Kind() != reflect.Slice && value.Kind() != reflect.Array && value.Kind() != reflect.Map {
return fmt.Errorf("collection must be a slice, array or map, got %s", value.Kind())
}

if value.Kind() == reflect.Map {
for _, key := range value.MapKeys() {
items = append(items, value.MapIndex(key).Interface())
}
} else {
for i := 0; i < value.Len(); i++ {
items = append(items, value.Index(i).Interface())
}
}

errChan := make(chan error, len(items))
defer close(errChan)

for _, item := range items {
go func(item interface{}) {
errChan <- fn(item)
}(item)
}

// Catch the first error
var err error
for range items {
if subErr := <-errChan; subErr != nil && err == nil {
err = fmt.Errorf("failed to process item: %w", subErr)
}
}

return err
}