mirror of
https://github.com/livekit/livekit.git
synced 2026-05-18 15:36:07 +00:00
init ua parser once (#3883)
This commit is contained in:
+4
-4
@@ -177,15 +177,15 @@ func ChunkProtoBatch[T proto.Message](batch []T, target int) [][]T {
|
||||
var chunks [][]T
|
||||
var start, size int
|
||||
for i, m := range batch {
|
||||
if s := proto.Size(m); size+s > target {
|
||||
s := proto.Size(m)
|
||||
if size+s > target {
|
||||
if start < i {
|
||||
chunks = append(chunks, batch[start:i])
|
||||
}
|
||||
start = i
|
||||
size = s
|
||||
} else {
|
||||
size += s
|
||||
size = 0
|
||||
}
|
||||
size += s
|
||||
}
|
||||
if start < len(batch) {
|
||||
chunks = append(chunks, batch[start:])
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
package rtc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand/v2"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -63,12 +62,14 @@ func TestChunkProtoBatch(t *testing.T) {
|
||||
|
||||
target := 64 * 1024
|
||||
batches := ChunkProtoBatch(updates, target)
|
||||
var count int
|
||||
for _, b := range batches {
|
||||
var sum int
|
||||
for _, m := range b {
|
||||
sum += proto.Size(m)
|
||||
count++
|
||||
}
|
||||
require.True(t, sum < target || len(b) == 1, "batch size exceeds target")
|
||||
}
|
||||
fmt.Println(len(batches))
|
||||
require.Equal(t, len(updates), count)
|
||||
}
|
||||
|
||||
+16
-2
@@ -24,6 +24,9 @@ import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/ua-parser/uap-go/uaparser"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/config"
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
@@ -33,7 +36,6 @@ import (
|
||||
"github.com/livekit/protocol/auth"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/ua-parser/uap-go/uaparser"
|
||||
)
|
||||
|
||||
func handleError(w http.ResponseWriter, r *http.Request, status int, err error, keysAndValues ...interface{}) {
|
||||
@@ -155,6 +157,18 @@ func ParseClientInfo(r *http.Request) *livekit.ClientInfo {
|
||||
return ci
|
||||
}
|
||||
|
||||
var (
|
||||
userAgentParserCache *uaparser.Parser
|
||||
userAgentParserInit sync.Once
|
||||
)
|
||||
|
||||
func getUserAgentParser() *uaparser.Parser {
|
||||
userAgentParserInit.Do(func() {
|
||||
userAgentParserCache = uaparser.NewFromSaved()
|
||||
})
|
||||
return userAgentParserCache
|
||||
}
|
||||
|
||||
func AugmentClientInfo(ci *livekit.ClientInfo, req *http.Request) {
|
||||
// get real address (forwarded http header) - check Cloudflare headers first, fall back to X-Forwarded-For
|
||||
ci.Address = GetClientIP(req)
|
||||
@@ -164,7 +178,7 @@ func AugmentClientInfo(ci *livekit.ClientInfo, req *http.Request) {
|
||||
ci.Sdk == livekit.ClientInfo_REACT_NATIVE ||
|
||||
ci.Sdk == livekit.ClientInfo_FLUTTER ||
|
||||
ci.Sdk == livekit.ClientInfo_UNITY {
|
||||
client := uaparser.NewFromSaved().Parse(req.UserAgent())
|
||||
client := getUserAgentParser().Parse(req.UserAgent())
|
||||
if ci.Browser == "" {
|
||||
ci.Browser = client.UserAgent.Family
|
||||
ci.BrowserVersion = client.UserAgent.ToVersionString()
|
||||
|
||||
Reference in New Issue
Block a user