-
Notifications
You must be signed in to change notification settings - Fork 122
/
util.go
46 lines (38 loc) · 831 Bytes
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright 2021-2024 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0
package storage
import (
"context"
"github.com/cerbos/cerbos/internal/policy"
)
const MaxPoliciesInBatch = 25
func minInt(a, b int) int {
if a < b {
return a
}
return b
}
func BatchLoadPolicy(
ctx context.Context,
maxPoliciesInBatch int,
loadPolicyFn func(context.Context, ...string) ([]*policy.Wrapper, error),
processPolicyFn func(*policy.Wrapper) error,
ids ...string,
) error {
for idx := range ids {
if idx%maxPoliciesInBatch == 0 {
idxEnd := minInt(idx+maxPoliciesInBatch, len(ids))
var err error
policies, err := loadPolicyFn(ctx, ids[idx:idxEnd]...)
if err != nil {
return err
}
for _, p := range policies {
if err := processPolicyFn(p); err != nil {
return err
}
}
}
}
return nil
}