Skip to content

Commit

Permalink
refactor: lock playback session by session key
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyXiang committed Mar 11, 2022
1 parent 0572ec3 commit c011d00
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions handler/plex.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,12 @@ func (c *PlexClient) IsTokenSet() bool {
return c.client.Token != ""
}

func (c *PlexClient) GetUser(token string) (user *plexUser) {
if user = c.searchUser(token); user != nil {
return
func (c *PlexClient) GetUser(token string) *plexUser {
if user := c.searchUser(token); user != nil {
return user
}
c.fetchUsers(token)
user = c.searchUser(token)
return
return c.searchUser(token)
}

func (c *PlexClient) searchUser(token string) *plexUser {
Expand Down Expand Up @@ -264,11 +263,11 @@ func (c *PlexClient) syncTimelineWithPlaxt(r *http.Request, user *plexUser) {
return
}

session := c.getPlayerSession(clientUuid, ratingKey)
sessionKey, session := c.getPlayerSession(clientUuid, ratingKey)
if session == nil || session.status == sessionWatched {
return
}
lockKey := fmt.Sprintf("plex:session:%d:%s:%s", user.Id, clientUuid, ratingKey)
lockKey := fmt.Sprintf("plex:session:%s", sessionKey)
c.MulLock.Lock(lockKey)
defer c.MulLock.Unlock(lockKey)

Expand Down Expand Up @@ -401,13 +400,12 @@ func (c *PlexClient) getServerIdentifier() string {
return *c.serverIdentifier
}

func (c *PlexClient) getLibrarySection(sectionKey string) (section *plex.Directory) {
if section = c.searchLibrarySection(sectionKey); section != nil {
return
func (c *PlexClient) getLibrarySection(sectionKey string) *plex.Directory {
if section := c.searchLibrarySection(sectionKey); section != nil {
return section
}
c.fetchLibrarySections()
section = c.searchLibrarySection(sectionKey)
return
return c.searchLibrarySection(sectionKey)
}

func (c *PlexClient) searchLibrarySection(sectionKey string) *plex.Directory {
Expand Down Expand Up @@ -440,25 +438,24 @@ func (c *PlexClient) fetchLibrarySections() {
}
}

func (c *PlexClient) getPlayerSession(playerIdentifier, ratingKey string) (session *sessionData) {
if session = c.searchPlayerSession(playerIdentifier, ratingKey); session != nil {
return session
func (c *PlexClient) getPlayerSession(playerIdentifier, ratingKey string) (string, *sessionData) {
if key, session := c.searchPlayerSession(playerIdentifier, ratingKey); session != nil {
return key, session
}
c.fetchPlayerSessions()
session = c.searchPlayerSession(playerIdentifier, ratingKey)
return
return c.searchPlayerSession(playerIdentifier, ratingKey)
}

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

for _, session := range c.sessions {
for key, session := range c.sessions {
if session.metadata.Player.MachineIdentifier == playerIdentifier && session.metadata.RatingKey == ratingKey {
return session
return key, session
}
}
return nil
return "", nil
}

func (c *PlexClient) fetchPlayerSessions() {
Expand Down

0 comments on commit c011d00

Please sign in to comment.