Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Revert "feat: limit transaction concurrency" #230

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"runtime"
"strings"
"sync/atomic"

"github.com/casbin/casbin/v2"
"github.com/casbin/casbin/v2/model"
Expand Down Expand Up @@ -82,7 +81,6 @@ type Adapter struct {
dbSpecified bool
db *gorm.DB
isFiltered bool
isTransaction atomic.Bool
}

// finalizer is the destructor for Adapter.
Expand Down Expand Up @@ -666,23 +664,16 @@ func (a *Adapter) AddPolicies(sec string, ptype string, rules [][]string) error
}

// Transaction perform a set of operations within a transaction
// will return error while operation failed and transaction in concurrent environment
func (a *Adapter) Transaction(e casbin.IEnforcer, fc func(casbin.IEnforcer) error, opts ...*sql.TxOptions) error {
var err error
oriAdapter := a.db

// fail fast while transaction in concurrent environment
if !a.isTransaction.CompareAndSwap(false, true) {
return errors.New("cannot start a transaction within another transaction")
}
// reload policy from database to sync with the transaction
defer func() {
e.SetAdapter(&Adapter{db: oriAdapter})
err = e.LoadPolicy()
if err != nil {
panic(err)
}
a.isTransaction.Store(false)
}()
copyDB := *a.db
tx := copyDB.Begin(opts...)
Expand Down
Loading