Skip to content

Commit 964a34c

Browse files
committed
Switch to using a single worker
Change "workers" cli option to be in pkg/config/operator and use ALLSTAR_NUM_WORKERS envvar with same default at 5. Update staging and prod config to use 1 worker to save concurrent memory usage. Signed-off-by: Jeff Mendoza <[email protected]>
1 parent 9c5f410 commit 964a34c

File tree

6 files changed

+28
-13
lines changed

6 files changed

+28
-13
lines changed

app-prod.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ env_variables:
1010
APP_ID: 119816
1111
KEY_SECRET: "gcpsecretmanager://projects/allstar-ossf/secrets/allstar-private-key?decoder=bytes"
1212
DO_NOTHING_ON_OPT_OUT: true
13+
ALLSTAR_NUM_WORKERS: 1

app-staging.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ resources:
1010
env_variables:
1111
APP_ID: 166485
1212
KEY_SECRET: "gcpsecretmanager://projects/allstar-ossf/secrets/allstar-staging-private-key?decoder=bytes"
13+
ALLSTAR_NUM_WORKERS: 1

cmd/allstar/main.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ func main() {
6262
specificPolicyArg := flag.String("policy", "", fmt.Sprintf("Run a specific policy check. Supported policies: %s", supportedPoliciesMsg))
6363
specificRepoArg := flag.String("repo", "", "Run on a specific \"owner/repo\". For example \"ossf/allstar\"")
6464

65-
numWorkersArg := flag.Int("workers", 5, "maximum number of active goroutines for Allstar scans")
66-
6765
flag.Parse()
6866

6967
if *specificPolicyArg != "" {
@@ -83,7 +81,7 @@ func main() {
8381
}
8482

8583
if runOnce {
86-
_, err := enforce.EnforceAll(ctx, ghc, *specificPolicyArg, *specificRepoArg, *numWorkersArg)
84+
_, err := enforce.EnforceAll(ctx, ghc, *specificPolicyArg, *specificRepoArg)
8785
if err != nil {
8886
log.Fatal().
8987
Err(err).
@@ -96,7 +94,7 @@ func main() {
9694
go func() {
9795
defer wg.Done()
9896
log.Info().
99-
Err(enforce.EnforceJob(ctx, ghc, (5 * time.Minute), *specificPolicyArg, *specificRepoArg, *numWorkersArg)).
97+
Err(enforce.EnforceJob(ctx, ghc, (5 * time.Minute), *specificPolicyArg, *specificRepoArg)).
10098
Msg("Enforce job shutting down.")
10199
}()
102100
sigs := make(chan os.Signal, 1)

pkg/config/operator/operator.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ const setNoticePingDurationHrs = (24 * time.Hour)
9595

9696
var NoticePingDuration time.Duration
9797

98+
// NumWorkers is the number of concurrent orginazations/installations the
99+
// Allstar binary will scan concurrently.
100+
const setNumWorkers = 5
101+
102+
var NumWorkers int
103+
98104
var osGetenv func(string) string
99105

100106
func init() {
@@ -147,4 +153,12 @@ func setVars() {
147153

148154
allowedOrgs := osGetenv("GITHUB_ALLOWED_ORGS")
149155
AllowedOrganizations = strings.Split(allowedOrgs, ",")
156+
157+
nws := osGetenv("ALLSTAR_NUM_WORKERS")
158+
nw, err := strconv.Atoi(nws)
159+
if err == nil {
160+
NumWorkers = nw
161+
} else {
162+
NumWorkers = setNumWorkers
163+
}
150164
}

pkg/enforce/enforce.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func init() {
6767
//
6868
// TBD: determine if this should remain exported, or if it will only be called
6969
// from EnforceJob.
70-
func EnforceAll(ctx context.Context, ghc ghclients.GhClientsInterface, specificPolicyArg string, specificRepoArg string, numWorkersArg int) (EnforceAllResults, error) {
70+
func EnforceAll(ctx context.Context, ghc ghclients.GhClientsInterface, specificPolicyArg string, specificRepoArg string) (EnforceAllResults, error) {
7171
var repoCount int
7272
var enforceAllResults = make(EnforceAllResults)
7373
ac, err := ghc.Get(0)
@@ -85,10 +85,13 @@ func EnforceAll(ctx context.Context, ghc ghclients.GhClientsInterface, specificP
8585
Msg("Enforcing policies on installations.")
8686

8787
g, ctx := errgroup.WithContext(ctx)
88-
g.SetLimit(numWorkersArg)
88+
g.SetLimit(operator.NumWorkers)
8989
var mu sync.Mutex
9090

9191
for _, i := range insts {
92+
if ctx.Err() != nil {
93+
break
94+
}
9295
if i.SuspendedAt != nil {
9396
log.Info().
9497
Str("area", "bot").
@@ -302,9 +305,9 @@ func getAppInstallationReposReal(ctx context.Context, ic *github.Client) ([]*git
302305

303306
// EnforceJob is a reconciliation job that enforces policies on all repos every
304307
// d duration. It runs forever until the context is done.
305-
func EnforceJob(ctx context.Context, ghc *ghclients.GHClients, d time.Duration, specificPolicyArg string, specificRepoArg string, numWorkersArg int) error {
308+
func EnforceJob(ctx context.Context, ghc *ghclients.GHClients, d time.Duration, specificPolicyArg string, specificRepoArg string) error {
306309
for {
307-
_, err := EnforceAll(ctx, ghc, specificPolicyArg, specificRepoArg, numWorkersArg)
310+
_, err := EnforceAll(ctx, ghc, specificPolicyArg, specificRepoArg)
308311
if err != nil {
309312
log.Error().
310313
Err(err).

pkg/enforce/enforce_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,7 @@ func TestEnforceAll(t *testing.T) {
549549
policy1Results = test.Policy1Results
550550
policy2Results = test.Policy2Results
551551

552-
numWorkers := 1
553-
enforceAllResults, err := EnforceAll(context.Background(), mockGhc, "", "", numWorkers)
552+
enforceAllResults, err := EnforceAll(context.Background(), mockGhc, "", "")
554553
if err != nil {
555554
t.Fatalf("Unexpected error: %v", err)
556555
}
@@ -582,16 +581,15 @@ func TestSuspendedEnforce(t *testing.T) {
582581
}
583582
suspended = false
584583
gaicalled = false
585-
numWorkers := 1
586-
if _, err := EnforceAll(context.Background(), &MockGhClients{}, "", "", numWorkers); err != nil {
584+
if _, err := EnforceAll(context.Background(), &MockGhClients{}, "", ""); err != nil {
587585
t.Fatalf("Unexpected error: %v", err)
588586
}
589587
if !gaicalled {
590588
t.Errorf("Expected getAppInstallationRepos() to be called, but wasn't")
591589
}
592590
suspended = true
593591
gaicalled = false
594-
if _, err := EnforceAll(context.Background(), &MockGhClients{}, "", "", numWorkers); err != nil {
592+
if _, err := EnforceAll(context.Background(), &MockGhClients{}, "", ""); err != nil {
595593
t.Fatalf("Unexpected error: %v", err)
596594
}
597595
if gaicalled {

0 commit comments

Comments
 (0)