Stop storing messages in memory and pull from persistence. Closes #81 (#105)

This commit is contained in:
Gabe Kangas 2020-08-05 21:01:06 -07:00 committed by GitHub
parent a692904516
commit b0b5801c5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 2 additions and 23 deletions

View file

@ -11,7 +11,6 @@ import (
func Setup(listener models.ChatListener) { func Setup(listener models.ChatListener) {
setupPersistence() setupPersistence()
messages := []models.ChatMessage{}
clients := make(map[string]*Client) clients := make(map[string]*Client)
addCh := make(chan *Client) addCh := make(chan *Client)
delCh := make(chan *Client) delCh := make(chan *Client)
@ -19,10 +18,8 @@ func Setup(listener models.ChatListener) {
pingCh := make(chan models.PingMessage) pingCh := make(chan models.PingMessage)
doneCh := make(chan bool) doneCh := make(chan bool)
errCh := make(chan error) errCh := make(chan error)
messages = append(messages, getChatHistory()...)
_server = &server{ _server = &server{
messages,
clients, clients,
"/entry", //hardcoded due to the UI requiring this and it is not configurable "/entry", //hardcoded due to the UI requiring this and it is not configurable
listener, listener,
@ -71,21 +68,5 @@ func GetMessages() []models.ChatMessage {
return []models.ChatMessage{} return []models.ChatMessage{}
} }
return getRecentMessages() return getChatHistory()
}
func getRecentMessages() []models.ChatMessage {
if len(_server.Messages) < 100 {
return _server.Messages
}
maxAgeInHours := float64(2)
messages := make([]models.ChatMessage, 0)
for _, message := range _server.Messages {
if time.Since(message.Timestamp).Hours() < maxAgeInHours {
messages = append(messages, message)
}
}
return messages
} }

View file

@ -18,8 +18,7 @@ var (
//Server represents the server which handles the chat //Server represents the server which handles the chat
type server struct { type server struct {
Messages []models.ChatMessage Clients map[string]*Client
Clients map[string]*Client
pattern string pattern string
listener models.ChatListener listener models.ChatListener
@ -113,7 +112,6 @@ func (s *server) Listen() {
// broadcast a message to all clients // broadcast a message to all clients
case msg := <-s.sendAllCh: case msg := <-s.sendAllCh:
s.Messages = append(s.Messages, msg)
s.listener.MessageSent(msg) s.listener.MessageSent(msg)
s.sendAll(msg) s.sendAll(msg)
addMessage(msg) addMessage(msg)