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 chunks [][]T
|
||||||
var start, size int
|
var start, size int
|
||||||
for i, m := range batch {
|
for i, m := range batch {
|
||||||
if s := proto.Size(m); size+s > target {
|
s := proto.Size(m)
|
||||||
|
if size+s > target {
|
||||||
if start < i {
|
if start < i {
|
||||||
chunks = append(chunks, batch[start:i])
|
chunks = append(chunks, batch[start:i])
|
||||||
}
|
}
|
||||||
start = i
|
start = i
|
||||||
size = s
|
size = 0
|
||||||
} else {
|
|
||||||
size += s
|
|
||||||
}
|
}
|
||||||
|
size += s
|
||||||
}
|
}
|
||||||
if start < len(batch) {
|
if start < len(batch) {
|
||||||
chunks = append(chunks, batch[start:])
|
chunks = append(chunks, batch[start:])
|
||||||
|
|||||||
@@ -15,7 +15,6 @@
|
|||||||
package rtc
|
package rtc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"math/rand/v2"
|
"math/rand/v2"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -63,12 +62,14 @@ func TestChunkProtoBatch(t *testing.T) {
|
|||||||
|
|
||||||
target := 64 * 1024
|
target := 64 * 1024
|
||||||
batches := ChunkProtoBatch(updates, target)
|
batches := ChunkProtoBatch(updates, target)
|
||||||
|
var count int
|
||||||
for _, b := range batches {
|
for _, b := range batches {
|
||||||
var sum int
|
var sum int
|
||||||
for _, m := range b {
|
for _, m := range b {
|
||||||
sum += proto.Size(m)
|
sum += proto.Size(m)
|
||||||
|
count++
|
||||||
}
|
}
|
||||||
require.True(t, sum < target || len(b) == 1, "batch size exceeds target")
|
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"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/ua-parser/uap-go/uaparser"
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/pkg/config"
|
"github.com/livekit/livekit-server/pkg/config"
|
||||||
"github.com/livekit/livekit-server/pkg/routing"
|
"github.com/livekit/livekit-server/pkg/routing"
|
||||||
@@ -33,7 +36,6 @@ import (
|
|||||||
"github.com/livekit/protocol/auth"
|
"github.com/livekit/protocol/auth"
|
||||||
"github.com/livekit/protocol/livekit"
|
"github.com/livekit/protocol/livekit"
|
||||||
"github.com/livekit/protocol/logger"
|
"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{}) {
|
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
|
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) {
|
func AugmentClientInfo(ci *livekit.ClientInfo, req *http.Request) {
|
||||||
// get real address (forwarded http header) - check Cloudflare headers first, fall back to X-Forwarded-For
|
// get real address (forwarded http header) - check Cloudflare headers first, fall back to X-Forwarded-For
|
||||||
ci.Address = GetClientIP(req)
|
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_REACT_NATIVE ||
|
||||||
ci.Sdk == livekit.ClientInfo_FLUTTER ||
|
ci.Sdk == livekit.ClientInfo_FLUTTER ||
|
||||||
ci.Sdk == livekit.ClientInfo_UNITY {
|
ci.Sdk == livekit.ClientInfo_UNITY {
|
||||||
client := uaparser.NewFromSaved().Parse(req.UserAgent())
|
client := getUserAgentParser().Parse(req.UserAgent())
|
||||||
if ci.Browser == "" {
|
if ci.Browser == "" {
|
||||||
ci.Browser = client.UserAgent.Family
|
ci.Browser = client.UserAgent.Family
|
||||||
ci.BrowserVersion = client.UserAgent.ToVersionString()
|
ci.BrowserVersion = client.UserAgent.ToVersionString()
|
||||||
|
|||||||
Reference in New Issue
Block a user