Skip to content

Commit

Permalink
Delete old expried sessions.
Browse files Browse the repository at this point in the history
There's no point keeping them around as trying to reuse will just result
in an exception anyway.
  • Loading branch information
erikjohnston committed Apr 23, 2019
1 parent 09278fb commit 165ac13
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions sydent/db/threepid_validation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ limitations under the License.
CREATE TABLE IF NOT EXISTS threepid_validation_sessions (id integer primary key, medium varchar(16) not null, address varchar(256) not null, clientSecret varchar(32) not null, validated int default 0, mtime bigint not null);
CREATE TABLE IF NOT EXISTS threepid_token_auths (id integer primary key, validationSession integer not null, token varchar(32) not null, sendAttemptNumber integer not null, foreign key (validationSession) references threepid_validations(id));

-- Used to find and delete expired sessions
CREATE INDEX IF NOT EXISTS threepid_validation_sessions_mtime ON threepid_validation_sessions(mtime);
30 changes: 30 additions & 0 deletions sydent/db/valsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from twisted.internet import task

import sydent.util.tokenutils

from sydent.validators import ValidationSession, IncorrectClientSecretException, InvalidSessionIdException, \
Expand All @@ -25,6 +27,10 @@ class ThreePidValSessionStore:
def __init__(self, syd):
self.sydent = syd

# Clean up old sessions every N minutes
cb = task.LoopingCall(self.deleteOldSessions)
cb.start(10 * 60.0)

def getOrCreateTokenSession(self, medium, address, clientSecret):
cur = self.sydent.db.cursor()

Expand Down Expand Up @@ -127,3 +133,27 @@ def getValidatedSession(self, sid, clientSecret):
raise SessionNotValidatedException()

return s

def deleteOldSessions(self):
"""Delete old threepid validation sessions that are long expired.
"""

cur = self.sydent.db.cursor()

delete_before_ts = time_msec() - 5 * ValidationSession.THREEPID_SESSION_VALID_LIFETIME_MS

sql = """
DELETE FROM threepid_validation_sessions
WHERE mtime < ?
"""
cur.execute(sql, (delete_before_ts,))

sql = """
DELETE FROM threepid_token_auths
WHERE validationSession NOT IN (
SELECT id FROM threepid_validation_sessions
)
"""
cur.execute(sql, (delete_before_ts,))

self.sydent.db.commit()

0 comments on commit 165ac13

Please sign in to comment.