Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Remove users_to_send_full_presence_to table culling; add fk for user_id
Browse files Browse the repository at this point in the history
  • Loading branch information
anoadragon453 committed May 5, 2021
1 parent b8d85c9 commit caee7da
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 35 deletions.
37 changes: 8 additions & 29 deletions synapse/storage/databases/main/presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,11 @@
from synapse.storage.util.id_generators import MultiWriterIdGenerator, StreamIdGenerator
from synapse.util.caches.descriptors import cached, cachedList
from synapse.util.caches.stream_change_cache import StreamChangeCache
from synapse.util.constants import HOUR_IN_MS
from synapse.util.iterutils import batch_iter

if TYPE_CHECKING:
from synapse.server import HomeServer

# How long to keep entries in the users_to_send_full_presence_to db table
USERS_TO_SEND_FULL_PRESENCE_TO_ENTRY_LIFETIME_MS = 14 * 24 * HOUR_IN_MS


class PresenceStore(SQLBaseStore):
def __init__(
Expand Down Expand Up @@ -247,31 +243,14 @@ async def add_users_to_send_full_presence_to(self, user_ids: Iterable[str]):
Args:
user_ids: An iterable of user IDs.
"""
time_now = self.hs.get_clock().time_msec()

def _add_users_to_send_full_presence_to_txn(txn):
# Add user entries to the table (or update their added_ms if they already exist)
self.db_pool.simple_upsert_many_txn(
txn,
table="users_to_send_full_presence_to",
key_names=("user_id",),
key_values=((user_id,) for user_id in user_ids),
value_names=("added_ms",),
value_values=((time_now,) for _ in user_ids),
)

# Delete entries in the table that have expired
sql = """
DELETE FROM users_to_send_full_presence_to
WHERE added_ms < ?
"""
txn.execute(
sql, (time_now - USERS_TO_SEND_FULL_PRESENCE_TO_ENTRY_LIFETIME_MS,)
)

await self.db_pool.runInteraction(
"add_users_to_send_full_presence_to",
_add_users_to_send_full_presence_to_txn,
# Add user entries to the table
await self.db_pool.simple_upsert_many(
table="users_to_send_full_presence_to",
key_names=("user_id",),
key_values=[(user_id,) for user_id in user_ids],
value_names=(),
value_values=(),
desc="add_users_to_send_full_presence_to",
)

async def remove_user_from_users_to_send_full_presence_to(self, user_id: str):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
-- want to duplicate work across multiple sync workers.

CREATE TABLE IF NOT EXISTS users_to_send_full_presence_to(
-- The user ID to send full presence to.
user_id TEXT PRIMARY KEY,
added_ms BIGINT NOT NULL
);

-- For checking expired entries
CREATE INDEX IF NOT EXISTS users_to_send_full_presence_to_added_ms
ON users_to_send_full_presence_to(added_ms);
FOREIGN KEY (user_id)
REFERENCES users (name)
);

0 comments on commit caee7da

Please sign in to comment.