-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelay_cache.go
More file actions
375 lines (320 loc) · 9.4 KB
/
Copy pathrelay_cache.go
File metadata and controls
375 lines (320 loc) · 9.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package main
import (
"flag"
"fmt"
"io"
"net/http"
"os"
"strings"
"sync"
"sync/atomic"
"time"
)
// ---------- Shared Cache Flag Helpers ----------
// addCacheFlags registers --cache, --cache-max-entries, --cache-stats on a FlagSet.
// Returns pointers to the parsed values. Env vars: CACHE=1, CACHE_MAX_ENTRIES, CACHE_STATS.
func addCacheFlags(fs *flag.FlagSet) (enabled *bool, maxEntries *int, statsInterval *int) {
defaultCache := os.Getenv("CACHE") == "1"
enabled = fs.Bool("cache", defaultCache, "enable in-memory response cache")
defaultMaxEntries := 100
if v := os.Getenv("CACHE_MAX_ENTRIES"); v != "" {
if n, err := fmt.Sscanf(v, "%d", &defaultMaxEntries); n != 1 || err != nil {
defaultMaxEntries = 100
}
}
maxEntries = fs.Int("cache-max-entries", defaultMaxEntries, "max cached items")
defaultCacheStats := 0
if v := os.Getenv("CACHE_STATS"); v != "" {
fmt.Sscanf(v, "%d", &defaultCacheStats)
}
statsInterval = fs.Int("cache-stats", defaultCacheStats, "log cache stats every N seconds (0 = off)")
return
}
// startCacheGoroutines starts the sweep (30s) and optional stats logging goroutines.
// Call after creating the cache, before the main event loop.
func startCacheGoroutines(cache *hlsCache, statsInterval int, done <-chan struct{}) {
go hlsCacheStatsLoop(cache, time.Duration(statsInterval)*time.Second, done)
go func() {
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for {
select {
case <-done:
return
case <-ticker.C:
cache.sweep()
}
}
}()
}
// ---------- Singleflight (inline, zero deps) ----------
// singleflightCall represents an in-progress or completed call.
type singleflightCall struct {
wg sync.WaitGroup
val interface{}
err error
}
// singleflightGroup provides duplicate function call suppression.
// Multiple concurrent callers with the same key get the same result.
type singleflightGroup struct {
mu sync.Mutex
m map[string]*singleflightCall
}
// Do executes fn once for a given key, deduplicating concurrent calls.
// All callers with the same key block until the first completes.
func (g *singleflightGroup) Do(key string, fn func() (interface{}, error)) (interface{}, error) {
g.mu.Lock()
if g.m == nil {
g.m = make(map[string]*singleflightCall)
}
if c, ok := g.m[key]; ok {
g.mu.Unlock()
c.wg.Wait()
return c.val, c.err
}
c := &singleflightCall{}
c.wg.Add(1)
g.m[key] = c
g.mu.Unlock()
c.val, c.err = fn()
c.wg.Done()
g.mu.Lock()
delete(g.m, key)
g.mu.Unlock()
return c.val, c.err
}
// ---------- HLS Cache ----------
// hlsCacheEntry stores a cached response with expiration.
type hlsCacheEntry struct {
data []byte
contentType string
expires time.Time
accessTime atomic.Int64 // UnixNano for LRU eviction
}
// hlsCache is an in-memory cache for HLS stream content with
// singleflight deduplication and LRU eviction. Keyed by request path.
//
// TTLs follow the TLTV protocol spec (section 9.10):
// - Manifests (.m3u8): 1 second
// - Segments (.ts, .m4s, .mp4): 3600 seconds
type hlsCache struct {
mu sync.RWMutex
items map[string]*hlsCacheEntry
sf singleflightGroup
maxEntries int
maxItemKB int // max single item size in KB (0 = no limit)
now func() time.Time
// Stats (atomics — no lock needed on the hot path)
hits atomic.Int64
misses atomic.Int64
evicts atomic.Int64
}
const (
hlsCacheDefaultMax = 100
hlsCacheDefaultItemMax = 50 * 1024 // 50 MB in KB
hlsCacheManifestTTL = 1 * time.Second
hlsCacheSegmentTTL = 3600 * time.Second
)
// newHLSCache creates a cache bounded by maxEntries.
func newHLSCache(maxEntries int) *hlsCache {
if maxEntries <= 0 {
maxEntries = hlsCacheDefaultMax
}
return &hlsCache{
items: make(map[string]*hlsCacheEntry),
maxEntries: maxEntries,
maxItemKB: hlsCacheDefaultItemMax,
now: time.Now,
}
}
// hlsCacheTTL returns the protocol-recommended TTL for a given path.
// Manifests (.m3u8) and protocol documents (.json, .xml, no extension)
// use a short TTL (singleflight dedup without staleness).
// Segments (.ts, .m4s, etc.) use a long TTL (immutable by sequence number).
func hlsCacheTTL(path string) time.Duration {
if strings.HasSuffix(path, ".m3u8") {
return hlsCacheManifestTTL
}
if strings.HasSuffix(path, ".json") || strings.HasSuffix(path, ".xml") {
return hlsCacheManifestTTL
}
// Paths with no file extension are protocol documents (e.g. /tltv/v1/channels/{id}).
lastSlash := strings.LastIndex(path, "/")
lastDot := strings.LastIndex(path, ".")
if lastDot <= lastSlash {
return hlsCacheManifestTTL
}
return hlsCacheSegmentTTL
}
// get returns a cached entry if it exists and hasn't expired.
// Returns nil on miss or expiration.
func (c *hlsCache) get(key string) *hlsCacheEntry {
c.mu.RLock()
entry, ok := c.items[key]
if !ok {
c.mu.RUnlock()
return nil
}
now := c.now()
if now.After(entry.expires) {
c.mu.RUnlock()
c.mu.Lock()
if current, ok := c.items[key]; ok && current == entry && now.After(current.expires) {
delete(c.items, key)
}
c.mu.Unlock()
return nil
}
entry.accessTime.Store(now.UnixNano())
c.mu.RUnlock()
return entry
}
// set stores a cache entry, evicting LRU entries if at capacity.
func (c *hlsCache) set(key string, data []byte, contentType string, ttl time.Duration) {
// Don't cache items larger than the limit
if c.maxItemKB > 0 && len(data)/1024 > c.maxItemKB {
return
}
now := c.now()
entry := &hlsCacheEntry{
data: data,
contentType: contentType,
expires: now.Add(ttl),
}
entry.accessTime.Store(now.UnixNano())
c.mu.Lock()
defer c.mu.Unlock()
// If key already exists, replace
if _, ok := c.items[key]; ok {
c.items[key] = entry
return
}
// Evict LRU entries if at capacity
for len(c.items) >= c.maxEntries {
c.evictLRU()
}
c.items[key] = entry
}
// evictLRU removes the least-recently-accessed entry.
// Must be called with c.mu held.
func (c *hlsCache) evictLRU() {
var oldestKey string
var oldestTime time.Time
first := true
for k, v := range c.items {
at := time.Unix(0, v.accessTime.Load())
if first || at.Before(oldestTime) {
oldestKey = k
oldestTime = at
first = false
}
}
if !first {
delete(c.items, oldestKey)
c.evicts.Add(1)
}
}
// getOrFetch returns cached data or fetches via fn, deduplicating
// concurrent requests for the same key via singleflight.
// Only caches successful (non-error) results.
func (c *hlsCache) getOrFetch(key string, fn func() (*hlsCacheFetchResult, error)) ([]byte, string, bool, error) {
// Check cache first
if entry := c.get(key); entry != nil {
c.hits.Add(1)
return entry.data, entry.contentType, true, nil
}
c.misses.Add(1)
// Singleflight: N concurrent requests = 1 upstream fetch
result, err := c.sf.Do(key, func() (interface{}, error) {
return fn()
})
if err != nil {
return nil, "", false, err
}
fr := result.(*hlsCacheFetchResult)
// Cache the result with protocol-recommended TTL
ttl := hlsCacheTTL(key)
c.set(key, fr.data, fr.contentType, ttl)
return fr.data, fr.contentType, false, nil
}
// stats returns current cache statistics.
func (c *hlsCache) stats() (hits, misses, evicts int64, size int) {
c.mu.RLock()
size = len(c.items)
c.mu.RUnlock()
return c.hits.Load(), c.misses.Load(), c.evicts.Load(), size
}
// sweep removes expired entries. Called periodically by background goroutine.
func (c *hlsCache) sweep() {
now := c.now()
c.mu.Lock()
defer c.mu.Unlock()
for k, v := range c.items {
if now.After(v.expires) {
delete(c.items, k)
}
}
}
// hlsCacheFetchResult is the result of an upstream fetch for caching.
type hlsCacheFetchResult struct {
data []byte
contentType string
}
// hlsCacheMaxBody is the maximum upstream response body size for cached items (50 MB).
const hlsCacheMaxBody = 50 * 1024 * 1024
// hlsCacheFetchUpstream performs an HTTP GET and returns the response body + content-type.
// Only returns results for 200 OK responses. Limits body to hlsCacheMaxBody.
func hlsCacheFetchUpstream(client *http.Client, fetchURL string, r *http.Request) (*hlsCacheFetchResult, error) {
req, err := http.NewRequestWithContext(r.Context(), "GET", fetchURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "tltv-cli/"+version)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, &hlsCacheUpstreamError{status: resp.StatusCode}
}
data, err := io.ReadAll(io.LimitReader(resp.Body, hlsCacheMaxBody))
if err != nil {
return nil, err
}
ct := resp.Header.Get("Content-Type")
if ct == "" {
ct = "application/octet-stream"
}
return &hlsCacheFetchResult{data: data, contentType: ct}, nil
}
// hlsCacheUpstreamError represents a non-200 upstream response.
type hlsCacheUpstreamError struct {
status int
}
func (e *hlsCacheUpstreamError) Error() string {
return "upstream returned " + http.StatusText(e.status)
}
// hlsCacheStatsLoop logs cache statistics periodically.
func hlsCacheStatsLoop(cache *hlsCache, interval time.Duration, done <-chan struct{}) {
if interval <= 0 {
return
}
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-done:
return
case <-ticker.C:
hits, misses, evicts, size := cache.stats()
total := hits + misses
var hitRate float64
if total > 0 {
hitRate = float64(hits) / float64(total) * 100
}
logInfof("cache: %d items, %d hits, %d misses (%.1f%% hit rate), %d evictions",
size, hits, misses, hitRate, evicts)
}
}
}