From 9b9298b9270d558c9b7cca9cf7d4faf540f4be25 Mon Sep 17 00:00:00 2001 From: cnderrauber Date: Wed, 6 Sep 2023 10:18:20 +0800 Subject: [PATCH] Add batch i/o to improve throughput (#2033) * Add batch i/o to improve throughput * remove empty line * Solve comments * Change rtcconfig.UDPPort to PortRange * Fix test --- cmd/server/commands.go | 5 +++-- cmd/server/main.go | 3 ++- config-sample.yaml | 11 ++++++++--- go.mod | 2 +- go.sum | 4 ++-- pkg/config/config.go | 3 +-- pkg/rtc/participant_internal_test.go | 1 - pkg/service/roommanager.go | 2 +- pkg/service/server.go | 2 +- test/integration_helpers.go | 3 ++- 10 files changed, 21 insertions(+), 15 deletions(-) diff --git a/cmd/server/commands.go b/cmd/server/commands.go index 99499a50..624af634 100644 --- a/cmd/server/commands.go +++ b/cmd/server/commands.go @@ -55,8 +55,9 @@ func printPorts(c *cli.Context) error { if conf.RTC.TCPPort != 0 { tcpPorts = append(tcpPorts, fmt.Sprintf("%d - ICE/TCP", conf.RTC.TCPPort)) } - if conf.RTC.UDPPort != 0 { - udpPorts = append(udpPorts, fmt.Sprintf("%d - ICE/UDP", conf.RTC.UDPPort)) + if conf.RTC.UDPPort.Valid() { + portStr, _ := conf.RTC.UDPPort.MarshalYAML() + udpPorts = append(udpPorts, fmt.Sprintf("%s - ICE/UDP", portStr)) } else { udpPorts = append(udpPorts, fmt.Sprintf("%d-%d - ICE/UDP range", conf.RTC.ICEPortRangeStart, conf.RTC.ICEPortRangeEnd)) } diff --git a/cmd/server/main.go b/cmd/server/main.go index 4d37142f..149d4cb2 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -29,6 +29,7 @@ import ( "github.com/livekit/livekit-server/pkg/rtc" "github.com/livekit/livekit-server/pkg/telemetry/prometheus" + "github.com/livekit/mediatransportutil/pkg/rtcconfig" "github.com/livekit/protocol/logger" "github.com/livekit/livekit-server/pkg/config" @@ -206,7 +207,7 @@ func getConfig(c *cli.Context) (*config.Config, error) { if c.String("config") == "" && c.String("config-body") == "" && conf.Development { // use single port UDP when no config is provided - conf.RTC.UDPPort = 7882 + conf.RTC.UDPPort = rtcconfig.PortRange{Start: 7882} conf.RTC.ICEPortRangeStart = 0 conf.RTC.ICEPortRangeEnd = 0 logger.Infow("starting in development mode") diff --git a/config-sample.yaml b/config-sample.yaml index c800abd1..0b44b11b 100644 --- a/config-sample.yaml +++ b/config-sample.yaml @@ -43,10 +43,10 @@ rtc: # that maps to an external one use_external_ip: true # # when set, LiveKit will attempt to use a UDP mux so all UDP traffic goes through - # # a single port. This simplifies deployment, but mux will become an overhead for - # # highly trafficked deployments. + # # listed port(s). To maximize system performance, we recommend using a range of ports + # # greater or equal to the number of vCPUs on the machine. # # port_range_start & end must not be set for this config to take effect - # udp_port: 7882 + # udp_port: 7882-7892 # # when set to true, server will use a lite ice agent, that will speed up ice connection, but # # might cause connect issue if server running behind NAT. # use_ice_lite: true @@ -107,6 +107,11 @@ rtc: # # are disabled and clients don't ACK opened peer connections, only reliable, ordered delivery # # will be available. # strict_acks: true + # # enable batch write to merge network write system calls to reduce cpu usage. Outgoing packets + # # will be queued until length of queue equal to `batch_size` or time elapsed since last write exceeds `max_flush_interval`. + # batch_io: + # batch_size: 128 + # max_flush_interval: 2ms # when enabled, LiveKit will expose prometheus metrics on :6789/metrics # prometheus_port: 6789 diff --git a/go.mod b/go.mod index 6150a168..b0fb0813 100644 --- a/go.mod +++ b/go.mod @@ -17,7 +17,7 @@ require ( github.com/hashicorp/golang-lru/v2 v2.0.5 github.com/jxskiss/base62 v1.1.0 github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1 - github.com/livekit/mediatransportutil v0.0.0-20230823131232-12f579dc9af0 + github.com/livekit/mediatransportutil v0.0.0-20230905070227-1decac7a3c61 github.com/livekit/protocol v1.6.2-0.20230828184341-dfb5162c7c86 github.com/livekit/psrpc v0.3.3 github.com/mackerelio/go-osstat v0.2.4 diff --git a/go.sum b/go.sum index 071770a9..1b551c5e 100644 --- a/go.sum +++ b/go.sum @@ -123,8 +123,8 @@ github.com/lithammer/shortuuid/v4 v4.0.0 h1:QRbbVkfgNippHOS8PXDkti4NaWeyYfcBTHtw github.com/lithammer/shortuuid/v4 v4.0.0/go.mod h1:Zs8puNcrvf2rV9rTH51ZLLcj7ZXqQI3lv67aw4KiB1Y= github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1 h1:jm09419p0lqTkDaKb5iXdynYrzB84ErPPO4LbRASk58= github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ= -github.com/livekit/mediatransportutil v0.0.0-20230823131232-12f579dc9af0 h1:cHNvPzn6VHFcsHx8ZC9LwU/4jj22mW3LILrNg/y5A6I= -github.com/livekit/mediatransportutil v0.0.0-20230823131232-12f579dc9af0/go.mod h1:xirUXW8xnLGmfCwUeAv/nj1VGo1OO1BmgxrYP7jK/14= +github.com/livekit/mediatransportutil v0.0.0-20230905070227-1decac7a3c61 h1:B03f6QRPP8ESgthh4FdV6RTrW+ukEePMi4uDdAnJxKQ= +github.com/livekit/mediatransportutil v0.0.0-20230905070227-1decac7a3c61/go.mod h1:njHoM3uTstzLF5N/FyzZ96+TYdJkgeyRbCWBWBiyS2s= github.com/livekit/protocol v1.6.2-0.20230828184341-dfb5162c7c86 h1:QEzGhfIOmGdRw17xIldbYzb1MTsYuVfXSqz8FTyfjWQ= github.com/livekit/protocol v1.6.2-0.20230828184341-dfb5162c7c86/go.mod h1:/JuO+G/btZ5gNwX2+901L6za3UvVO6DHRXHsv8kkLsU= github.com/livekit/psrpc v0.3.3 h1:+lltbuN39IdaynXhLLxRShgYqYsRMWeeXKzv60oqyWo= diff --git a/pkg/config/config.go b/pkg/config/config.go index b8dc6044..e2f165cf 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -308,7 +308,6 @@ var DefaultConfig = Config{ RTCConfig: rtcconfig.RTCConfig{ UseExternalIP: false, TCPPort: 7881, - UDPPort: 0, ICEPortRangeStart: 0, ICEPortRangeEnd: 0, STUNServers: []string{}, @@ -802,7 +801,7 @@ func (conf *Config) updateFromCLI(c *cli.Context, baseFlags []cli.Flag) error { conf.RTC.NodeIP = c.String("node-ip") } if c.IsSet("udp-port") { - conf.RTC.UDPPort = uint32(c.Int("udp-port")) + conf.RTC.UDPPort = rtcconfig.PortRange{Start: (c.Int("udp-port"))} } if c.IsSet("bind") { conf.BindAddresses = c.StringSlice("bind") diff --git a/pkg/rtc/participant_internal_test.go b/pkg/rtc/participant_internal_test.go index 55f77387..1454a27d 100644 --- a/pkg/rtc/participant_internal_test.go +++ b/pkg/rtc/participant_internal_test.go @@ -661,7 +661,6 @@ func newParticipantForTestWithOpts(identity livekit.ParticipantIdentity, opts *p } conf, _ := config.NewConfig("", true, nil, nil) // disable mux, it doesn't play too well with unit test - conf.RTC.UDPPort = 0 conf.RTC.TCPPort = 0 rtcConf, err := NewWebRTCConfig(conf) if err != nil { diff --git a/pkg/service/roommanager.go b/pkg/service/roommanager.go index 6393e55b..9f26d1ab 100644 --- a/pkg/service/roommanager.go +++ b/pkg/service/roommanager.go @@ -556,7 +556,7 @@ func (r *RoomManager) rtcSessionWorker(room *rtc.Room, participant types.LocalPa _ = r.refreshToken(participant) tokenTicker := time.NewTicker(tokenRefreshInterval) defer tokenTicker.Stop() - stateCheckTicker := time.NewTicker(time.Millisecond * 50) + stateCheckTicker := time.NewTicker(time.Millisecond * 500) defer stateCheckTicker.Stop() for { select { diff --git a/pkg/service/server.go b/pkg/service/server.go index 69331599..c6755561 100644 --- a/pkg/service/server.go +++ b/pkg/service/server.go @@ -220,7 +220,7 @@ func (s *LivekitServer) Start() error { if s.config.RTC.TCPPort != 0 { values = append(values, "rtc.portTCP", s.config.RTC.TCPPort) } - if !s.config.RTC.ForceTCP && s.config.RTC.UDPPort != 0 { + if !s.config.RTC.ForceTCP && s.config.RTC.UDPPort.Valid() { values = append(values, "rtc.portUDP", s.config.RTC.UDPPort) } else { values = append(values, diff --git a/test/integration_helpers.go b/test/integration_helpers.go index 633c6a93..7a11b353 100644 --- a/test/integration_helpers.go +++ b/test/integration_helpers.go @@ -31,6 +31,7 @@ import ( "github.com/livekit/livekit-server/pkg/telemetry/prometheus" "github.com/livekit/livekit-server/pkg/testutils" testclient "github.com/livekit/livekit-server/test/client" + "github.com/livekit/mediatransportutil/pkg/rtcconfig" "github.com/livekit/protocol/auth" "github.com/livekit/protocol/livekit" "github.com/livekit/protocol/logger" @@ -178,7 +179,7 @@ func createMultiNodeServer(nodeID string, port uint32) *service.LivekitServer { panic(fmt.Sprintf("could not create config: %v", err)) } conf.Port = port - conf.RTC.UDPPort = port + 1 + conf.RTC.UDPPort = rtcconfig.PortRange{Start: int(port) + 1} conf.RTC.TCPPort = port + 2 conf.Redis.Address = "localhost:6379" conf.Keys = map[string]string{testApiKey: testApiSecret}