casbin / casbin-rs

An authorization library that supports access control models like ACL, RBAC, ABAC in Rust.
https://casbin.org
Apache License 2.0
861 stars 74 forks source link

Multithreaded environment: RwLock needed around Enforcer with sqlx-adapter? #278

Closed bk138 closed 2 years ago

bk138 commented 2 years ago

I've got a question about casbin-rs usage: I'm using it with actix-web and the example states that the Enforcer should be wrapped in an RwLock. I wonder if this is because of the example using FileAdapter and that the protection by the RwLock can maybe be omitted when using a DB adapter like sqlx-adapter? Or does the RwLock protect state inside the single Enforcer instance? Thanks!

casbin-bot commented 2 years ago

@smrpn @hackerchai @PsiACE @GopherJ

hsluoyz commented 2 years ago

@hackerchai

hackerchai commented 2 years ago

I've got a question about casbin-rs usage: I'm using it with actix-web and the example states that the Enforcer should be wrapped in an RwLock. I wonder if this is because of the example using FileAdapter and that the protection by the RwLock can maybe be omitted when using a DB adapter like sqlx-adapter? Or does the RwLock protect state inside the single Enforcer instance? Thanks!

We use RwLock because actix-web is actually multi-threads when handling concurrent requests. In this way, you can use adapter to work with cash in.

bk138 commented 2 years ago

We use RwLock because actix-web is actually multi-threads when handling concurrent requests. In this way, you can use adapter to work with cash in.

Yep, the question is more like: is Enforcer thread-safe (maybe only with certain adapters)? https://github.com/casbin/casbin-rs/commit/529554e0a044b20bbbc9607e59a5d053b537b86a kinda hints at that. If it is not, it's maybe a good idea to state that in the docs. Would you accept a PR to the README adding a hint?

hackerchai commented 2 years ago

We use RwLock because actix-web is actually multi-threads when handling concurrent requests. In this way, you can use adapter to work with cash in.

Yep, the question is more like: is Enforcer thread-safe (maybe only with certain adapters)? 529554e kinda hints at that. If it is not, it's maybe a good idea to state that in the docs. Would you accept a PR to the README adding a hint?

The enforcer is not thread-safe. PR is always welcomed.

ip1981 commented 1 year ago

From my experience, sqlx adapter will update the DB, but not the enforcer's model, so that any changes will not have any effect.