Skip to content
This repository was archived by the owner on May 30, 2023. It is now read-only.

Commit d14d771

Browse files
committed
naive LRU to throw away old rooms
halve forward pagination frequency don't stack up on forward pagination jobs Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
1 parent de1cea8 commit d14d771

File tree

9 files changed

+45
-5
lines changed

9 files changed

+45
-5
lines changed

src/github.com/t3chguy/matrix-static/job-room-aliases.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ func (job RoomAliasesJob) Work(w *Worker) {
4444
job.pageSize,
4545
job.page,
4646
}
47+
room.Access()
4748
}

src/github.com/t3chguy/matrix-static/job-room-events.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ func (job RoomEventsJob) Work(w *Worker) {
5252
atBottomEnd,
5353
err,
5454
}
55+
room.Access()
5556
}

src/github.com/t3chguy/matrix-static/job-room-forwardpaginate.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,33 @@
1414

1515
package main
1616

17+
import (
18+
log "github.com/Sirupsen/logrus"
19+
"sync"
20+
"time"
21+
)
22+
1723
// This Job has no Resp.
1824

19-
type RoomForwardPaginateJob struct{}
25+
type RoomForwardPaginateJob struct {
26+
wg *sync.WaitGroup
27+
}
28+
29+
const LastAccessDiscardDuration = 30 * time.Minute
2030

2131
func (job RoomForwardPaginateJob) Work(w *Worker) {
32+
// discard old rooms first
33+
numRoomsBefore := len(w.rooms)
34+
for id, room := range w.rooms {
35+
if room.LastAccess.Before(time.Now().Add(-LastAccessDiscardDuration)) {
36+
delete(w.rooms, id)
37+
}
38+
}
39+
numRoomsAfter := len(w.rooms)
40+
log.WithField("worker", w.ID).WithField("numRooms", numRoomsAfter).Infof("Removed %d rooms", numRoomsBefore-numRoomsAfter)
41+
2242
for _, room := range w.rooms {
2343
room.ForwardPaginateRoom()
2444
}
45+
job.wg.Done()
2546
}

src/github.com/t3chguy/matrix-static/job-room-memberinfo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@ func (job RoomMemberInfoJob) Work(w *Worker) {
5959
memberInfo,
6060
err,
6161
}
62+
room.Access()
6263
}

src/github.com/t3chguy/matrix-static/job-room-members.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ func (job RoomMembersJob) Work(w *Worker) {
5050
job.pageSize,
5151
job.page,
5252
}
53+
room.Access()
5354
}

src/github.com/t3chguy/matrix-static/job-room-powerlevels.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ func (job RoomPowerLevelsJob) Work(w *Worker) {
3535
room.RoomInfo(),
3636
powerLevels,
3737
}
38+
room.Access()
3839
}

src/github.com/t3chguy/matrix-static/job-room-servers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ func (job RoomServersJob) Work(w *Worker) {
4444
job.pageSize,
4545
job.page,
4646
}
47+
room.Access()
4748
}

src/github.com/t3chguy/matrix-static/matrix-static.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"os"
3535
"path/filepath"
3636
"strconv"
37+
"sync"
3738
"time"
3839
"unicode"
3940
"unicode/utf8"
@@ -362,13 +363,17 @@ func startPublicRoomListTimer(worldReadableRooms *mxclient.WorldReadableRooms) {
362363
}
363364
}
364365

365-
const LazyForwardPaginateRooms = time.Minute
366+
const LazyForwardPaginateRooms = 2 * time.Minute
366367

367368
func startForwardPaginator(workers *Workers) {
368-
t := time.NewTicker(LazyForwardPaginateRooms)
369+
//t := time.NewTicker(LazyForwardPaginateRooms)
370+
wg := sync.WaitGroup{}
369371
for {
370-
<-t.C
372+
//<-t.C
373+
time.Sleep(LazyForwardPaginateRooms)
374+
wg.Add(int(workers.numWorkers))
371375
log.Info("Forward paginating all loaded rooms")
372-
workers.JobForAllWorkers(RoomForwardPaginateJob{})
376+
workers.JobForAllWorkers(RoomForwardPaginateJob{&wg})
377+
wg.Wait()
373378
}
374379
}

src/github.com/t3chguy/matrix-static/mxclient/room.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"errors"
1919
"github.com/matrix-org/gomatrix"
2020
"github.com/t3chguy/matrix-static/utils"
21+
"time"
2122
)
2223

2324
type RoomInfo struct {
@@ -46,6 +47,12 @@ type Room struct {
4647
latestRoomState RoomState
4748

4849
HasReachedHistoricEndOfTimeline bool
50+
51+
LastAccess time.Time
52+
}
53+
54+
func (r *Room) Access() {
55+
r.LastAccess = time.Now()
4956
}
5057

5158
// ForwardPaginateRoom queries the API for any events newer than the latest one currently in the timeline and appends them.
@@ -197,6 +204,7 @@ func (m *Client) NewRoom(roomID string) (*Room, error) {
197204
backPaginationToken: resp.Messages.Start,
198205
eventList: filteredEventList,
199206
latestRoomState: *NewRoomState(m),
207+
LastAccess: time.Now(),
200208
}
201209

202210
for _, event := range resp.State {

0 commit comments

Comments
 (0)