Skip to content

Commit

Permalink
feat: improve performance of AddPolicies (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
ishikawa-pro authored Jan 11, 2024
1 parent e87187c commit 8560937
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,20 +367,16 @@ func (a *adapter) AddPolicy(sec string, ptype string, rule []string) error {

// AddPolicies adds policy rules to the storage.
func (a *adapter) AddPolicies(sec string, ptype string, rules [][]string) error {
var lines []CasbinRule
var lines []interface{}
for _, rule := range rules {
line := savePolicyLine(ptype, rule)
lines = append(lines, line)
}

for _, line := range lines {
ctx, cancel := context.WithTimeout(context.TODO(), a.timeout)
defer cancel()
if _, err := a.collection.InsertOne(ctx, line); err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.TODO(), a.timeout)
defer cancel()
if _, err := a.collection.InsertMany(ctx, lines); err != nil {
return err
}

return nil
}

Expand Down

0 comments on commit 8560937

Please sign in to comment.