server

package
v1.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 27, 2026 License: GPL-2.0, GPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

Package server contains the plexServer orchestrator: the refresh loop, per-subsystem refresh methods, and the Prometheus Describe/Collect implementation that emits metrics from plexServer state.

Index

Constants

View Source
const SessionPollInterval = 5 * time.Second

SessionPollInterval is the interval between /status/sessions polls. Short enough (~5s) that the 60s tracker retention catches transient sessions between scrapes.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	LastItemsRefresh time.Time
	ErrorCounts      map[string]float64
	Client           *plex.Client
	Sessions         *sessions.Tracker
	ID               string
	Name             string
	Version          string
	Platform         string
	PlatformVersion  string
	Libraries        []library.Library
	HostCPU          float64
	HostMem          float64
	TransmitBytes    float64
	LastBandwidthAt  int
	ActiveTranscodes int

	HTTPReachable     bool
	SessionsReachable bool
	PlexPass          bool
	// contains filtered or unexported fields
}

Server is the Plex orchestrator. Fields are exported so that package main (Describe/Collect code) and tests can read and mutate them under mu without a wall of accessor methods. The whole internal/* tree is a single trust boundary.

func NewServer

func NewServer(client *plex.Client) *Server

NewServer returns an initialised Server for the given Plex HTTP client. LastBandwidthAt is seeded to "now" so the first bandwidth refresh only picks up samples produced after startup, matching legacy behaviour.

func (*Server) Collect

func (s *Server) Collect(ch chan<- prometheus.Metric)

Collect implements prometheus.Collector. It snapshots server state under s.mu (via Snapshot) and then emits metrics outside the lock so the collector never holds s.mu across a channel send.

func (*Server) Describe

func (s *Server) Describe(ch chan<- *prometheus.Desc)

Describe implements prometheus.Collector. The descriptor set is the single source of truth published in metrics.AllDescs so that Describe and Collect cannot drift.

func (*Server) RecordError

func (s *Server) RecordError(typ string)

RecordError increments the error counter for the given type. The type must be a member of metrics.ErrorTypes; unknown types are silently dropped to preserve the Prometheus cardinality bound.

func (*Server) Refresh

func (s *Server) Refresh(outerCtx context.Context) error

Refresh polls Plex for server identity, library list, host resources, and bandwidth. Intended to be called both from startup (to establish initial state) and from RunRefreshLoop on a ticker.

func (*Server) RefreshSessions added in v1.1.0

func (s *Server) RefreshSessions(ctx context.Context)

RefreshSessions fetches /status/sessions, applies each active session to the tracker, classifies transcode state inline (from the embedded TranscodeSession element), and fills library labels via /library/metadata/<ratingKey>.

func (*Server) RunRefreshLoop

func (s *Server) RunRefreshLoop(ctx context.Context)

RunRefreshLoop invokes Refresh on a 5-second ticker until ctx is cancelled. On failure it flips HTTPReachable to false and records a "refresh" error; on recovery it logs a single info-level line to keep log volume bounded. If a previous Refresh is still in-flight the tick is skipped to prevent redundant concurrent HTTP calls.

func (*Server) RunSessionPollLoop added in v1.1.0

func (s *Server) RunSessionPollLoop(ctx context.Context)

RunSessionPollLoop polls /status/sessions on a short interval, feeding the tracker with session state, transcode classification, and library labels. Replaces the former WebSocket event-driven architecture while keeping the tracker's accumulation/pruning/classification unchanged.

func (*Server) SetHTTPReachable

func (s *Server) SetHTTPReachable(v bool)

SetHTTPReachable atomically sets the HTTPReachable flag.

func (*Server) SetSessionsReachable added in v1.2.0

func (s *Server) SetSessionsReachable(v bool)

SetSessionsReachable atomically sets the SessionsReachable flag.

func (*Server) Snapshot

func (s *Server) Snapshot() Snapshot

Snapshot returns a consistent point-in-time copy of the server's metric-visible state. Callers emit Prometheus metrics from the snapshot so Collect never holds s.Mu across a channel send.

func (*Server) SnapshotLibraries

func (s *Server) SnapshotLibraries() []library.Library

SnapshotLibraries returns a copy of the current library list under the mutex.

type Snapshot

type Snapshot struct {
	ErrorCounts       map[string]float64
	PlatformVersion   string
	Name              string
	ID                string
	Version           string
	Platform          string
	PlexPass          string
	Libraries         []library.Library
	HostCPU           float64
	HostMem           float64
	TransmitBytes     float64
	ActiveTranscodes  int
	HTTPReachable     float64
	SessionsReachable float64
	Retries           float64
}

Snapshot is an immutable view of Server captured under s.Mu for metric emission. Keeping the snapshot/emit split tight keeps Collect's lock scope to a single block. PlexPass is stored as a string so the caller can emit it directly as a Prometheus label value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL