Skip to content

Commit

Permalink
Make fix active entities interval dynamic according to openvidu.analy…
Browse files Browse the repository at this point in the history
…tics.interval
  • Loading branch information
pabloFuente committed Nov 27, 2024
1 parent 381b7d0 commit 8c07f8f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
24 changes: 17 additions & 7 deletions openvidu/analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ import (

const dbLockName = "analytics-db-operations-lock"

var ANALYTICS_CONFIGURATION *openviduconfig.AnalyticsConfig
var ANALYTICS_SENDERS []*AnalyticsSender
var redisLocker *redislock.Client = nil
var mutex sync.Mutex
var (
activeEntitiesFixerWaitInterval time.Duration = time.Minute
activeEntitiesLinearBackoff time.Duration
activeEntitiesTtl time.Duration

ANALYTICS_CONFIGURATION *openviduconfig.AnalyticsConfig
ANALYTICS_SENDERS []*AnalyticsSender
redisLocker *redislock.Client = nil
mutex sync.Mutex
)

type EntityType string

Expand Down Expand Up @@ -100,6 +106,10 @@ func InitializeAnalytics(configuration *config.Config, livekithelper livekithelp
ANALYTICS_CONFIGURATION = &configuration.OpenVidu.Analytics
ANALYTICS_SENDERS = []*AnalyticsSender{mongoDatabaseClient.owner}

activeEntitiesFixerWaitInterval = max(ANALYTICS_CONFIGURATION.Interval+time.Second*10, time.Minute)
activeEntitiesLinearBackoff = activeEntitiesFixerWaitInterval / 6
activeEntitiesTtl = activeEntitiesFixerWaitInterval + 5*time.Second

if configuration.Redis.IsConfigured() {
rc, err := redisLiveKit.GetRedisClient(&configuration.Redis)
if err != nil {
Expand Down Expand Up @@ -172,9 +182,9 @@ func startActiveEntitiesFixer() {
func() {
if redisLocker != nil {
context := context.Background()
backoff := redislock.LinearBackoff(10 * time.Second)
backoff := redislock.LinearBackoff(activeEntitiesLinearBackoff)

lock, err := redisLocker.Obtain(context, "active-entities-lock", 2*time.Minute, &redislock.Options{
lock, err := redisLocker.Obtain(context, "active-entities-lock", activeEntitiesTtl, &redislock.Options{
RetryStrategy: backoff,
})
if err != nil {
Expand Down Expand Up @@ -209,7 +219,7 @@ func startActiveEntitiesFixer() {
mutex.Unlock()
}

time.Sleep(time.Minute)
time.Sleep(activeEntitiesFixerWaitInterval)
}()
}
}
Expand Down
2 changes: 1 addition & 1 deletion openvidu/analytics/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func (m *MongoDatabaseClient) FixActiveEntities() {
* - There is another goroutine that runs every 10 seconds (by default) to send events to MongoDB.
* - This function runs every minute and may detect a closed entity as still active because the
* 10-second goroutine has not yet saved its authentic close event.
*
*
* To handle this, a fake close event for the entity is added to the list, and on the next iteration
* (after one minute), the process rechecks MongoDB to verify if an authentic close event has already
* been saved before performing further write or delete operations.
Expand Down

0 comments on commit 8c07f8f

Please sign in to comment.