Only create goroutines for next hour of disappearing messages

This commit is contained in:
Tulir Asokan 2022-01-07 14:46:53 +02:00
parent 3d457d005e
commit a61a3f5cf9
3 changed files with 15 additions and 9 deletions

View file

@ -54,7 +54,7 @@ func (dmq *DisappearingMessageQuery) NewWithValues(roomID id.RoomID, eventID id.
const (
getAllScheduledDisappearingMessagesQuery = `
SELECT room_id, event_id, expire_in, expire_at FROM disappearing_message WHERE expire_at IS NOT NULL
SELECT room_id, event_id, expire_in, expire_at FROM disappearing_message WHERE expire_at IS NOT NULL AND expire_at <= $1
`
startUnscheduledDisappearingMessagesInRoomQuery = `
UPDATE disappearing_message SET expire_at=$1+expire_in WHERE room_id=$2 AND expire_at IS NULL
@ -62,8 +62,8 @@ const (
`
)
func (dmq *DisappearingMessageQuery) GetAllScheduled() (messages []*DisappearingMessage) {
rows, err := dmq.db.Query(getAllScheduledDisappearingMessagesQuery)
func (dmq *DisappearingMessageQuery) GetUpcomingScheduled(duration time.Duration) (messages []*DisappearingMessage) {
rows, err := dmq.db.Query(getAllScheduledDisappearingMessagesQuery, time.Now().Add(duration).UnixMilli())
if err != nil || rows == nil {
return nil
}

View file

@ -42,15 +42,21 @@ func (portal *Portal) ScheduleDisappearing() {
if !portal.bridge.Config.Bridge.DisappearingMessagesInGroups && portal.IsGroupChat() {
return
}
nowPlusHour := time.Now().Add(1 * time.Hour)
for _, msg := range portal.bridge.DB.DisappearingMessage.StartAllUnscheduledInRoom(portal.MXID) {
go portal.sleepAndDelete(msg)
if msg.ExpireAt.Before(nowPlusHour) {
go portal.sleepAndDelete(msg)
}
}
}
func (bridge *Bridge) RestartAllDisappearing() {
for _, msg := range bridge.DB.DisappearingMessage.GetAllScheduled() {
portal := bridge.GetPortalByMXID(msg.RoomID)
go portal.sleepAndDelete(msg)
func (bridge *Bridge) DisappearingLoop() {
for {
for _, msg := range bridge.DB.DisappearingMessage.GetUpcomingScheduled(1 * time.Hour) {
portal := bridge.GetPortalByMXID(msg.RoomID)
go portal.sleepAndDelete(msg)
}
time.Sleep(1 * time.Hour)
}
}

View file

@ -333,7 +333,7 @@ func (bridge *Bridge) Start() {
if bridge.Config.Bridge.ResendBridgeInfo {
go bridge.ResendBridgeInfo()
}
go bridge.RestartAllDisappearing()
go bridge.DisappearingLoop()
bridge.AS.Ready = true
}