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

Possible concurrency issues with SQL approach #4

Open
wqweto opened this issue Sep 10, 2018 · 0 comments
Open

Possible concurrency issues with SQL approach #4

wqweto opened this issue Sep 10, 2018 · 0 comments

Comments

@wqweto
Copy link

wqweto commented Sep 10, 2018

Generating next timestamp after executing query might miss time interval needed to process the query itself.

Consider this code snippet:

    # For given list of topics, get all the messages from the SQLite db using subscriber's timestamp
    for topic in self.topics:
        self.cursor.execute("SELECT message, timestamp from mps_messages WHERE topic=:topic and timestamp>:timestamp", {"topic": topic, "timestamp": self.timestamp})
        data = self.cursor.fetchall()
        for each_record in data:
            self.messages.put_nowait(each_record[0])
    # Update the timestamp
    self.timestamp = datetime.now()

Probably a more resilient approach would be to generate next timestamp before processing the query and put it as upper bound on timestamp too.

    # For given list of topics, get all the messages from the SQLite db using subscriber's timestamp
    new_timestamp = datetime.now()
    for topic in self.topics:
        self.cursor.execute("SELECT message, timestamp from mps_messages WHERE topic=:topic and timestamp>:timestamp and timestamp<=:new_timestamp", {"topic": topic, "timestamp": self.timestamp, "new_timestamp": new_timestamp})
        data = self.cursor.fetchall()
        for each_record in data:
            self.messages.put_nowait(each_record[0])
    # Update the timestamp
    self.timestamp = new_timestamp

(air code)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant