From 97fd7cf1b8ec4b0c1244036e0b321927e9460d94 Mon Sep 17 00:00:00 2001 From: ishikawa-pro Date: Thu, 11 Jan 2024 19:51:06 +0900 Subject: [PATCH] perf: AddPolicies to use InsertMany for improved performance --- adapter.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/adapter.go b/adapter.go index 766850f..6351ff1 100644 --- a/adapter.go +++ b/adapter.go @@ -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 }