From c85b8d0df7ae1509eac7e6a08cf738356846fb4d Mon Sep 17 00:00:00 2001 From: Jibon Date: Tue, 17 Oct 2023 17:07:36 +0200 Subject: [PATCH] fixed: race condition during add analytics value --- .air.toml | 2 +- pkg/models/analytics.go | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.air.toml b/.air.toml index 2f0b98cb..1cd09a90 100644 --- a/.air.toml +++ b/.air.toml @@ -5,7 +5,7 @@ tmp_dir = "./etc/tmp" [build] args_bin = [] bin = "./etc/tmp/main" - cmd = "go build -o ./etc/tmp/main cmd/server/*.go" + cmd = "go build -race -o ./etc/tmp/main cmd/server/*.go" delay = 1000 exclude_dir = ["tmp", "test", "log", "etc", "github_files", "sql_dump", "upload", "recording_files", "client"] exclude_file = [] diff --git a/pkg/models/analytics.go b/pkg/models/analytics.go index d6b73344..6c11a65c 100644 --- a/pkg/models/analytics.go +++ b/pkg/models/analytics.go @@ -11,6 +11,7 @@ import ( "os" "strconv" "strings" + "sync" "time" ) @@ -21,6 +22,7 @@ const ( ) type AnalyticsModel struct { + sync.RWMutex rc *redis.Client ctx context.Context data *plugnmeet.AnalyticsDataMsg @@ -38,9 +40,12 @@ func (m *AnalyticsModel) HandleEvent(d *plugnmeet.AnalyticsDataMsg) { !config.AppCnf.AnalyticsSettings.Enabled { return } + + m.Lock() // we'll use unix milliseconds to make sure fields are unique d.Time = time.Now().UnixMilli() m.data = d + m.Unlock() switch d.EventType { case plugnmeet.AnalyticsEventType_ANALYTICS_EVENT_TYPE_ROOM: @@ -80,6 +85,9 @@ func (m *AnalyticsModel) HandleWebSocketData(dataMsg *plugnmeet.DataMessage) { } func (m *AnalyticsModel) handleRoomTypeEvents() { + m.Lock() + defer m.Unlock() + if m.data.EventName == plugnmeet.AnalyticsEvents_ANALYTICS_EVENT_UNKNOWN { return } @@ -96,6 +104,9 @@ func (m *AnalyticsModel) handleRoomTypeEvents() { } func (m *AnalyticsModel) handleUserTypeEvents() { + m.Lock() + defer m.Unlock() + if m.data.EventName == plugnmeet.AnalyticsEvents_ANALYTICS_EVENT_UNKNOWN { return }