Skip to content

Commit c4544a6

Browse files
committed
Add Worker Flag
1 parent b17955b commit c4544a6

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

cmd/root.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7+
"runtime"
78
"time"
89

910
"github.com/pterm/pterm"
@@ -65,6 +66,8 @@ func init() {
6566
rootCmd.PersistentFlags().String("tlsClientPrivateKey", "", "Client private key for mtls")
6667
rootCmd.PersistentFlags().String("tlsClientCert", "", "Client certificate for mtls")
6768

69+
rootCmd.PersistentFlags().Uint("workers", 0, "Number of Concurrent Workers for Expensive Operations. 0 (default) uses the number of CPU cores")
70+
6871
viper.BindPFlag("debug", rootCmd.PersistentFlags().Lookup("debug"))
6972
viper.BindPFlag("timeout", rootCmd.PersistentFlags().Lookup("timeout"))
7073
viper.BindPFlag("serverAddress", rootCmd.PersistentFlags().Lookup("serverAddress"))
@@ -81,6 +84,8 @@ func init() {
8184
viper.BindPFlag("tlsSkipVerify", rootCmd.PersistentFlags().Lookup("tlsSkipVerify"))
8285
viper.BindPFlag("tlsClientCert", rootCmd.PersistentFlags().Lookup("tlsClientCert"))
8386
viper.BindPFlag("tlsClientPrivateKey", rootCmd.PersistentFlags().Lookup("tlsClientPrivateKey"))
87+
88+
viper.BindPFlag("workers", rootCmd.PersistentFlags().Lookup("workers"))
8489
}
8590

8691
func fileToContent(file, contentFlag string) {
@@ -148,6 +153,15 @@ func initConfig() {
148153
} else if err != nil && viper.GetBool("debug") {
149154
fmt.Fprintln(os.Stderr, "Getting Client Certificate File Flag:", err)
150155
}
156+
157+
// If Workers is set to 0, autodetect Number of Cores
158+
workers := viper.GetUint("workers")
159+
if workers == 0 {
160+
viper.Set("workers", runtime.NumCPU())
161+
if viper.GetBool("debug") {
162+
fmt.Fprintln(os.Stderr, "Autodetected Worker Count:", viper.GetUint("workers"))
163+
}
164+
}
151165
}
152166

153167
func SetVersionInfo(version, commit, date string, dirty bool) {

resource/list.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"runtime"
87
"strings"
98
"sync"
109
"time"
@@ -15,6 +14,7 @@ import (
1514
"github.com/passbolt/go-passbolt/helper"
1615
"github.com/pterm/pterm"
1716
"github.com/spf13/cobra"
17+
"github.com/spf13/viper"
1818
)
1919

2020
// decryptedResource holds the result of decrypting a single resource
@@ -128,10 +128,9 @@ func ResourceList(cmd *cobra.Command, args []string) error {
128128

129129
func decryptResourcesParallel(ctx context.Context, client *api.Client, resources []api.Resource, needSecrets bool) ([]decryptedResource, error) {
130130
// Use parallel decryption with worker pool
131-
numWorkers := runtime.NumCPU()
132-
if numWorkers > 16 {
133-
numWorkers = 16 // Cap at 16 workers
134-
}
131+
numWorkers := int(viper.GetUint("workers"))
132+
133+
// Limit Worker count to Resource count
135134
if len(resources) < numWorkers {
136135
numWorkers = len(resources)
137136
}

0 commit comments

Comments
 (0)