Skip to content

Commit

Permalink
Refactor to include new CacheAge (#22)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Gibson <cgibson@datto.com>
  • Loading branch information
equalsgibson and Chris Gibson authored Oct 19, 2023
1 parent 942c307 commit 807d4ee
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
10 changes: 10 additions & 0 deletions five9/internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,13 @@ func (cache *MemoryCacheInstance[Key, T]) GetLastUpdated() *time.Time {

return cache.lastUpdated
}

func (cache *MemoryCacheInstance[Key, T]) GetCacheAge() *time.Duration {
cache.mutex.Lock()
defer cache.mutex.Unlock()
if cache.lastUpdated != nil {
age := time.Since(*cache.lastUpdated)
return &age
}
return nil
}
6 changes: 4 additions & 2 deletions five9/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ type SupervisorService struct {
}

func (s *SupervisorService) getDomainUserInfoMap(ctx context.Context) (map[five9types.UserID]five9types.AgentInfo, error) {
if s.domainMetadataCache.agentInfoState.GetLastUpdated() != nil {
if time.Since(*s.domainMetadataCache.agentInfoState.GetLastUpdated()) < time.Hour {
timeLastUpdated := s.domainMetadataCache.agentInfoState.GetCacheAge()

if timeLastUpdated != nil {
if *timeLastUpdated < time.Hour {
return s.domainMetadataCache.agentInfoState.GetAll().Items, nil
}
}
Expand Down
1 change: 1 addition & 0 deletions five9/supervisor_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func (s *SupervisorService) read(ctx context.Context) error {
func (s *SupervisorService) resetCache() {
s.webSocketCache.agentState.Reset()
s.webSocketCache.timers.Reset()
s.domainMetadataCache.agentInfoState.Reset()

serviceReset := time.Now()
s.webSocketCache.timers.Update(five9types.EventIDPongReceived, &serviceReset)
Expand Down
12 changes: 2 additions & 10 deletions five9/supervisor_websocket_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func (s *SupervisorService) handleWebsocketMessage(messageBytes []byte) error {
MessageBytes: messageBytes,
}
}
eventReceivedTime := time.Now()
s.webSocketCache.timers.Update(message.Context.EventID, &eventReceivedTime)

switch message.Context.EventID {
case five9types.EventIDServerConnected:
Expand All @@ -58,10 +60,6 @@ func (s *SupervisorService) handlerPong(payload any) error {
return fmt.Errorf("payload not expected type")
}

pongReceived := time.Now()

s.webSocketCache.timers.Update(five9types.EventIDPongReceived, &pongReceived)

return nil
}

Expand Down Expand Up @@ -164,9 +162,6 @@ func (s *SupervisorService) handlerSupervisorStats(payload any) error {
}
}

statisticsReceivedTime := time.Now()
s.webSocketCache.timers.Update(five9types.EventIDSupervisorStats, &statisticsReceivedTime)

return nil
}

Expand All @@ -183,8 +178,5 @@ func (s *SupervisorService) handleAgentStateUpdate(eventData five9types.WebSocke
s.webSocketCache.agentState.Delete(removedData.ID)
}

incrementalUpdateComplete := time.Now()
s.webSocketCache.timers.Update(five9types.EventIDIncrementalStatsUpdate, &incrementalUpdateComplete)

return nil
}

0 comments on commit 807d4ee

Please sign in to comment.