pycasbin / postgresql-watcher

PostgreSQL Watcher for PyCasbin
https://github.com/casbin/pycasbin
Apache License 2.0
1 stars 8 forks source link

fix: made should_reload into a blocking call #21

Closed stuartbeattie84 closed 2 years ago

stuartbeattie84 commented 2 years ago

I have watcher code that looks something like this:

    adapter = casbin_sqlalchemy_adapter.Adapter(db_url)
    self._e = casbin.Enforcer(Config.casbin_conf_file, adapter)
    parsed_url = urllib.parse.urlparse(db_url)

    if db_adapter is True:
        self._watcher = PostgresqlWatcher(
            host=parsed_url.hostname, user=parsed_url.username, password=parsed_url.password,
            port=parsed_url.port, dbname=parsed_url.path.lstrip('/')
        )
        self._watcher.set_update_callback(self._e.load_policy)
        self._e.set_watcher(self._watcher)

        update_thread = threading.Thread(target=self.watcher_update, daemon=True)
        update_thread.start()

def watcher_update(self):
    while True:
        # should_reload() calls poll() to wait for data - returns True if there's been an update
        if self._watcher.should_reload():
            self._watcher.update_callback()

However, with the current watcher implementation the watcher_update() thread will peg CPU as the poll() call in should_reload() is non-blocking. I've changed this to a blocking call as well as removing the should_reload() call in create_subscriber_process(), which at the moment doesn't really do anything but would block the main thread if turned into a blocking call.

Unless I'm misunderstanding something, this is the only way I can get the watcher to work properly :).

casbin-bot commented 2 years ago

@ffyuanda @Zxilly please review

CLAassistant commented 2 years ago

CLA assistant check
All committers have signed the CLA.

hsluoyz commented 2 years ago

@leeqvip

hsluoyz commented 2 years ago

@stuartbeattie84 plz fix:

image

stuartbeattie84 commented 2 years ago

Sorted licensing issue :)

gmc77 commented 2 years ago

Hi, this is currently blocking us. Is there an ETA for when we can get his merged into a release? Thanks!

hsluoyz commented 2 years ago

@tangyang9464 @abichinger @Abingcbc plz review

hsluoyz commented 2 years ago

@gmc77 plz review

github-actions[bot] commented 2 years ago

:tada: This PR is included in version 0.1.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket: