From a51629f7b5069f8a398aff29b3318d79bdddcddf Mon Sep 17 00:00:00 2001 From: claudemiro Date: Fri, 16 Jan 2015 13:17:22 -0300 Subject: [PATCH] Removing empty channels. Removing empty channels when the channel becomes vacated. --- TODO.org | 4 ++-- app.go | 23 +++++++++++++++++++++++ conn.go | 5 ++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/TODO.org b/TODO.org index 26d12ac..76d359e 100644 --- a/TODO.org +++ b/TODO.org @@ -8,10 +8,10 @@ IPÊ * [ ] Escrever testes automatizados * [ ] SSL * [X] Expvar - Canais, inscritos - * [-] Otimizações [1/3] + * [-] Otimizações [2/3] * [ ] Refatorar partes do código, remover repetições * [X] Alterar tipos de dados de slices para mapas em alguns locais. - * [ ] Remover Canais vazios com uma go routine - Uma especie de coletor de lixo + * [X] Remover Canais vazios. * [ ] Segurança, tempo de expiração, etc * [X] Dados extra na conexão do usuário. Ver Websockets onOpen * [X] Webhooks [5/5] diff --git a/app.go b/app.go index 307b9b4..121b030 100644 --- a/app.go +++ b/app.go @@ -134,6 +134,29 @@ func (a *App) FindConnection(socketID string) (*Connection, error) { return nil, errors.New("Connection not found") } +// DeleteChannel removes the channel from app +func (a *App) RemoveChannel(c *Channel) { + log.Infof("Remove the channel %s from app %s", c.ChannelID, a.Name) + a.Lock() + defer a.Unlock() + + delete(a.Channels, c.ChannelID) + + if c.IsPresence() { + a.Stats.Add("TotalPresenceChannels", -1) + } + + if c.IsPrivate() { + a.Stats.Add("TotalPrivateChannels", -1) + } + + if c.IsPublic() { + a.Stats.Add("TotalPublicChannels", -1) + } + + a.Stats.Add("TotalChannels", -1) +} + // Add a new Channel to this APP func (a *App) AddChannel(c *Channel) { log.Infof("Adding a new channel %s to app %s", c.ChannelID, a.Name) diff --git a/conn.go b/conn.go index 4a075aa..34dd9e7 100644 --- a/conn.go +++ b/conn.go @@ -176,9 +176,12 @@ func (c *Channel) Unsubscribe(a *App, conn *Connection) error { a.TriggerMemberRemovedHook(c, subscription) } - // WebHook if !c.IsOccupied() { + // WebHook a.TriggerChannelVacatedHook(c) + + // Remove the empty Channel + a.RemoveChannel(c) } return nil