Re-issue tokens when clients initially connect. (#569)

This ensures if they are disconnected before token is refreshed, they
could reconnect back with a valid token.
This commit is contained in:
David Zhao
2022-03-25 23:55:19 -07:00
committed by GitHub
parent 51cf626a70
commit f2556483a3
2 changed files with 12 additions and 11 deletions

View File

@@ -99,7 +99,7 @@ func (r *RedisRouter) SetNodeForRoom(_ context.Context, roomName livekit.RoomNam
}
func (r *RedisRouter) ClearRoomState(_ context.Context, roomName livekit.RoomName) error {
if err := r.rc.HDel(r.ctx, NodeRoomKey, string(roomName)).Err(); err != nil {
if err := r.rc.HDel(context.Background(), NodeRoomKey, string(roomName)).Err(); err != nil {
return errors.Wrap(err, "could not clear room state")
}
return nil

View File

@@ -394,22 +394,23 @@ func (r *RoomManager) rtcSessionWorker(room *rtc.Room, participant types.LocalPa
participant.Identity(), participant.ID(),
)
lastTokenUpdate := time.Now()
// send first refresh for cases when client token is close to expiring
_ = r.refreshToken(participant)
tokenTicker := time.NewTicker(tokenRefreshInterval)
defer tokenTicker.Stop()
stateCheckTicker := time.NewTicker(time.Millisecond * 50)
defer stateCheckTicker.Stop()
for {
select {
case <-time.After(time.Millisecond * 50):
case <-stateCheckTicker.C:
// periodic check to ensure participant didn't become disconnected
if participant.State() == livekit.ParticipantInfo_DISCONNECTED {
return
}
if time.Since(lastTokenUpdate) > tokenRefreshInterval {
pLogger.Debugw("refreshing client token after interval")
// refresh token with the first API Key/secret pair
if err := r.refreshToken(participant); err != nil {
pLogger.Errorw("could not refresh token", err)
}
lastTokenUpdate = time.Now()
case <-tokenTicker.C:
// refresh token with the first API Key/secret pair
if err := r.refreshToken(participant); err != nil {
pLogger.Errorw("could not refresh token", err)
}
case obj := <-requestSource.ReadChan():
// In single node mode, the request source is directly tied to the signal message channel