Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce spam #715

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions pkg/stats/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,30 +140,35 @@ func (m *Monitor) validateCPUConfig() error {
}

func (m *Monitor) CanAcceptWebRequest() bool {
m.mu.Lock()
defer m.mu.Unlock()

return m.canAcceptRequestLocked(&rpc.StartEgressRequest{
Request: &rpc.StartEgressRequest_Web{},
})
return m.webRequests.Load() < m.cpuCostConfig.MaxConcurrentWeb
}

func (m *Monitor) CanAcceptRequest(req *rpc.StartEgressRequest) bool {
m.mu.Lock()
defer m.mu.Unlock()
fields, canAccept := m.canAcceptRequestLocked(req)
m.mu.Unlock()

return m.canAcceptRequestLocked(req)
logger.Debugw("cpu check", fields...)
return canAccept
}

func (m *Monitor) canAcceptRequestLocked(req *rpc.StartEgressRequest) bool {
func (m *Monitor) canAcceptRequestLocked(req *rpc.StartEgressRequest) ([]interface{}, bool) {
total, available, pending, used := m.getCPUUsageLocked()
fields := []interface{}{
"total", total,
"available", available,
"pending", pending,
"used", used,
"activeRequests", m.requests.Load(),
"activeWeb", m.webRequests.Load(),
}

var accept bool
var required float64
switch r := req.Request.(type) {
case *rpc.StartEgressRequest_RoomComposite:
if m.webRequests.Load() >= m.cpuCostConfig.MaxConcurrentWeb {
return false
return fields, false
}
if r.RoomComposite.AudioOnly {
required = m.cpuCostConfig.AudioRoomCompositeCpuCost
Expand All @@ -172,7 +177,7 @@ func (m *Monitor) canAcceptRequestLocked(req *rpc.StartEgressRequest) bool {
}
case *rpc.StartEgressRequest_Web:
if m.webRequests.Load() >= m.cpuCostConfig.MaxConcurrentWeb {
return false
return fields, false
}
if r.Web.AudioOnly {
required = m.cpuCostConfig.AudioWebCpuCost
Expand All @@ -188,17 +193,12 @@ func (m *Monitor) canAcceptRequestLocked(req *rpc.StartEgressRequest) bool {
}
accept = available >= required

logger.Debugw("cpu check",
"total", total,
"pending", pending,
"used", used,
fields = append(fields,
"required", required,
"available", available,
"activeRequests", m.requests.Load(),
"canAccept", accept,
)

return accept
return fields, accept
}

func (m *Monitor) AcceptRequest(req *rpc.StartEgressRequest) error {
Expand All @@ -208,7 +208,8 @@ func (m *Monitor) AcceptRequest(req *rpc.StartEgressRequest) error {
if m.pending[req.EgressId] != nil {
return errors.ErrEgressAlreadyExists
}
if !m.canAcceptRequestLocked(req) {
if _, ok := m.canAcceptRequestLocked(req); !ok {
logger.Warnw("can not accept request", nil)
return errors.ErrNotEnoughCPU
}

Expand Down
8 changes: 7 additions & 1 deletion pkg/stats/monitor_prom.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package stats

import (
"github.com/prometheus/client_golang/prometheus"

"github.com/livekit/protocol/rpc"
)

func (m *Monitor) initPrometheus() {
Expand Down Expand Up @@ -72,7 +74,11 @@ func (m *Monitor) promIsIdle() float64 {
}

func (m *Monitor) promCanAcceptRequest() float64 {
if !m.svc.IsDisabled() && m.CanAcceptWebRequest() {
m.mu.Lock()
_, canAccept := m.canAcceptRequestLocked(&rpc.StartEgressRequest{Request: &rpc.StartEgressRequest_Web{}})
m.mu.Unlock()

if !m.svc.IsDisabled() && canAccept {
return 1
}
return 0
Expand Down
Loading