aboutsummaryrefslogtreecommitdiff
path: root/matrix/rooms/roomcache.go
blob: ffdcad1d55944d084f484f93880219a7f8f4f2d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2020 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

package rooms

import (
	"compress/gzip"
	"encoding/gob"
	"os"
	"path/filepath"
	"time"

	"github.com/pkg/errors"
	sync "github.com/sasha-s/go-deadlock"

	"maunium.net/go/gomuks/debug"
	"maunium.net/go/mautrix/id"
)

// RoomCache contains room state info in a hashmap and linked list.
type RoomCache struct {
	sync.Mutex

	listPath  string
	directory string
	maxSize   int
	maxAge    int64
	getOwner  func() id.UserID
	noUnload  bool

	Map  map[id.RoomID]*Room
	head *Room
	tail *Room
	size int
}

func NewRoomCache(listPath, directory string, maxSize int, maxAge int64, getOwner func() id.UserID) *RoomCache {
	return &RoomCache{
		listPath:  listPath,
		directory: directory,
		maxSize:   maxSize,
		maxAge:    maxAge,
		getOwner:  getOwner,

		Map: make(map[id.RoomID]*Room),
	}
}

func (cache *RoomCache) DisableUnloading() {
	cache.noUnload = true
}

func (cache *RoomCache) EnableUnloading() {
	cache.noUnload = false
}

func (cache *RoomCache) LoadList() error {
	cache.Lock()
	defer cache.Unlock()

	// Open room list file
	file, err := os.OpenFile(cache.listPath, os.O_RDONLY, 0600)
	if err != nil {
		if os.IsNotExist(err) {
			return nil
		}
		return errors.Wrap(err, "failed to open room list file for reading")
	}
	defer debugPrintError(file.Close, "Failed to close room list file after reading")

	// Open gzip reader for room list file
	cmpReader, err := gzip.NewReader(file)
	if err != nil {
		return errors.Wrap(err, "failed to read gzip room list")
	}
	defer debugPrintError(cmpReader.Close, "Failed to close room list gzip reader")

	// Open gob decoder for gzip reader
	dec := gob.NewDecoder(cmpReader)
	// Read number of items in list
	var size int
	err = dec.Decode(&size)
	if err != nil {
		return errors.Wrap(err, "failed to read size of room list")
	}

	// Read list
	cache.Map = make(map[id.RoomID]*Room, size)
	for i := 0; i < size; i++ {
		room := &Room{}
		err = dec.Decode(room)
		if err != nil {
			debug.Printf("Failed to decode %dth room list entry: %v", i+1, err)
			continue
		}
		room.path = cache.roomPath(room.ID)
		room.cache = cache
		cache.Map[room.ID] = room
	}
	return nil
}

func (cache *RoomCache) SaveLoadedRooms() {
	cache.Lock()
	defer cache.Unlock()
	cache.clean(false)
	for node := cache.head; node != nil; node = node.prev {
		node.Save()
	}
}

func (cache *RoomCache) SaveList() error {
	cache.Lock()
	defer cache.Unlock()

	debug.Print("Saving room list...")
	// Open room list file
	file, err := os.OpenFile(cache.listPath, os.O_WRONLY|os.O_CREATE, 0600)
	if err != nil {
		return errors.Wrap(err, "failed to open room list file for writing")
	}
	defer debugPrintError(file.Close, "Failed to close room list file after writing")

	// Open gzip writer for room list file
	cmpWriter := gzip.NewWriter(file)
	defer debugPrintError(cmpWriter.Close, "Failed to close room list gzip writer")

	// Open gob encoder for gzip writer
	enc := gob.NewEncoder(cmpWriter)
	// Write number of items in list
	err = enc.Encode(len(cache.Map))
	if err != nil {
		return errors.Wrap(err, "failed to write size of room list")
	}

	// Write list
	for _, node := range cache.Map {
		err = enc.Encode(node)
		if err != nil {
			debug.Printf("Failed to encode room list entry of %s: %v", node.ID, err)
		}
	}
	debug.Print("Room list saved to", cache.listPath, len(cache.Map), cache.size)
	return nil
}

func (cache *RoomCache) Touch(roomID id.RoomID) {
	cache.Lock()
	node, ok := cache.Map[roomID]
	if !ok || node == nil {
		cache.Unlock()
		return
	}
	cache.touch(node)
	cache.Unlock()
}

func (cache *RoomCache) TouchNode(node *Room) {
	if cache.noUnload || node.touch + 2 > time.Now().Unix() {
		return
	}
	cache.Lock()
	cache.touch(node)
	cache.Unlock()
}

func (cache *RoomCache) touch(node *Room) {
	if node == cache.head {
		return
	}
	debug.Print("Touching", node.ID)
	cache.llPop(node)
	cache.llPush(node)
	node.touch = time.Now().Unix()
}

func (cache *RoomCache) Get(roomID id.RoomID) *Room {
	cache.Lock()
	node := cache.get(roomID)
	cache.Unlock()
	return node
}

func (cache *RoomCache) GetOrCreate(roomID id.RoomID) *Room {
	cache.Lock()
	node := cache.get(roomID)
	if node == nil {
		node = cache.newRoom(roomID)
		cache.llPush(node)
	}
	cache.Unlock()
	return node
}

func (cache *RoomCache) get(roomID id.RoomID) *Room {
	node, ok := cache.Map[roomID]
	if ok && node != nil {
		return node
	}
	return nil
}

func (cache *RoomCache) Put(room *Room) {
	cache.Lock()
	node := cache.get(room.ID)
	if node != nil {
		cache.touch(node)
	} else {
		cache.Map[room.ID] = room
		if room.Loaded() {
			cache.llPush(room)
		}
		node = room
	}
	cache.Unlock()
	node.Save()
}

func (cache *RoomCache) roomPath(roomID id.RoomID) string {
	return filepath.Join(cache.directory, string(roomID)+".gob.gz")
}

func (cache *RoomCache) Load(roomID id.RoomID) *Room {
	cache.Lock()
	defer cache.Unlock()
	node, ok := cache.Map[roomID]
	if ok {
		return node
	}

	node = NewRoom(roomID, cache)
	node.Load()
	return node
}

func (cache *RoomCache) llPop(node *Room) {
	if node.prev == nil && node.next == nil {
		return
	}
	if node.prev != nil {
		node.prev.next = node.next
	}
	if node.next != nil {
		node.next.prev = node.prev
	}
	if node == cache.tail {
		cache.tail = node.next
	}
	if node == cache.head {
		cache.head = node.prev
	}
	node.next = nil
	node.prev = nil
	cache.size--
}

func (cache *RoomCache) llPush(node *Room) {
	if node.next != nil || node.prev != nil {
		debug.PrintStack()
		debug.Print("Tried to llPush node that is already in stack")
		return
	}
	if node == cache.head {
		return
	}
	if cache.head != nil {
		cache.head.next = node
	}
	node.prev = cache.head
	node.next = nil
	cache.head = node
	if cache.tail == nil {
		cache.tail = node
	}
	cache.size++
	cache.clean(false)
}

func (cache *RoomCache) ForceClean() {
	cache.Lock()
	cache.clean(true)
	cache.Unlock()
}

func (cache *RoomCache) clean(force bool) {
	if cache.noUnload && !force {
		return
	}
	origSize := cache.size
	maxTS := time.Now().Unix() - cache.maxAge
	for cache.size > cache.maxSize {
		if cache.tail.touch > maxTS && !force {
			break
		}
		ok := cache.tail.Unload()
		node := cache.tail
		cache.llPop(node)
		if !ok {
			debug.Print("Unload returned false, pushing node back")
			cache.llPush(node)
		}
	}
	if cleaned := origSize - cache.size; cleaned > 0 {
		debug.Print("Cleaned", cleaned, "rooms")
	}
}

func (cache *RoomCache) Unload(node *Room) {
	cache.Lock()
	defer cache.Unlock()
	cache.llPop(node)
	ok := node.Unload()
	if !ok {
		debug.Print("Unload returned false, pushing node back")
		cache.llPush(node)
	}
}

func (cache *RoomCache) newRoom(roomID id.RoomID) *Room {
	node := NewRoom(roomID, cache)
	cache.Map[node.ID] = node
	return node
}