node-casbin / sequelize-adapter

Sequelize adapter for Casbin
https://github.com/casbin/node-casbin
Apache License 2.0
64 stars 34 forks source link

savePolicy() taking 6 seconds to perform #12

Closed lucasrolim-intuitive closed 5 years ago

lucasrolim-intuitive commented 5 years ago

I'm using MySQL and having some troubles with the time to add a new user policy. This is taking around 6 seconds. Is this the expected time?

My express.js route

app.put('/role/user/add', async (req, res) => {
  const status = await addUserRole(req.body.user, req.body.role_name);
  send_response(res, status);
})

Function to insert a new user role.

    async addUserRole(user_name, role_name){
        const status = await this.enforcer.addNamedGroupingPolicy('g', user_name, role_name);
        await this.enforcer.savePolicy();
        return status;
    }
hsluoyz commented 5 years ago

@lucasrolim-intuitive , how many policy rules do you have? Which adapter are you using?

lucasrolim-intuitive commented 5 years ago

I have around 20 rules type (p) and 250 of type (g). I´m using the sequelizer adapter.

    async createEnforcer(){
        const database = await SequelizeAdapter.newAdapter(`mysql://${db_credentials.user}:${db_credentials.password}@${db_credentials.host}:${db_credentials.port}/${db_credentials.name}`, true);
        this.enforcer = await Enforcer.newEnforcer('casbin/model.conf', database);
        await this.enforcer.loadPolicy();
    }
hsluoyz commented 5 years ago

@lucasrolim-intuitive Sequelizer Adapter supports the AutoSave feature, you do not need to call await this.enforcer.savePolicy() manually, addNamedGroupingPolicy() will automatically store the new policy rule into DB.

The savePolicy() in this adapter doesn't use bulk operation, it saves all policy rules (20 + 250) one by one, that's why it is slow. You can also choose to improve this adapter with bulk-style savePolicy operation

lucasrolim-intuitive commented 5 years ago

Thank you!