Skip to content

Commit

Permalink
fix: unmarshal user info from cache correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyXiang committed Mar 10, 2022
1 parent 4be16b8 commit 2c538a2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
4 changes: 4 additions & 0 deletions handler/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ const (
contentTypeAny = "*/*"
contentTypeXml = "xml"

lockKeyFriends = "plex:friends"
lockKeySections = "plex:library:sections"
lockKeySessions = "plex:playback:sessions"

watchedThreshold = 90

webhookEventPlay = "media.play"
Expand Down
34 changes: 23 additions & 11 deletions handler/plex.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,9 @@ func (c *PlexClient) SubscribeToNotifications(events *plex.NotificationEvents, i
c.client.SubscribeToNotifications(events, interrupt, fn)
}

func (c *PlexClient) GetUser(token string) (user *plexUser) {
func (c *PlexClient) GetUser(token string) *plexUser {
if realUser, ok := c.friends[token]; ok {
user = &realUser
return
return &realUser
}

var err error
Expand All @@ -193,17 +192,22 @@ func (c *PlexClient) GetUser(token string) (user *plexUser) {

isCacheEnabled := redisClient != nil
if isCacheEnabled {
err = redisClient.Get(ctx, cacheKey).Scan(user)
var result plexUser
err = redisClient.Get(ctx, cacheKey).Scan(&result)
if err == nil {
c.friends[token] = *user
return
c.friends[token] = result
return &result
}
}

c.MulLock.Lock(lockKeyFriends)
defer c.MulLock.Unlock(lockKeyFriends)

response := c.GetSharedServers()
if response == nil {
return
return nil
}
var user *plexUser
for _, friend := range response.Friends {
realUser := plexUser{
Id: friend.UserId,
Expand All @@ -219,7 +223,7 @@ func (c *PlexClient) GetUser(token string) (user *plexUser) {
}
}
if user != nil {
return
return user
}

info := c.GetAccountInfo(token)
Expand All @@ -230,7 +234,7 @@ func (c *PlexClient) GetUser(token string) (user *plexUser) {
redisClient.Set(ctx, cacheKey, user, 0)
}
}
return
return user
}

func (c *PlexClient) GetSharedServers() *plex.SharedServersResponse {
Expand Down Expand Up @@ -447,8 +451,12 @@ func (c *PlexClient) getLibrarySection(sectionKey string) (isFound bool) {
return
}

c.MulLock.Lock(lockKeySections)
c.mu.RLock()
defer c.mu.RUnlock()
defer func() {
c.mu.RUnlock()
c.MulLock.Unlock(lockKeySections)
}()

sections, err := c.client.GetLibraries()
if err != nil {
Expand All @@ -474,8 +482,12 @@ func (c *PlexClient) getPlayerSession(playerIdentifier, ratingKey string) (sessi
}
}

c.MulLock.Lock(lockKeySessions)
c.mu.RLock()
defer c.mu.RUnlock()
defer func() {
c.mu.RUnlock()
c.MulLock.Unlock(lockKeySessions)
}()

sessions, err := c.client.GetSessions()
if err != nil {
Expand Down

0 comments on commit 2c538a2

Please sign in to comment.