/
subscription_manager.cr
344 lines (268 loc) · 9.33 KB
/
subscription_manager.cr
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
class PubRelay::SubscriptionManager
record Subscription,
domain : String,
inbox_url : URI,
follow_id : String,
follow_actor_id : String
record AcceptSent,
domain : String
record Unsubscription,
domain : String
record FollowSent,
domain : String,
inbox_url : URI,
following_id : String,
following_actor_id : String
record AcceptReceive,
domain : String
record Announce,
object : String,
source_domain : String
record Deliver,
message : String,
source_domain : String
include Earl::Artist(Subscription | AcceptSent | Unsubscription | FollowSent | AcceptReceive | Announce | Deliver)
enum State
Pending
Subscribed
Failed
Unsubscribed
Undefined
def transition?(new_state)
case self
when Pending
new_state.subscribed? || new_state.unsubscribed?
when Subscribed
new_state.unsubscribed? || new_state.failed?
when Failed
new_state.subscribed? || new_state.unsubscribed?
when Unsubscribed, Undefined
false
end
end
end
def initialize(
@relay_domain : String,
@private_key : OpenSSL::PKey::RSA,
@redis : Redis::PooledClient,
@stats : Stats
)
@workers = Set(DeliverWorker).new
@subscribed_workers = Set(DeliverWorker).new
load_subscriptions
end
private def load_subscriptions
@redis.keys(key_for("*")).each do |key|
key = key.as(String)
domain = key.lchop(key_for(""))
raise "BUG" if domain == key
inbox_url = @redis.hget(key, "inbox_url").not_nil!
inbox_url = URI.parse inbox_url
state = get_state(domain)
following_state = get_following_state(domain)
if state.failed?
transition_state(domain, :subscribed)
state = get_state(domain)
end
deliver_worker = DeliverWorker.new(
domain, inbox_url, @relay_domain, @private_key, @stats, self
)
@workers << deliver_worker
@subscribed_workers << deliver_worker if state.subscribed? && (following_state.undefined? || following_state.subscribed?)
end
log.info "Found #{@workers.size} subscriptions, #{@subscribed_workers.size} of which are subscribed"
end
def peers
@subscribed_workers.map(&.domain)
end
def call
@workers.each do |worker|
supervise worker
end
while message = receive?
call(message)
end
end
def call(subscription : Subscription)
log.info "Received subscription for #{subscription.domain}"
deliver_worker = DeliverWorker.new(
subscription.domain, subscription.inbox_url, @relay_domain, @private_key, @stats, self
)
@redis.hmset(key_for(subscription.domain), {
inbox_url: subscription.inbox_url,
follow_id: subscription.follow_id,
follow_actor_id: subscription.follow_actor_id,
state: State::Pending.to_s,
})
supervise deliver_worker
accept_activity = {
"@context": "https://www.w3.org/ns/activitystreams",
id: route_url("/actor#accepts/follows/#{UUID.random}"),
type: "Accept",
actor: route_url("/actor"),
object: {
id: subscription.follow_id,
type: "Follow",
actor: subscription.follow_actor_id,
object: route_url("/actor"),
},
}
counter = new_counter
delivery = DeliverWorker::Delivery.new(
accept_activity.to_json, @relay_domain, counter, accept: true
)
deliver_worker.send delivery
end
def call(following : FollowSent)
log.info "Send follow to #{following.domain}"
deliver_worker = DeliverWorker.new(
following.domain, following.inbox_url, @relay_domain, @private_key, @stats, self
)
@redis.hmset(key_for(following.domain), {
inbox_url: following.inbox_url,
following_id: following.following_id,
following_actor_id: following.following_actor_id,
following_state: State::Pending.to_s,
})
supervise deliver_worker
follow_activity = {
"@context": "https://www.w3.org/ns/activitystreams",
id: following.following_id,
type: "Follow",
actor: route_url("/actor"),
object: following.following_actor_id,
}
counter = new_counter
delivery = DeliverWorker::Delivery.new(
follow_activity.to_json, @relay_domain, counter, accept: false
)
deliver_worker.send delivery
end
def call(accept : AcceptReceive)
following_state = get_following_state(accept.domain)
raise "#{accept.domain}'s state as #{following_state}, not pending" unless following_state.pending?
transition_following_state(accept.domain, :subscribed)
worker = @workers.find { |worker| worker.domain == accept.domain }
raise "Worker not found" unless worker
state = get_state(accept.domain)
@subscribed_workers << worker if state.subscribed?
end
def call(accept : AcceptSent)
state = get_state(accept.domain)
raise "#{accept.domain}'s state as #{state}, not pending" unless state.pending?
transition_state(accept.domain, :subscribed)
worker = @workers.find { |worker| worker.domain == accept.domain }
raise "Worker not found" unless worker
following_state = get_following_state(accept.domain)
@subscribed_workers << worker if following_state.undefined? || following_state.subscribed?
end
def call(unsubscribe : Unsubscription)
return unless @redis.exists(key_for(unsubscribe.domain)) == 1
deliver_worker = @workers.find { |worker| worker.domain == unsubscribe.domain }
raise "Worker not found for unsubscribe" unless deliver_worker
@subscribed_workers.delete(deliver_worker)
transition_state(unsubscribe.domain, :unsubscribed)
@workers.delete(deliver_worker)
deliver_worker.stop
@redis.del(key_for(unsubscribe.domain))
@stats.send Stats::UnsubscribePayload.new(unsubscribe.domain)
end
def call(announce : Announce)
announce_activity = {
"@context": "https://www.w3.org/ns/activitystreams",
id: route_url("/actor#announce/#{UUID.random}"),
type: "Announce",
actor: route_url("/actor"),
object: announce.object,
to: [Activity::PUBLIC_COLLECTION],
published: Time.utc,
}
counter = new_counter
delivery = DeliverWorker::Delivery.new(announce_activity.to_json, announce.source_domain, counter, accept: false)
@subscribed_workers.each do |worker|
# TODO: checking then sending is a race condition with threads
worker.send(delivery)
end
end
def call(deliver : Deliver)
counter = new_counter
delivery = DeliverWorker::Delivery.new(deliver.message, deliver.source_domain, counter, accept: false)
@subscribed_workers.each do |worker|
# TODO: checking then sending is a race condition with threads
worker.send(delivery)
end
end
def fail_worker(worker)
@subscribed_workers.delete(worker)
transition_state(worker.domain, :failed)
worker.stop
end
@deliver_counter = 0
private def new_counter : Int32
counter = @deliver_counter += 1
@stats.send Stats::DeliveryCounterPayload.new(counter)
counter
end
private def transition_state(domain, new_state : State)
state = get_state(domain)
raise "Invalid transition for #{domain} (#{state} -> #{new_state})" unless state.transition? new_state
log.info "Transitioning #{domain} (#{state} -> #{new_state})"
@redis.hset(key_for(domain), "state", new_state.to_s)
end
private def transition_following_state(domain, new_state : State)
state = get_following_state(domain)
raise "Invalid transition for #{domain} (#{state} -> #{new_state})" unless state.transition? new_state
log.info "Transitioning #{domain} (#{state} -> #{new_state})"
@redis.hset(key_for(domain), "following_state", new_state.to_s)
end
private def get_state(domain) : State
state = @redis.hget(key_for(domain), "state")
state.nil? ? State::Unsubscribed : State.parse(state)
end
private def get_following_state(domain) : State
state = @redis.hget(key_for(domain), "following_state")
state.nil? ? State::Undefined : State.parse(state)
end
private def key_for(domain)
"relay:subscription:#{domain}"
end
private def supervise(deliver_worker)
@workers << deliver_worker
spawn do
log.error "Supervision started when subscription manager not running!" unless self.running?
while self.running? && deliver_worker.starting?
deliver_worker.start(link: self)
end
end
end
def trap(agent, exception)
return log.error("Trapped agent was not a DeliverWorker!") unless agent.is_a? DeliverWorker
# This is a worker unsubscribing
return if agent.stopping? && !@workers.includes?(agent)
if exception
Earl::Logger.error(agent, exception) if exception
log.error { "worker for #{agent.domain} crashed (#{exception.class.name})" }
else
log.error { "worker for #{agent.domain} exited early" } if self.running?
end
agent.recycle if self.running?
end
private def route_url(path)
"https://#{@relay_domain}#{path}"
end
def terminate
@workers.each do |agent|
agent.stop if agent.running?
end
end
def reset
old_workers = @workers
@workers = Set(DeliverWorker).new
@subscribed_workers = Set(DeliverWorker).new
load_subscriptions
old_workers.each do |worker|
worker.stop if worker.running?
end
end
end
require "./deliver_worker"