From f48c646208cb5227b8f6c6a5139bf9af66da802d Mon Sep 17 00:00:00 2001 From: Corey Hulen Date: Tue, 3 Jan 2017 11:45:45 -0500 Subject: Fixing should send event removing extra go channel creation (#4942) --- api/web_conn.go | 7 +------ store/sql_channel_store.go | 21 +++++++++++++++++++++ store/sql_channel_store_test.go | 8 ++++++++ store/store.go | 1 + 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/api/web_conn.go b/api/web_conn.go index 3b991d449..2f5036922 100644 --- a/api/web_conn.go +++ b/api/web_conn.go @@ -200,13 +200,8 @@ func (webCon *WebConn) ShouldSendEvent(msg *model.WebSocketEvent) bool { // Only broadcast typing messages if less than 1K people in channel if msg.Event == model.WEBSOCKET_EVENT_TYPING { - if result := <-Srv.Store.Channel().GetMemberCount(msg.Broadcast.ChannelId, true); result.Err != nil { - l4g.Error("webhub.shouldSendEvent: " + result.Err.Error()) + if Srv.Store.Channel().GetMemberCountFromCache(msg.Broadcast.ChannelId) > *utils.Cfg.TeamSettings.MaxNotificationsPerChannel { return false - } else { - if result.Data.(int64) > *utils.Cfg.TeamSettings.MaxNotificationsPerChannel { - return false - } } } diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index 7cdebba8a..2ab9d87c1 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -839,6 +839,27 @@ func (us SqlChannelStore) InvalidateMemberCount(channelId string) { channelMemberCountsCache.Remove(channelId) } +func (s SqlChannelStore) GetMemberCountFromCache(channelId string) int64 { + metrics := einterfaces.GetMetricsInterface() + + if cacheItem, ok := channelMemberCountsCache.Get(channelId); ok { + if metrics != nil { + metrics.IncrementMemCacheHitCounter("Channel Member Counts") + } + return cacheItem.(int64) + } else { + if metrics != nil { + metrics.IncrementMemCacheMissCounter("Channel Member Counts") + } + } + + if result := <-s.GetMemberCount(channelId, true); result.Err != nil { + return 0 + } else { + return result.Data.(int64) + } +} + func (s SqlChannelStore) GetMemberCount(channelId string, allowFromCache bool) StoreChannel { storeChannel := make(StoreChannel, 1) metrics := einterfaces.GetMetricsInterface() diff --git a/store/sql_channel_store_test.go b/store/sql_channel_store_test.go index 7aeef98cc..6186a242c 100644 --- a/store/sql_channel_store_test.go +++ b/store/sql_channel_store_test.go @@ -419,6 +419,14 @@ func TestChannelMemberStore(t *testing.T) { t.Fatal("should have saved 2 members") } + if store.Channel().GetMemberCountFromCache(o1.ChannelId) != 2 { + t.Fatal("should have saved 2 members") + } + + if store.Channel().GetMemberCountFromCache("junk") != 0 { + t.Fatal("should have saved 0 members") + } + count = (<-store.Channel().GetMemberCount(o1.ChannelId, false)).Data.(int64) if count != 2 { t.Fatal("should have saved 2 members") diff --git a/store/store.go b/store/store.go index 9fe566844..88a553b7c 100644 --- a/store/store.go +++ b/store/store.go @@ -108,6 +108,7 @@ type ChannelStore interface { IsUserInChannelUseCache(userId string, channelId string) bool GetMemberForPost(postId string, userId string) StoreChannel InvalidateMemberCount(channelId string) + GetMemberCountFromCache(channelId string) int64 GetMemberCount(channelId string, allowFromCache bool) StoreChannel RemoveMember(channelId string, userId string) StoreChannel PermanentDeleteMembersByUser(userId string) StoreChannel -- cgit v1.2.3-1-g7c22