-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelay_registry.go
More file actions
261 lines (225 loc) · 6.83 KB
/
relay_registry.go
File metadata and controls
261 lines (225 loc) · 6.83 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
package main
import (
"sync"
"time"
)
// ---------- Types ----------
// relayRegisteredChannel is a channel being relayed with cached verified documents.
// Once placed in the registry map, must never be mutated. Uses immutable replacement.
type relayRegisteredChannel struct {
ChannelID string
Name string
Hints []string // upstream host:port sources
Metadata []byte // raw verified metadata JSON (served verbatim)
Guide []byte // raw verified guide JSON (served verbatim)
GuideEntries []guideEntry // parsed entries (for XMLTV generation)
StreamHint string // best upstream for stream proxying
LastVerified time.Time
}
// relayRegistry manages relayed channels and peer state.
// Thread-safe via sync.RWMutex with immutable replacement.
type relayRegistry struct {
mu sync.RWMutex
channels map[string]*relayRegisteredChannel // channelID -> channel
peers map[string]*peerEntry // channelID -> peer info (from gossip)
hostname string
gossipEnabled bool // include gossip-discovered peers in the peers response
maxPeers int
staleDays int
}
// ---------- Constructor ----------
func newRelayRegistry(hostname string, gossipEnabled bool, maxPeers, staleDays int) *relayRegistry {
if maxPeers <= 0 {
maxPeers = 100
}
if staleDays <= 0 {
staleDays = 7
}
return &relayRegistry{
channels: make(map[string]*relayRegisteredChannel),
peers: make(map[string]*peerEntry),
hostname: hostname,
gossipEnabled: gossipEnabled,
maxPeers: maxPeers,
staleDays: staleDays,
}
}
// ---------- Read Methods (RLock) ----------
// GetChannel returns a relayed channel by ID, or nil.
func (r *relayRegistry) GetChannel(id string) *relayRegisteredChannel {
r.mu.RLock()
defer r.mu.RUnlock()
return r.channels[id]
}
// ListChannels returns all relayed channels.
func (r *relayRegistry) ListChannels() []*relayRegisteredChannel {
r.mu.RLock()
defer r.mu.RUnlock()
result := make([]*relayRegisteredChannel, 0, len(r.channels))
for _, ch := range r.channels {
result = append(result, ch)
}
return result
}
// ChannelCount returns the number of relayed channels.
func (r *relayRegistry) ChannelCount() int {
r.mu.RLock()
defer r.mu.RUnlock()
return len(r.channels)
}
// ListPeers returns peer entries for the gossip exchange response.
// Always includes relayed channels with our hostname as hint.
// Gossip-discovered peers are only included when gossipEnabled is true.
// Applies staleness cutoff and max limit.
func (r *relayRegistry) ListPeers() []peerEntry {
r.mu.RLock()
defer r.mu.RUnlock()
cutoff := time.Now().Add(-time.Duration(r.staleDays) * 24 * time.Hour)
var result []peerEntry
// Our own relayed channels
for _, ch := range r.channels {
var hints []string
if r.hostname != "" {
hints = []string{r.hostname}
}
result = append(result, peerEntry{
ChannelID: ch.ChannelID,
Name: ch.Name,
Hints: hints,
LastSeen: ch.LastVerified,
})
}
// Gossip-discovered peers (only when --gossip is enabled)
if r.gossipEnabled {
for _, p := range r.peers {
if _, relaying := r.channels[p.ChannelID]; relaying {
continue // already included from our own channels
}
if p.LastSeen.Before(cutoff) {
continue // stale
}
result = append(result, *p)
}
}
// Apply max limit
if len(result) > r.maxPeers {
result = result[:r.maxPeers]
}
return result
}
// ListGossipPeers returns only gossip-discovered peers, excluding relayed channels.
// Used by the peers endpoint since own relayed channels are visible via /.well-known/tltv.
func (r *relayRegistry) ListGossipPeers() []peerEntry {
r.mu.RLock()
defer r.mu.RUnlock()
if !r.gossipEnabled {
return nil
}
cutoff := time.Now().Add(-time.Duration(r.staleDays) * 24 * time.Hour)
var result []peerEntry
for _, p := range r.peers {
if _, relaying := r.channels[p.ChannelID]; relaying {
continue
}
if p.LastSeen.Before(cutoff) {
continue
}
result = append(result, *p)
}
if len(result) > r.maxPeers {
result = result[:r.maxPeers]
}
return result
}
// ---------- Write Methods (Lock) ----------
// UpdateChannel adds or updates a relayed channel with verified metadata.
// The raw bytes are served verbatim; the doc is used for field extraction only.
func (r *relayRegistry) UpdateChannel(channelID string, raw []byte, doc map[string]interface{}, hints []string, streamHint ...string) {
r.mu.Lock()
defer r.mu.Unlock()
name := getString(doc, "name")
selectedStreamHint := ""
if len(streamHint) > 0 {
selectedStreamHint = streamHint[0]
}
if selectedStreamHint == "" && len(hints) > 0 {
selectedStreamHint = hints[0]
}
// Preserve existing guide if we have one
var guide []byte
var guideEntries []guideEntry
if old, ok := r.channels[channelID]; ok {
guide = old.Guide
guideEntries = old.GuideEntries
if selectedStreamHint == "" {
selectedStreamHint = old.StreamHint
}
}
hintsCopy := append([]string(nil), hints...)
r.channels[channelID] = &relayRegisteredChannel{
ChannelID: channelID,
Name: name,
Hints: hintsCopy,
Metadata: raw,
Guide: guide,
GuideEntries: guideEntries,
StreamHint: selectedStreamHint,
LastVerified: time.Now(),
}
}
// UpdateGuide updates the cached guide for a relayed channel.
func (r *relayRegistry) UpdateGuide(channelID string, raw []byte, entries []guideEntry) {
r.mu.Lock()
defer r.mu.Unlock()
old, ok := r.channels[channelID]
if !ok {
return
}
// Immutable replacement
updated := &relayRegisteredChannel{
ChannelID: old.ChannelID,
Name: old.Name,
Hints: append([]string(nil), old.Hints...),
Metadata: old.Metadata,
Guide: raw,
GuideEntries: entries,
StreamHint: old.StreamHint,
LastVerified: old.LastVerified,
}
r.channels[channelID] = updated
}
// RemoveChannel removes a channel from the relay.
func (r *relayRegistry) RemoveChannel(channelID string) {
r.mu.Lock()
defer r.mu.Unlock()
delete(r.channels, channelID)
}
// StoreMigration stores a migration document at the old channel's ID
// and removes the old channel from active relay.
func (r *relayRegistry) StoreMigration(channelID string, raw []byte) {
r.mu.Lock()
defer r.mu.Unlock()
// Store the migration doc so it can be served at the old endpoint
r.channels[channelID] = &relayRegisteredChannel{
ChannelID: channelID,
Name: "(migrated)",
Metadata: raw,
LastVerified: time.Now(),
}
}
// MergePeers adds validated peer entries from gossip exchange.
func (r *relayRegistry) MergePeers(peers []peerEntry) {
r.mu.Lock()
defer r.mu.Unlock()
for i := range peers {
p := peers[i]
r.peers[p.ChannelID] = &p
}
// Prune stale peers
cutoff := time.Now().Add(-time.Duration(r.staleDays) * 24 * time.Hour)
for id, p := range r.peers {
if p.LastSeen.Before(cutoff) {
delete(r.peers, id)
}
}
}