Skip to content

Commit

Permalink
fixed: race condition during add analytics value
Browse files Browse the repository at this point in the history
  • Loading branch information
jibon57 committed Oct 17, 2023
1 parent 988a27e commit c85b8d0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
11 changes: 11 additions & 0 deletions pkg/models/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"strconv"
"strings"
"sync"
"time"
)

Expand All @@ -21,6 +22,7 @@ const (
)

type AnalyticsModel struct {
sync.RWMutex
rc *redis.Client
ctx context.Context
data *plugnmeet.AnalyticsDataMsg
Expand All @@ -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:
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit c85b8d0

Please sign in to comment.