casbin / gorm-adapter

GORM adapter for Casbin, see extended version of GORM Adapter Ex at: https://github.com/casbin/gorm-adapter-ex
https://github.com/casbin/casbin
Apache License 2.0
678 stars 206 forks source link

Lots of duplicate key error when running distributed casbin instances with watcher enabled #242

Closed longshine closed 1 month ago

longshine commented 1 month ago

In an environment of multiple distributed casbin instances with watcher enabled, every time adding policy in one instance, it will broadcast the operation to all other instances, making them to do the add operation too. This will cause "duplicate key error" in underlying database since they are inserting one same rule at the same time.

sequenceDiagram
    participant Casbin-1
    participant Casbin-2
    participant DB
    Casbin-1->>DB: Add policy (p, alice, data1, read)
    DB-->>Casbin-1: OK
    Casbin-1->>Casbin-2: New policy added!
    Casbin-2->>DB: Add policy (p, alice, data1, read)
    DB-->>Casbin-2: Duplicate key error

To solve this issue, there is an option OnConflict=DoNothing to set on insert operations.

hsluoyz commented 1 month ago

@longshine can you make a PR to fix it?

github-actions[bot] commented 1 month ago

:tada: This issue has been resolved in version 3.27.0 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket:

longshine commented 4 weeks ago

Thanks!