Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
synapse
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Maunium
synapse
Commits
ddd48b68
Commit
ddd48b68
authored
5 years ago
by
Erik Johnston
Browse files
Options
Downloads
Patches
Plain Diff
Move account validity bg updates to registration store
parent
2aa89438
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
synapse/storage/_base.py
+0
-66
0 additions, 66 deletions
synapse/storage/_base.py
synapse/storage/data_stores/main/registration.py
+64
-0
64 additions, 0 deletions
synapse/storage/data_stores/main/registration.py
with
64 additions
and
66 deletions
synapse/storage/_base.py
+
0
−
66
View file @
ddd48b68
...
...
@@ -252,8 +252,6 @@ class SQLBaseStore(object):
# A set of tables that are not safe to use native upserts in.
self
.
_unsafe_to_upsert_tables
=
set
(
UNIQUE_INDEX_BACKGROUND_UPDATES
.
keys
())
self
.
_account_validity
=
self
.
hs
.
config
.
account_validity
# We add the user_directory_search table to the blacklist on SQLite
# because the existing search table does not have an index, making it
# unsafe to use native upserts.
...
...
@@ -272,14 +270,6 @@ class SQLBaseStore(object):
self
.
rand
=
random
.
SystemRandom
()
if
self
.
_account_validity
.
enabled
:
self
.
_clock
.
call_later
(
0.0
,
run_as_background_process
,
"
account_validity_set_expiration_dates
"
,
self
.
_set_expiration_date_when_missing
,
)
@defer.inlineCallbacks
def
_check_safe_to_upsert
(
self
):
"""
...
...
@@ -312,62 +302,6 @@ class SQLBaseStore(object):
self
.
_check_safe_to_upsert
,
)
@defer.inlineCallbacks
def
_set_expiration_date_when_missing
(
self
):
"""
Retrieves the list of registered users that don
'
t have an expiration date, and
adds an expiration date for each of them.
"""
def
select_users_with_no_expiration_date_txn
(
txn
):
"""
Retrieves the list of registered users with no expiration date from the
database, filtering out deactivated users.
"""
sql
=
(
"
SELECT users.name FROM users
"
"
LEFT JOIN account_validity ON (users.name = account_validity.user_id)
"
"
WHERE account_validity.user_id is NULL AND users.deactivated = 0;
"
)
txn
.
execute
(
sql
,
[])
res
=
self
.
cursor_to_dict
(
txn
)
if
res
:
for
user
in
res
:
self
.
set_expiration_date_for_user_txn
(
txn
,
user
[
"
name
"
],
use_delta
=
True
)
yield
self
.
runInteraction
(
"
get_users_with_no_expiration_date
"
,
select_users_with_no_expiration_date_txn
,
)
def
set_expiration_date_for_user_txn
(
self
,
txn
,
user_id
,
use_delta
=
False
):
"""
Sets an expiration date to the account with the given user ID.
Args:
user_id (str): User ID to set an expiration date for.
use_delta (bool): If set to False, the expiration date for the user will be
now + validity period. If set to True, this expiration date will be a
random value in the [now + period - d ; now + period] range, d being a
delta equal to 10% of the validity period.
"""
now_ms
=
self
.
_clock
.
time_msec
()
expiration_ts
=
now_ms
+
self
.
_account_validity
.
period
if
use_delta
:
expiration_ts
=
self
.
rand
.
randrange
(
expiration_ts
-
self
.
_account_validity
.
startup_job_max_delta
,
expiration_ts
,
)
self
.
_simple_upsert_txn
(
txn
,
"
account_validity
"
,
keyvalues
=
{
"
user_id
"
:
user_id
},
values
=
{
"
expiration_ts_ms
"
:
expiration_ts
,
"
email_sent
"
:
False
},
)
def
start_profiling
(
self
):
self
.
_previous_loop_ts
=
monotonic_time
()
...
...
This diff is collapsed.
Click to expand it.
synapse/storage/data_stores/main/registration.py
+
64
−
0
View file @
ddd48b68
...
...
@@ -926,6 +926,14 @@ class RegistrationStore(RegistrationBackgroundUpdateStore):
self
.
_account_validity
=
hs
.
config
.
account_validity
if
self
.
_account_validity
.
enabled
:
self
.
_clock
.
call_later
(
0.0
,
run_as_background_process
,
"
account_validity_set_expiration_dates
"
,
self
.
_set_expiration_date_when_missing
,
)
# Create a background job for culling expired 3PID validity tokens
def
start_cull
():
# run as a background process to make sure that the database transactions
...
...
@@ -1502,3 +1510,59 @@ class RegistrationStore(RegistrationBackgroundUpdateStore):
self
.
_invalidate_cache_and_stream
(
txn
,
self
.
get_user_deactivated_status
,
(
user_id
,)
)
@defer.inlineCallbacks
def
_set_expiration_date_when_missing
(
self
):
"""
Retrieves the list of registered users that don
'
t have an expiration date, and
adds an expiration date for each of them.
"""
def
select_users_with_no_expiration_date_txn
(
txn
):
"""
Retrieves the list of registered users with no expiration date from the
database, filtering out deactivated users.
"""
sql
=
(
"
SELECT users.name FROM users
"
"
LEFT JOIN account_validity ON (users.name = account_validity.user_id)
"
"
WHERE account_validity.user_id is NULL AND users.deactivated = 0;
"
)
txn
.
execute
(
sql
,
[])
res
=
self
.
cursor_to_dict
(
txn
)
if
res
:
for
user
in
res
:
self
.
set_expiration_date_for_user_txn
(
txn
,
user
[
"
name
"
],
use_delta
=
True
)
yield
self
.
runInteraction
(
"
get_users_with_no_expiration_date
"
,
select_users_with_no_expiration_date_txn
,
)
def
set_expiration_date_for_user_txn
(
self
,
txn
,
user_id
,
use_delta
=
False
):
"""
Sets an expiration date to the account with the given user ID.
Args:
user_id (str): User ID to set an expiration date for.
use_delta (bool): If set to False, the expiration date for the user will be
now + validity period. If set to True, this expiration date will be a
random value in the [now + period - d ; now + period] range, d being a
delta equal to 10% of the validity period.
"""
now_ms
=
self
.
_clock
.
time_msec
()
expiration_ts
=
now_ms
+
self
.
_account_validity
.
period
if
use_delta
:
expiration_ts
=
self
.
rand
.
randrange
(
expiration_ts
-
self
.
_account_validity
.
startup_job_max_delta
,
expiration_ts
,
)
self
.
_simple_upsert_txn
(
txn
,
"
account_validity
"
,
keyvalues
=
{
"
user_id
"
:
user_id
},
values
=
{
"
expiration_ts_ms
"
:
expiration_ts
,
"
email_sent
"
:
False
},
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment