2021-11-03 02:09:49 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type memCache struct {
|
2021-11-03 16:33:34 +01:00
|
|
|
messages map[string][]*message
|
2021-12-09 16:23:17 +01:00
|
|
|
nop bool
|
2021-11-03 16:33:34 +01:00
|
|
|
mu sync.Mutex
|
2021-11-03 02:09:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ cache = (*memCache)(nil)
|
|
|
|
|
2021-12-09 16:23:17 +01:00
|
|
|
// newMemCache creates an in-memory cache
|
2021-11-03 02:09:49 +01:00
|
|
|
func newMemCache() *memCache {
|
|
|
|
return &memCache{
|
|
|
|
messages: make(map[string][]*message),
|
2021-12-09 16:23:17 +01:00
|
|
|
nop: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// newNopCache creates an in-memory cache that discards all messages;
|
|
|
|
// it is always empty and can be used if caching is entirely disabled
|
|
|
|
func newNopCache() *memCache {
|
|
|
|
return &memCache{
|
|
|
|
messages: make(map[string][]*message),
|
|
|
|
nop: true,
|
2021-11-03 02:09:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *memCache) AddMessage(m *message) error {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2021-12-09 16:23:17 +01:00
|
|
|
if s.nop {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-07 17:45:15 +01:00
|
|
|
if m.Event != messageEvent {
|
|
|
|
return errUnexpectedMessageType
|
|
|
|
}
|
2021-11-03 02:09:49 +01:00
|
|
|
if _, ok := s.messages[m.Topic]; !ok {
|
|
|
|
s.messages[m.Topic] = make([]*message, 0)
|
|
|
|
}
|
|
|
|
s.messages[m.Topic] = append(s.messages[m.Topic], m)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-08 15:46:31 +01:00
|
|
|
func (s *memCache) Messages(topic string, since sinceTime) ([]*message, error) {
|
2021-11-03 02:09:49 +01:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2021-12-07 17:45:15 +01:00
|
|
|
if _, ok := s.messages[topic]; !ok || since.IsNone() {
|
2021-11-03 02:09:49 +01:00
|
|
|
return make([]*message, 0), nil
|
|
|
|
}
|
|
|
|
messages := make([]*message, 0) // copy!
|
|
|
|
for _, m := range s.messages[topic] {
|
|
|
|
msgTime := time.Unix(m.Time, 0)
|
2021-11-08 15:46:31 +01:00
|
|
|
if msgTime == since.Time() || msgTime.After(since.Time()) {
|
2021-11-03 02:09:49 +01:00
|
|
|
messages = append(messages, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return messages, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *memCache) MessageCount(topic string) (int, error) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
if _, ok := s.messages[topic]; !ok {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return len(s.messages[topic]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *memCache) Topics() (map[string]*topic, error) {
|
2021-12-09 04:57:31 +01:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
topics := make(map[string]*topic)
|
|
|
|
for topic := range s.messages {
|
|
|
|
topics[topic] = newTopic(topic)
|
|
|
|
}
|
|
|
|
return topics, nil
|
2021-11-03 02:09:49 +01:00
|
|
|
}
|
|
|
|
|
2021-12-09 04:57:31 +01:00
|
|
|
func (s *memCache) Prune(olderThan time.Time) error {
|
2021-11-03 02:09:49 +01:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2021-12-07 17:45:15 +01:00
|
|
|
for topic := range s.messages {
|
2021-12-09 04:57:31 +01:00
|
|
|
s.pruneTopic(topic, olderThan)
|
2021-11-03 02:09:49 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-09 04:57:31 +01:00
|
|
|
func (s *memCache) pruneTopic(topic string, olderThan time.Time) {
|
|
|
|
messages := make([]*message, 0)
|
|
|
|
for _, m := range s.messages[topic] {
|
|
|
|
if m.Time >= olderThan.Unix() {
|
|
|
|
messages = append(messages, m)
|
2021-11-03 02:09:49 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-09 04:57:31 +01:00
|
|
|
s.messages[topic] = messages
|
2021-11-03 02:09:49 +01:00
|
|
|
}
|