* feat(nodepool): node groups, per-node caps, and local transcode fallback control Group co-located transcode and proxy nodes so transcoded streams are served by a proxy on the same host/LAN instead of bouncing across the internal network (fixes #93): - New nodepool.Planner is the single selection entry point: it picks the transcode node and its group's proxy together (round-robin within the group), replacing the independent ProxyPool.Pick/TranscodePool.Acquire calls scattered across the native and jellycompat handlers, and absorbs the duplicated soft-affinity pick logic. - A group is only eligible while all of its enabled members are healthy; ungrouped nodes keep the historical behavior. - New per-node max_jobs cap (transcodes for transcode nodes, streams for proxies; NULL = unlimited), enforced via health-reported job counts plus short-lived reservations that expire once fresher health data arrives. Proxy health now reports real stream counts, including HLS sessions via idle-expiry tracking. - New playback.local_transcode_fallback setting (default on) lets admins refuse API-server transcoding when no eligible node exists. - Health checks now publish updated node copies under the pool lock instead of mutating shared structs in place, fixing a data race. - Admin UI: group + cap fields on the node form, group/cap columns, and the new fallback toggle in playback settings. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(nodepool): proxy bandwidth measurement and egress caps Proxy nodes now measure their stream egress (rolling 60s average over everything under /stream) and report it via the health endpoint. A new per-proxy max_bandwidth_kbps cap lets the planner route new streams away from saturated proxies: - Admission combines the measured egress with the estimated bitrate of the new stream (transcode target bitrate, or source bitrate for direct play/remux) so a stream is only admitted where it fits. - Recently admitted streams are bridged as bandwidth reservations for the meter window, since the rolling average only converges on a new stream's rate gradually. - A group whose proxies lack bandwidth headroom is treated as full: its transcode nodes are skipped, same as the job cap. - Admin UI: per-proxy "Max Egress Bandwidth (Mbps)" field and a live egress column; manual health checks return the measured rate. Active streams are never interrupted - the cap only gates new admissions. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * perf(playback): trim node-mode time-to-stream-start Distributed playback paid several avoidable costs before the first frame that integrated mode doesn't have. This trims the safe ones: - Web player preconnects to the stream origin (the proxy node) as soon as /playback/start returns, overlapping DNS/TCP/TLS handshakes with the transcode dispatch instead of paying them at the first manifest fetch. - The transcode node no longer blocks its 202 on monitoring work: the Redis session-track write moves off the request path, and a replaced session's segment directory is renamed aside and deleted in the background instead of synchronously (RemoveAll of a long session can take seconds on slow disks during quality switches). - The proxy's node-facing HTTP client gets a tuned transport: a larger idle-connection pool (Go's default of 2 per host causes connection churn and TLS re-handshakes when many viewers stream through one proxy->node pair) and a response-header timeout so a hung transcode node can no longer hang client requests indefinitely. - jellycompat's remote transcode dispatch gains the same 10s timeout the native path has had; an unreachable node previously hung the compat manifest request until the OS gave up. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
87 lines
2.3 KiB
Go
87 lines
2.3 KiB
Go
package proxy
|
|
|
|
import (
|
|
"net/http"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// meterWindowSeconds is the averaging window for the egress rate. HLS clients
|
|
// fetch segments in bursts (especially when buffering ahead), so a window of
|
|
// this size smooths the spikes into something close to the steady-state
|
|
// stream rate. The planner's bandwidth reservation bridge matches this value.
|
|
const meterWindowSeconds = 60
|
|
|
|
// egressMeter measures outbound stream bytes as a rolling per-second ring,
|
|
// reporting the average rate over the window. Safe for concurrent use.
|
|
type egressMeter struct {
|
|
mu sync.Mutex
|
|
// One extra bucket so the current (partial) second never collides with
|
|
// the oldest second still inside the window.
|
|
buckets [meterWindowSeconds + 1]int64
|
|
stamps [meterWindowSeconds + 1]int64 // unix second each bucket holds
|
|
now func() time.Time
|
|
}
|
|
|
|
func newEgressMeter() *egressMeter {
|
|
return &egressMeter{now: time.Now}
|
|
}
|
|
|
|
// Add records n egressed bytes against the current second.
|
|
func (m *egressMeter) Add(n int64) {
|
|
if n <= 0 {
|
|
return
|
|
}
|
|
sec := m.now().Unix()
|
|
i := int(sec % int64(len(m.buckets)))
|
|
m.mu.Lock()
|
|
if m.stamps[i] != sec {
|
|
m.stamps[i] = sec
|
|
m.buckets[i] = 0
|
|
}
|
|
m.buckets[i] += n
|
|
m.mu.Unlock()
|
|
}
|
|
|
|
// RateKbps returns the average egress over the window in kilobits/s.
|
|
func (m *egressMeter) RateKbps() int {
|
|
sec := m.now().Unix()
|
|
var total int64
|
|
m.mu.Lock()
|
|
for i := range m.buckets {
|
|
if sec-m.stamps[i] < meterWindowSeconds {
|
|
total += m.buckets[i]
|
|
}
|
|
}
|
|
m.mu.Unlock()
|
|
return int(total * 8 / 1000 / meterWindowSeconds)
|
|
}
|
|
|
|
// meteredResponseWriter counts every byte written to the client.
|
|
// Embedding the interface intentionally hides optimizations like
|
|
// io.ReaderFrom so all writes flow through Write.
|
|
type meteredResponseWriter struct {
|
|
http.ResponseWriter
|
|
meter *egressMeter
|
|
}
|
|
|
|
func (w *meteredResponseWriter) Write(b []byte) (int, error) {
|
|
n, err := w.ResponseWriter.Write(b)
|
|
w.meter.Add(int64(n))
|
|
return n, err
|
|
}
|
|
|
|
func (w *meteredResponseWriter) Flush() {
|
|
if f, ok := w.ResponseWriter.(http.Flusher); ok {
|
|
f.Flush()
|
|
}
|
|
}
|
|
|
|
// meterEgress wraps stream handlers so their responses count toward the
|
|
// node's measured egress bandwidth.
|
|
func (s *Server) meterEgress(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
next.ServeHTTP(&meteredResponseWriter{ResponseWriter: w, meter: s.egress}, r)
|
|
})
|
|
}
|