Skip to content

Commit

Permalink
fix: defer read lock unlocking while searching playback sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyXiang committed Mar 11, 2022
1 parent 52d4597 commit 0572ec3
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions handler/plex.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,12 @@ func (c *PlexClient) getPlayerSession(playerIdentifier, ratingKey string) (sessi

func (c *PlexClient) searchPlayerSession(playerIdentifier, ratingKey string) *sessionData {
c.MulLock.RLock(lockKeySessions)
c.MulLock.RUnlock(lockKeySessions)
defer c.MulLock.RUnlock(lockKeySessions)

key := playerIdentifier + "-" + ratingKey
if session, ok := c.sessions[key]; ok {
return session
for _, session := range c.sessions {
if session.metadata.Player.MachineIdentifier == playerIdentifier && session.metadata.RatingKey == ratingKey {
return session
}
}
return nil
}
Expand All @@ -476,10 +477,9 @@ func (c *PlexClient) fetchPlayerSessions() {

keys := make(map[string]struct{}, len(sessions.MediaContainer.Metadata))
for _, session := range sessions.MediaContainer.Metadata {
key := session.Player.MachineIdentifier + "-" + session.RatingKey
keys[key] = emptyStruct
if _, ok := c.sessions[key]; !ok {
c.sessions[key] = &sessionData{
keys[session.SessionKey] = emptyStruct
if _, ok := c.sessions[session.SessionKey]; !ok {
c.sessions[session.SessionKey] = &sessionData{
metadata: session,
guids: nil,
status: sessionUnplayed,
Expand All @@ -488,6 +488,7 @@ func (c *PlexClient) fetchPlayerSessions() {
}
for key := range c.sessions {
if _, ok := keys[key]; !ok {
c.sessions[key] = nil
delete(c.sessions, key)
}
}
Expand Down

0 comments on commit 0572ec3

Please sign in to comment.