+4
server/message_store.go
+4
server/message_store.go
···
5
5
"sync"
6
6
)
7
7
8
+
// Memory store allows messages to be stored in memory
8
9
type MemoryStore struct {
9
10
mu sync.Mutex
10
11
msgs map[int]message
11
12
offset int
12
13
}
13
14
15
+
// New memory store initializes a new in memory store
14
16
func NewMemoryStore() *MemoryStore {
15
17
return &MemoryStore{
16
18
msgs: make(map[int]message),
17
19
}
18
20
}
19
21
22
+
// Write will write the provided message to the in memory store
20
23
func (m *MemoryStore) Write(msg message) error {
21
24
m.mu.Lock()
22
25
defer m.mu.Unlock()
···
28
31
return nil
29
32
}
30
33
34
+
// ReadFrom will read messages from (and including) the provided offset and pass them to the provided handler
31
35
func (m *MemoryStore) ReadFrom(offset int, handleFunc func(msg message)) error {
32
36
if offset < 0 || offset > m.offset {
33
37
return fmt.Errorf("invalid offset provided")