Skip to content

Commit

Permalink
feat: implement --parallelism flag for diff and sync commands
Browse files Browse the repository at this point in the history
This controls the number of concurrent requests that are sent against
the Admin API of Kong.

Fix #85
  • Loading branch information
hbagdi authored and Travis Raines committed Apr 21, 2021
1 parent 0c3335d commit 58d1cef
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
9 changes: 7 additions & 2 deletions cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (
"github.com/spf13/cobra"
)

var diffCmdKongStateFile string
var (
diffCmdKongStateFile string
diffCmdParallelism int
)

// diffCmd represents the diff command
var diffCmd = &cobra.Command{
Expand Down Expand Up @@ -41,7 +44,7 @@ that will be created or updated or deleted.
return err
}
s, _ := diff.NewSyncer(currentState, targetState)
errs := solver.Solve(stopChannel, s, client, true)
errs := solver.Solve(stopChannel, s, client, diffCmdParallelism, true)
if errs != nil {
return utils.ErrArray{Errors: errs}
}
Expand All @@ -64,4 +67,6 @@ func init() {
diffCmd.Flags().BoolVar(&dumpConfig.SkipConsumers, "skip-consumers",
false, "do not diff consumers or "+
"any plugins associated with consumers")
diffCmd.Flags().IntVar(&diffCmdParallelism, "parallelism",
10, "Maximum number of concurrent operations")
}
10 changes: 8 additions & 2 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (
"github.com/spf13/cobra"
)

var syncCmdKongStateFile string
var (
syncCmdKongStateFile string
syncCmdParallelism int
)

// syncCmd represents the sync command
var syncCmd = &cobra.Command{
Expand All @@ -38,7 +41,8 @@ to get Kong's state in sync with the input state.`,
return err
}
syncer, _ := diff.NewSyncer(currentState, targetState)
errs := solver.Solve(stopChannel, syncer, client, false)
errs := solver.Solve(stopChannel, syncer, client, syncCmdParallelism,
false)
if errs != nil {
return utils.ErrArray{Errors: errs}
}
Expand All @@ -61,4 +65,6 @@ func init() {
syncCmd.Flags().BoolVar(&dumpConfig.SkipConsumers, "skip-consumers",
false, "do not diff consumers or "+
"any plugins associated with consumers")
syncCmd.Flags().IntVar(&syncCmdParallelism, "parallelism",
10, "Maximum number of concurrent operations")
}
2 changes: 2 additions & 0 deletions dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ func newOpt(tags []string) *kong.ListOpt {
// all the entities in KongRawState.
func Get(client *kong.Client, config Config) (*utils.KongRawState, error) {

// TODO make these requests concurrent

var state utils.KongRawState
services, err := GetAllServices(client, config.SelectorTags)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion reset/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (

// Reset deletes all entities in Kong.
func Reset(state *utils.KongRawState, client *kong.Client) error {

if state == nil {
return errors.New("state cannot be empty")
}
// TODO parallelize these operations

// Delete routes before services
for _, r := range state.Routes {
err := client.Routes.Delete(nil, r.ID)
Expand Down
4 changes: 2 additions & 2 deletions solver/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// Solve generates a diff and walks the graph.
func Solve(doneCh chan struct{}, syncer *diff.Syncer,
client *kong.Client, dry bool) []error {
client *kong.Client, parallelism int, dry bool) []error {
var r *crud.Registry
var err error
if dry {
Expand All @@ -23,7 +23,7 @@ func Solve(doneCh chan struct{}, syncer *diff.Syncer,
return append([]error{}, errors.Wrapf(err, "cannot build registry"))
}

return syncer.Run(doneCh, 10, func(e diff.Event) (crud.Arg, error) {
return syncer.Run(doneCh, parallelism, func(e diff.Event) (crud.Arg, error) {
return r.Do(e.Kind, e.Op, e)
})
}
Expand Down

0 comments on commit 58d1cef

Please sign in to comment.