Merge commit '0da97ebd2149ec2a0412a273b17270f540431d81' as 'livekit-server'
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright 2023 LiveKit, Inc
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package utils
|
||||
|
||||
import "sync"
|
||||
|
||||
type ChangeNotifier struct {
|
||||
lock sync.Mutex
|
||||
observers map[string]func()
|
||||
}
|
||||
|
||||
func NewChangeNotifier() *ChangeNotifier {
|
||||
return &ChangeNotifier{
|
||||
observers: make(map[string]func()),
|
||||
}
|
||||
}
|
||||
|
||||
func (n *ChangeNotifier) AddObserver(key string, onChanged func()) {
|
||||
n.lock.Lock()
|
||||
defer n.lock.Unlock()
|
||||
|
||||
n.observers[key] = onChanged
|
||||
}
|
||||
|
||||
func (n *ChangeNotifier) RemoveObserver(key string) {
|
||||
n.lock.Lock()
|
||||
defer n.lock.Unlock()
|
||||
|
||||
delete(n.observers, key)
|
||||
}
|
||||
|
||||
func (n *ChangeNotifier) HasObservers() bool {
|
||||
n.lock.Lock()
|
||||
defer n.lock.Unlock()
|
||||
|
||||
return len(n.observers) > 0
|
||||
}
|
||||
|
||||
func (n *ChangeNotifier) NotifyChanged() {
|
||||
n.lock.Lock()
|
||||
if len(n.observers) == 0 {
|
||||
n.lock.Unlock()
|
||||
return
|
||||
}
|
||||
observers := make([]func(), 0, len(n.observers))
|
||||
for _, f := range n.observers {
|
||||
observers = append(observers, f)
|
||||
}
|
||||
n.lock.Unlock()
|
||||
|
||||
go func() {
|
||||
for _, f := range observers {
|
||||
f()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
type ChangeNotifierManager struct {
|
||||
lock sync.Mutex
|
||||
notifiers map[string]*ChangeNotifier
|
||||
}
|
||||
|
||||
func NewChangeNotifierManager() *ChangeNotifierManager {
|
||||
return &ChangeNotifierManager{
|
||||
notifiers: make(map[string]*ChangeNotifier),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *ChangeNotifierManager) GetNotifier(key string) *ChangeNotifier {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
|
||||
return m.notifiers[key]
|
||||
}
|
||||
|
||||
func (m *ChangeNotifierManager) GetOrCreateNotifier(key string) *ChangeNotifier {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
|
||||
if notifier, ok := m.notifiers[key]; ok {
|
||||
return notifier
|
||||
}
|
||||
|
||||
notifier := NewChangeNotifier()
|
||||
m.notifiers[key] = notifier
|
||||
return notifier
|
||||
}
|
||||
|
||||
func (m *ChangeNotifierManager) RemoveNotifier(key string, force bool) {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
|
||||
if notifier, ok := m.notifiers[key]; ok {
|
||||
if force || !notifier.HasObservers() {
|
||||
delete(m.notifiers, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2023 LiveKit, Inc
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/livekit/protocol/logger"
|
||||
)
|
||||
|
||||
type attemptKey struct{}
|
||||
type loggerKey = struct{}
|
||||
|
||||
func ContextWithAttempt(ctx context.Context, attempt int) context.Context {
|
||||
return context.WithValue(ctx, attemptKey{}, attempt)
|
||||
}
|
||||
|
||||
func GetAttempt(ctx context.Context) int {
|
||||
if attempt, ok := ctx.Value(attemptKey{}).(int); ok {
|
||||
return attempt
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func ContextWithLogger(ctx context.Context, logger logger.Logger) context.Context {
|
||||
return context.WithValue(ctx, loggerKey{}, logger)
|
||||
}
|
||||
|
||||
func GetLogger(ctx context.Context) logger.Logger {
|
||||
if l, ok := ctx.Value(loggerKey{}).(logger.Logger); ok {
|
||||
return l
|
||||
}
|
||||
return logger.GetLogger()
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/jellydator/ttlcache/v3"
|
||||
|
||||
"github.com/livekit/protocol/livekit"
|
||||
)
|
||||
|
||||
const (
|
||||
iceConfigTTLMin = 5 * time.Minute
|
||||
)
|
||||
|
||||
type IceConfigCache[T comparable] struct {
|
||||
c *ttlcache.Cache[T, *livekit.ICEConfig]
|
||||
}
|
||||
|
||||
func NewIceConfigCache[T comparable](ttl time.Duration) *IceConfigCache[T] {
|
||||
cache := ttlcache.New(
|
||||
ttlcache.WithTTL[T, *livekit.ICEConfig](max(ttl, iceConfigTTLMin)),
|
||||
ttlcache.WithDisableTouchOnHit[T, *livekit.ICEConfig](),
|
||||
)
|
||||
go cache.Start()
|
||||
|
||||
return &IceConfigCache[T]{cache}
|
||||
}
|
||||
|
||||
func (icc *IceConfigCache[T]) Stop() {
|
||||
icc.c.Stop()
|
||||
}
|
||||
|
||||
func (icc *IceConfigCache[T]) Put(key T, iceConfig *livekit.ICEConfig) {
|
||||
icc.c.Set(key, iceConfig, ttlcache.DefaultTTL)
|
||||
}
|
||||
|
||||
func (icc *IceConfigCache[T]) Get(key T) *livekit.ICEConfig {
|
||||
if it := icc.c.Get(key); it != nil {
|
||||
return it.Value()
|
||||
}
|
||||
return &livekit.ICEConfig{}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/livekit/protocol/livekit"
|
||||
)
|
||||
|
||||
func TestIceConfigCache(t *testing.T) {
|
||||
cache := NewIceConfigCache[string](10 * time.Second)
|
||||
t.Cleanup(cache.Stop)
|
||||
|
||||
cache.Put("test", &livekit.ICEConfig{})
|
||||
require.NotNil(t, cache)
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright 2024 LiveKit, Inc
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/frostbyte73/core"
|
||||
)
|
||||
|
||||
// IncrementalDispatcher is a dispatcher that allows multiple consumers to consume items as they become
|
||||
// available, while producers can add items at anytime.
|
||||
type IncrementalDispatcher[T any] struct {
|
||||
done core.Fuse
|
||||
lock sync.RWMutex
|
||||
cond *sync.Cond
|
||||
items []T
|
||||
}
|
||||
|
||||
func NewIncrementalDispatcher[T any]() *IncrementalDispatcher[T] {
|
||||
p := &IncrementalDispatcher[T]{}
|
||||
p.cond = sync.NewCond(&p.lock)
|
||||
return p
|
||||
}
|
||||
|
||||
func (d *IncrementalDispatcher[T]) Add(item T) {
|
||||
if d.done.IsBroken() {
|
||||
return
|
||||
}
|
||||
d.lock.Lock()
|
||||
d.items = append(d.items, item)
|
||||
d.lock.Unlock()
|
||||
d.cond.Broadcast()
|
||||
}
|
||||
|
||||
func (d *IncrementalDispatcher[T]) Done() {
|
||||
d.lock.Lock()
|
||||
d.done.Break()
|
||||
d.cond.Broadcast()
|
||||
d.lock.Unlock()
|
||||
}
|
||||
|
||||
func (d *IncrementalDispatcher[T]) ForEach(fn func(T)) {
|
||||
idx := 0
|
||||
dispatchFromIdx := func() {
|
||||
var itemsToDispatch []T
|
||||
d.lock.RLock()
|
||||
for idx < len(d.items) {
|
||||
itemsToDispatch = append(itemsToDispatch, d.items[idx])
|
||||
idx++
|
||||
}
|
||||
d.lock.RUnlock()
|
||||
for _, item := range itemsToDispatch {
|
||||
fn(item)
|
||||
}
|
||||
}
|
||||
for !d.done.IsBroken() {
|
||||
dispatchFromIdx()
|
||||
d.lock.Lock()
|
||||
// need to check again because Done may have been called while dispatching
|
||||
if d.done.IsBroken() {
|
||||
d.lock.Unlock()
|
||||
break
|
||||
}
|
||||
if idx == len(d.items) {
|
||||
d.cond.Wait()
|
||||
}
|
||||
d.lock.Unlock()
|
||||
}
|
||||
|
||||
dispatchFromIdx()
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright 2024 LiveKit, Inc
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package utils_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go.uber.org/atomic"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/testutils"
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
)
|
||||
|
||||
func TestForEach(t *testing.T) {
|
||||
producer := utils.NewIncrementalDispatcher[int]()
|
||||
go func() {
|
||||
defer producer.Done()
|
||||
producer.Add(1)
|
||||
producer.Add(2)
|
||||
producer.Add(3)
|
||||
}()
|
||||
|
||||
sum := 0
|
||||
producer.ForEach(func(item int) {
|
||||
sum += item
|
||||
})
|
||||
|
||||
require.Equal(t, 6, sum)
|
||||
}
|
||||
|
||||
func TestConcurrentConsumption(t *testing.T) {
|
||||
producer := utils.NewIncrementalDispatcher[int]()
|
||||
numConsumers := 100
|
||||
sums := make([]atomic.Int32, numConsumers)
|
||||
var wg sync.WaitGroup
|
||||
|
||||
for i := 0; i < numConsumers; i++ {
|
||||
wg.Add(1)
|
||||
i := i
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
producer.ForEach(func(item int) {
|
||||
sums[i].Add(int32(item))
|
||||
})
|
||||
}()
|
||||
}
|
||||
|
||||
// Add items
|
||||
expectedSum := 0
|
||||
for i := 0; i < 20; i++ {
|
||||
expectedSum += i
|
||||
producer.Add(i)
|
||||
}
|
||||
|
||||
for i := 0; i < numConsumers; i++ {
|
||||
testutils.WithTimeout(t, func() string {
|
||||
if sums[i].Load() != int32(expectedSum) {
|
||||
return fmt.Sprintf("consumer %d did not consume all the items. expected %d, actual: %d",
|
||||
i, expectedSum, sums[i].Load())
|
||||
}
|
||||
return ""
|
||||
}, time.Second)
|
||||
}
|
||||
|
||||
// keep adding and ensure it's consumed
|
||||
for i := 20; i < 30; i++ {
|
||||
expectedSum += i
|
||||
producer.Add(i)
|
||||
}
|
||||
|
||||
// wait for all consumers to finish
|
||||
producer.Done()
|
||||
wg.Wait()
|
||||
|
||||
for i := 0; i < numConsumers; i++ {
|
||||
require.Equal(t, int32(expectedSum), sums[i].Load(), "consumer %d did not match", i)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2023 LiveKit, Inc
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package utils
|
||||
|
||||
const (
|
||||
ComponentPub = "pub"
|
||||
ComponentSub = "sub"
|
||||
ComponentRoom = "room"
|
||||
ComponentAPI = "api"
|
||||
ComponentTransport = "transport"
|
||||
ComponentSFU = "sfu"
|
||||
// transport subcomponents
|
||||
ComponentCongestionControl = "cc"
|
||||
)
|
||||
@@ -0,0 +1,36 @@
|
||||
// Copyright 2023 LiveKit, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package utils
|
||||
|
||||
import "sort"
|
||||
|
||||
// MedianFloat32 gets median value for an array of float32
|
||||
func MedianFloat32(input []float32) float32 {
|
||||
num := len(input)
|
||||
if num == 0 {
|
||||
return 0
|
||||
} else if num == 1 {
|
||||
return input[0]
|
||||
}
|
||||
sort.Slice(input, func(i, j int) bool {
|
||||
return input[i] < input[j]
|
||||
})
|
||||
if num%2 != 0 {
|
||||
return input[num/2]
|
||||
}
|
||||
left := input[num/2-1]
|
||||
right := input[num/2]
|
||||
return (left + right) / 2
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
// Copyright 2023 LiveKit, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/gammazero/deque"
|
||||
|
||||
"github.com/livekit/protocol/logger"
|
||||
)
|
||||
|
||||
type OpsQueueParams struct {
|
||||
Name string
|
||||
MinSize uint
|
||||
FlushOnStop bool
|
||||
Logger logger.Logger
|
||||
}
|
||||
|
||||
type UntypedQueueOp func()
|
||||
|
||||
func (op UntypedQueueOp) run() {
|
||||
op()
|
||||
}
|
||||
|
||||
type OpsQueue struct {
|
||||
opsQueueBase[UntypedQueueOp]
|
||||
}
|
||||
|
||||
func NewOpsQueue(params OpsQueueParams) *OpsQueue {
|
||||
return &OpsQueue{*newOpsQueueBase[UntypedQueueOp](params)}
|
||||
}
|
||||
|
||||
type typedQueueOp[T any] struct {
|
||||
fn func(T)
|
||||
arg T
|
||||
}
|
||||
|
||||
func (op typedQueueOp[T]) run() {
|
||||
op.fn(op.arg)
|
||||
}
|
||||
|
||||
type TypedOpsQueue[T any] struct {
|
||||
opsQueueBase[typedQueueOp[T]]
|
||||
}
|
||||
|
||||
func NewTypedOpsQueue[T any](params OpsQueueParams) *TypedOpsQueue[T] {
|
||||
return &TypedOpsQueue[T]{*newOpsQueueBase[typedQueueOp[T]](params)}
|
||||
}
|
||||
|
||||
func (oq *TypedOpsQueue[T]) Enqueue(fn func(T), arg T) {
|
||||
oq.opsQueueBase.Enqueue(typedQueueOp[T]{fn, arg})
|
||||
}
|
||||
|
||||
type opsQueueItem interface {
|
||||
run()
|
||||
}
|
||||
|
||||
type opsQueueBase[T opsQueueItem] struct {
|
||||
params OpsQueueParams
|
||||
|
||||
lock sync.Mutex
|
||||
ops deque.Deque[T]
|
||||
wake chan struct{}
|
||||
isStarted bool
|
||||
doneChan chan struct{}
|
||||
isStopped bool
|
||||
}
|
||||
|
||||
func newOpsQueueBase[T opsQueueItem](params OpsQueueParams) *opsQueueBase[T] {
|
||||
o := &opsQueueBase[T]{
|
||||
params: params,
|
||||
wake: make(chan struct{}, 1),
|
||||
doneChan: make(chan struct{}),
|
||||
}
|
||||
o.ops.SetBaseCap(int(min(params.MinSize, 128)))
|
||||
return o
|
||||
}
|
||||
|
||||
func (oq *opsQueueBase[T]) Start() {
|
||||
oq.lock.Lock()
|
||||
if oq.isStarted {
|
||||
oq.lock.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
oq.isStarted = true
|
||||
oq.lock.Unlock()
|
||||
|
||||
go oq.process()
|
||||
}
|
||||
|
||||
func (oq *opsQueueBase[T]) Stop() <-chan struct{} {
|
||||
oq.lock.Lock()
|
||||
if oq.isStopped {
|
||||
oq.lock.Unlock()
|
||||
return oq.doneChan
|
||||
}
|
||||
|
||||
oq.isStopped = true
|
||||
close(oq.wake)
|
||||
oq.lock.Unlock()
|
||||
return oq.doneChan
|
||||
}
|
||||
|
||||
func (oq *opsQueueBase[T]) Enqueue(op T) {
|
||||
oq.lock.Lock()
|
||||
defer oq.lock.Unlock()
|
||||
|
||||
if oq.isStopped {
|
||||
return
|
||||
}
|
||||
|
||||
oq.ops.PushBack(op)
|
||||
if oq.ops.Len() == 1 {
|
||||
select {
|
||||
case oq.wake <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (oq *opsQueueBase[T]) process() {
|
||||
defer close(oq.doneChan)
|
||||
|
||||
for {
|
||||
<-oq.wake
|
||||
for {
|
||||
oq.lock.Lock()
|
||||
if oq.isStopped && (!oq.params.FlushOnStop || oq.ops.Len() == 0) {
|
||||
oq.lock.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
if oq.ops.Len() == 0 {
|
||||
oq.lock.Unlock()
|
||||
break
|
||||
}
|
||||
op := oq.ops.PopFront()
|
||||
oq.lock.Unlock()
|
||||
|
||||
op.run()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2023 LiveKit, Inc
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/utils"
|
||||
)
|
||||
|
||||
func ClientInfoWithoutAddress(c *livekit.ClientInfo) *livekit.ClientInfo {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
clone := utils.CloneProto(c)
|
||||
clone.Address = ""
|
||||
return clone
|
||||
}
|
||||
Reference in New Issue
Block a user