Redis Router graceful stop (#116)

* prestop and hasparticipants in interface

* add prestop function to existing routers

* fakerouter prestop

* update protocol version

* read lock

* redis router graceful stop

* test fix

* force stop
This commit is contained in:
Mathew Kamkar
2021-09-15 13:07:44 -07:00
committed by GitHub
parent 20d0c0ca43
commit 700a879c0b
18 changed files with 56 additions and 18 deletions
+1
View File
@@ -62,6 +62,7 @@ type Router interface {
OnRTCMessage(callback RTCMessageCallback)
Start() error
PreStop()
Stop()
}
+4
View File
@@ -132,6 +132,10 @@ func (r *LocalRouter) Start() error {
return nil
}
func (r *LocalRouter) PreStop() {
r.currentNode.State = livekit.NodeState_SHUTTING_DOWN
}
func (r *LocalRouter) Stop() {
r.rtcMessageChan.Close()
}
+1
View File
@@ -28,6 +28,7 @@ func NewLocalNode(conf *config.Config) (LocalNode, error) {
Id: fmt.Sprintf("%s%s", utils.NodePrefix, HashedID(hostname)[:8]),
Ip: conf.RTC.NodeIP,
NumCpus: uint32(runtime.NumCPU()),
State: livekit.NodeState_SERVING,
Stats: &livekit.NodeStats{
StartedAt: time.Now().Unix(),
UpdatedAt: time.Now().Unix(),
+5
View File
@@ -258,6 +258,11 @@ func (r *RedisRouter) Start() error {
}
}
func (r *RedisRouter) PreStop() {
r.currentNode.State = livekit.NodeState_SHUTTING_DOWN
r.RegisterNode()
}
func (r *RedisRouter) Stop() {
if !r.isStarted.TrySet(false) {
return
+2
View File
@@ -782,6 +782,8 @@ func (fake *FakeRouter) Stop() {
}
}
func (fake *FakeRouter) PreStop() {}
func (fake *FakeRouter) StopCallCount() int {
fake.stopMutex.RLock()
defer fake.stopMutex.RUnlock()
+2
View File
@@ -12,6 +12,7 @@ import (
var (
nodeLoadLow = &livekit.Node{
State: livekit.NodeState_SERVING,
Stats: &livekit.NodeStats{
UpdatedAt: time.Now().Unix(),
NumCpus: 1,
@@ -20,6 +21,7 @@ var (
}
nodeLoadHigh = &livekit.Node{
State: livekit.NodeState_SERVING,
Stats: &livekit.NodeStats{
UpdatedAt: time.Now().Unix(),
NumCpus: 1,
+1 -1
View File
@@ -18,7 +18,7 @@ func IsAvailable(node *livekit.Node) bool {
func GetAvailableNodes(nodes []*livekit.Node) []*livekit.Node {
return funk.Filter(nodes, func(node *livekit.Node) bool {
return IsAvailable(node)
return IsAvailable(node) && node.State == livekit.NodeState_SERVING
}).([]*livekit.Node)
}
+1
View File
@@ -41,5 +41,6 @@ type RoomManager interface {
StartSession(ctx context.Context, roomName string, pi routing.ParticipantInit, requestSource routing.MessageSource, responseSink routing.MessageSink)
CleanupRooms() error
CloseIdleRooms()
HasParticipants() bool
Stop()
}
+12
View File
@@ -202,6 +202,18 @@ func (r *LocalRoomManager) CloseIdleRooms() {
}
}
func (r *LocalRoomManager) HasParticipants() bool {
r.lock.RLock()
defer r.lock.RUnlock()
for _, room := range r.rooms {
if len(room.GetParticipants()) != 0 {
return true
}
}
return false
}
func (r *LocalRoomManager) Stop() {
// disconnect all clients
r.lock.RLock()
+15 -6
View File
@@ -171,7 +171,7 @@ func (s *LivekitServer) Start() error {
logger.Infow("starting LiveKit server", values...)
if err := s.httpServer.Serve(ln); err != http.ErrServerClosed {
logger.Errorw("could not start server", err)
s.Stop()
s.Stop(true)
}
}()
@@ -184,10 +184,6 @@ func (s *LivekitServer) Start() error {
<-s.doneChan
if err := s.router.UnregisterNode(); err != nil {
logger.Errorw("could not unregister node", err)
}
// wait for shutdown
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
@@ -203,7 +199,20 @@ func (s *LivekitServer) Start() error {
return nil
}
func (s *LivekitServer) Stop() {
func (s *LivekitServer) Stop(force bool) {
// wait for all participants to exit
s.router.PreStop()
partTicker := time.NewTicker(5 * time.Second)
waitingForParticipants := !force && s.roomManager.HasParticipants()
for waitingForParticipants {
select {
case <-partTicker.C:
logger.Infow("waiting for participants to exit")
waitingForParticipants = s.roomManager.HasParticipants()
}
}
partTicker.Stop()
if !s.running.TrySet(false) {
return
}
+2 -1
View File
@@ -1,7 +1,8 @@
// Code generated by Wire. DO NOT EDIT.
//go:generate go run github.com/google/wire/cmd/wire
//+build !wireinject
//go:build !wireinject
// +build !wireinject
package service