Documentation
¶
Overview ¶
Package sessions maintains the in-memory active-session tracker updated by the session poll loop and snapshotted by the collector. The tracker exposes its mutex, map, and per-session fields so the remaining callers in package main can orchestrate lock scope without a wall of getter methods; when a caller outside this module needs a narrower contract a later cycle can add Snapshot / Apply helpers.
Exported symbols: State / StatePlaying / StateStopped, the SessionTimeout / StaleSessionTimeout / MaxSessionKeyLen / MaxTrackedSessions bounds, the Session DTO, Tracker with NewTracker, Update, Prune, and RunPruneLoop.
Index ¶
- Constants
- func SubtitleAction(ts *plexapi.WSTranscodeSession) string
- func TranscodeKind(ts *plexapi.WSTranscodeSession) string
- type PruneConfig
- type Session
- type State
- type Tracker
- func (t *Tracker) Prune()
- func (t *Tracker) RunPruneLoop(ctx context.Context)
- func (t *Tracker) SnapshotSessions() (sessions map[string]Session, kbits float64)
- func (t *Tracker) Update(id string, newState State, meta, mediaMeta *plexapi.SessionMetadata)
- func (t *Tracker) UpdateLibraryLabels(id string, fn func(*Session))
- func (t *Tracker) UpdateTranscode(transcodeKey, kind, subtitle string) bool
Constants ¶
const ( SessionTimeout = defaultSessionTimeout StaleSessionTimeout = defaultStaleSessionTimeout )
Exported aliases for test ergonomics (same-package tests reference these directly in session setup).
const ( MaxSessionKeyLen = 64 MaxTrackedSessions = 256 )
MaxSessionKeyLen and MaxTrackedSessions bound the session tracker against a compromised or buggy Plex server that streams unbounded distinct sessionKey values. SessionKey is used both as a map key and as a Prometheus label value, so unbounded growth would OOM the exporter and inflate Mimir's active-series count.
Variables ¶
This section is empty.
Functions ¶
func SubtitleAction ¶ added in v1.1.0
func SubtitleAction(ts *plexapi.WSTranscodeSession) string
SubtitleAction classifies a transcode session's subtitle handling. Return values are one of ValBurn, ValCopy, ValTranscode, ValNone, or "other".
func TranscodeKind ¶ added in v1.1.0
func TranscodeKind(ts *plexapi.WSTranscodeSession) string
TranscodeKind classifies a transcode session by audio/video decision and codec changes. Return values are one of ValVideo, ValAudio, ValBoth, or ValNone.
Types ¶
type PruneConfig ¶
type PruneConfig struct {
SessionTimeout time.Duration
StaleTimeout time.Duration
Interval time.Duration
}
PruneConfig holds prune timing parameters. Zero values mean "use package defaults".
type Session ¶
type Session struct {
PlayStarted time.Time
LastUpdate time.Time
TranscodeType string
TranscodeKey string
SubtitleAction string
LibName string
LibID string
LibType string
State State
Meta plexapi.SessionMetadata
MediaMeta plexapi.SessionMetadata
PrevPlayedTime time.Duration
}
Session is a single tracked Plex playback session. All fields are exported so callers (the Prometheus collector and the session poll handler in package server) can read and mutate the tracked state under the tracker's mutex without a wall of getter methods.
type State ¶
type State string
State is a normalised session playback state derived from the Plex /status/sessions Player.State string.
const ( StatePlaying State = "playing" StateStopped State = "stopped" StatePaused State = "paused" StateOther State = "other" )
State values observed from Plex session polling.
func ParseState ¶
ParseState maps a raw Plex wire-protocol state string to a typed State constant. Unknown values map to StateOther so the state machine handles them intentionally rather than silently.
type Tracker ¶
type Tracker struct {
Sessions map[string]Session
TotalEstimatedKBits float64
PruneConf PruneConfig
// contains filtered or unexported fields
}
Tracker is the in-memory active-session map. All lock-protected operations are encapsulated as methods; external callers must not access the mutex directly.
func NewTracker ¶
func NewTracker() *Tracker
NewTracker returns an empty session tracker ready for use.
func (*Tracker) Prune ¶
func (t *Tracker) Prune()
Prune reclaims stopped sessions past SessionTimeout and non-stopped sessions idle past StaleSessionTimeout. Safe to call concurrently with Update.
func (*Tracker) RunPruneLoop ¶
RunPruneLoop invokes Prune on a configurable interval until ctx is cancelled.
func (*Tracker) SnapshotSessions ¶
SnapshotSessions returns a copy of the sessions map and the current TotalEstimatedKBits under the tracker's lock.
func (*Tracker) Update ¶
func (t *Tracker) Update(id string, newState State, meta, mediaMeta *plexapi.SessionMetadata)
Update records a state transition for the given session key. The meta and mediaMeta arguments may be nil; when non-nil they replace the cached metadata on the tracked session. On a playing→non-playing transition with attached media, the elapsed play time is accumulated into TotalEstimatedKBits for the transmit-bytes estimator.
func (*Tracker) UpdateLibraryLabels ¶
UpdateLibraryLabels applies fn to the session identified by id under the tracker's lock. No-op if the session does not exist. If fn sets TranscodeKey, the transcode index is updated.
func (*Tracker) UpdateTranscode ¶
UpdateTranscode correlates a transcode update (by its path-prefixed key) with a tracked session via the transcode index, then applies the classification. Falls back to checking each session's TranscodeKey field and Part-URL match when the index misses. Returns true if a match was found.