update to utils parallel execute (#1450)
* log change * remove identity field * update to protocol parallel execute * update protocol
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
package sfu
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
"go.uber.org/atomic"
|
||||
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/utils"
|
||||
)
|
||||
|
||||
type DownTrackSpreaderParams struct {
|
||||
@@ -21,18 +19,12 @@ type DownTrackSpreader struct {
|
||||
downTrackMu sync.RWMutex
|
||||
downTracks map[livekit.ParticipantID]TrackSender
|
||||
downTracksShadow []TrackSender
|
||||
numProcs int
|
||||
}
|
||||
|
||||
func NewDownTrackSpreader(params DownTrackSpreaderParams) *DownTrackSpreader {
|
||||
d := &DownTrackSpreader{
|
||||
params: params,
|
||||
downTracks: make(map[livekit.ParticipantID]TrackSender),
|
||||
numProcs: runtime.NumCPU(),
|
||||
}
|
||||
|
||||
if runtime.GOMAXPROCS(0) < d.numProcs {
|
||||
d.numProcs = runtime.GOMAXPROCS(0)
|
||||
}
|
||||
|
||||
return d
|
||||
@@ -83,39 +75,17 @@ func (d *DownTrackSpreader) HasDownTrack(subscriberID livekit.ParticipantID) boo
|
||||
|
||||
func (d *DownTrackSpreader) Broadcast(writer func(TrackSender)) {
|
||||
downTracks := d.GetDownTracks()
|
||||
if d.params.Threshold == 0 || (len(downTracks)) < d.params.Threshold {
|
||||
// serial - not enough down tracks for parallelization to outweigh overhead
|
||||
for _, dt := range downTracks {
|
||||
writer(dt)
|
||||
}
|
||||
} else {
|
||||
// parallel - enables much more efficient multi-core utilization
|
||||
start := atomic.NewUint64(0)
|
||||
end := uint64(len(downTracks))
|
||||
|
||||
// 100µs is enough to amortize the overhead and provide sufficient load balancing.
|
||||
// WriteRTP takes about 50µs on average, so we write to 2 down tracks per loop.
|
||||
step := uint64(2)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(d.numProcs)
|
||||
for p := 0; p < d.numProcs; p++ {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for {
|
||||
n := start.Add(step)
|
||||
if n >= end+step {
|
||||
return
|
||||
}
|
||||
|
||||
for i := n - step; i < n && i < end; i++ {
|
||||
writer(downTracks[i])
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
threshold := uint64(d.params.Threshold)
|
||||
if threshold == 0 {
|
||||
threshold = 1000000
|
||||
}
|
||||
|
||||
// 100µs is enough to amortize the overhead and provide sufficient load balancing.
|
||||
// WriteRTP takes about 50µs on average, so we write to 2 down tracks per loop.
|
||||
step := uint64(2)
|
||||
utils.ParallelExec(downTracks, threshold, step, func(dt TrackSender) {
|
||||
writer(dt)
|
||||
})
|
||||
}
|
||||
|
||||
func (d *DownTrackSpreader) DownTrackCount() int {
|
||||
|
||||
Reference in New Issue
Block a user