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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
module StreamsTbl = struct
include Hashtbl.MakeSeeded (struct
type t = Stream_identifier.t
let equal = Stream_identifier.( === )
let hash i k = Hashtbl.seeded_hash i k
end)
let[@inline] find_opt h key = try Some (find h key) with Not_found -> None
end
module type StreamDescriptor = sig
type t
val id : t -> Stream_identifier.t
val requires_output : t -> bool
val flush_write_body : t -> max_bytes:int -> int
val finish_stream : t -> Stream.closed_reason -> unit
val is_idle : t -> bool
end
module Make (Streamd : StreamDescriptor) = struct
module rec PriorityTreeNode : sig
type root = Root
type nonroot = NonRoot
type stream = nonroot node
and parent = Parent : _ node -> parent
and _ node =
| Connection :
{ all_streams : stream StreamsTbl.t
; mutable t_last : int
; mutable children : PriorityQueue.t
; mutable flow : Settings.WindowSize.t
; mutable inflow : Settings.WindowSize.t
; mutable marked_for_removal :
(Stream_identifier.t * Stream.closed) list
}
-> root node
| Stream :
{ descriptor : Streamd.t
; mutable t_last : int
; mutable t : int
; mutable priority : Priority.t
; mutable parent : parent
; mutable children : PriorityQueue.t
; mutable flow : Settings.WindowSize.t
; mutable inflow : Settings.WindowSize.t
}
-> nonroot node
end =
PriorityTreeNode
and PriorityQueue :
(Psq.S with type k = Int32.t and type p = PriorityTreeNode.stream) =
Psq.Make
(Int32)
(struct
include PriorityTreeNode
type t = stream
let compare (Stream { t = t1; _ }) (Stream { t = t2; _ }) =
compare t1 t2
end)
include PriorityTreeNode
type t = root node
let make_root ?(capacity = 65536) () =
Connection
{ t_last = 0
; children = PriorityQueue.empty
; all_streams = StreamsTbl.create ~random:true capacity
; flow = Settings.WindowSize.default_initial_window_size
; inflow = Settings.WindowSize.default_initial_window_size
; marked_for_removal = []
}
let create
~parent ~initial_send_window_size ~initial_recv_window_size descriptor
=
Stream
{ descriptor
; t_last = 0
; t =
0
; priority = Priority.default_priority
; parent
; children = PriorityQueue.empty
; flow = initial_send_window_size
; inflow = initial_recv_window_size
}
let pq_add stream_id node pq = PriorityQueue.add stream_id node pq
let remove_from_parent (Parent parent) id =
match parent with
| Connection root ->
root.children <- PriorityQueue.remove id root.children
| Stream stream ->
stream.children <- PriorityQueue.remove id stream.children
let children : type a. a node -> PriorityQueue.t = function
| Stream { children; _ } ->
children
| Connection { children; _ } ->
children
let stream_id : type a. a node -> int32 = function
| Connection _ ->
Stream_identifier.connection
| Stream { descriptor; _ } ->
Streamd.id descriptor
let set_parent stream_node ~exclusive new_parent =
let (Stream ({ descriptor; _ } as stream)) = stream_node in
let (Parent new_parent_node) = new_parent in
let stream_id = Streamd.id descriptor in
remove_from_parent stream.parent stream_id;
stream.parent <- new_parent;
let new_children =
let new_children = children new_parent_node in
if exclusive then (
stream.children <-
PriorityQueue.fold
(fun k (Stream p as p_node) pq ->
p.parent <- Parent stream_node;
PriorityQueue.add k p_node pq)
stream.children
new_children;
PriorityQueue.sg stream_id stream_node)
else
pq_add stream_id stream_node new_children
in
match new_parent_node with
| Stream stream ->
stream.children <- new_children
| Connection root ->
root.children <- new_children
let would_create_cycle ~new_parent (Stream { descriptor; _ }) =
let rec inner : type a. a node -> bool = function
| Connection _ ->
false
| Stream { parent = Parent parent; _ }
when Stream_identifier.(stream_id parent === Streamd.id descriptor) ->
true
| Stream { parent = Parent parent; _ } ->
inner parent
in
let (Parent parent_node) = new_parent in
inner parent_node
let reprioritize_stream (Connection root as t) ~priority stream_node =
let (Stream stream) = stream_node in
let new_parent, new_priority =
if Stream_identifier.is_connection priority.Priority.stream_dependency
then
Parent t, priority
else
match
StreamsTbl.find_opt root.all_streams priority.stream_dependency
with
| Some parent_stream ->
Parent parent_stream, priority
| None ->
Parent t, Priority.default_priority
in
if not (Priority.equal stream.priority new_priority) then (
let { Priority.stream_dependency; exclusive; _ } = new_priority in
let (Parent current_parent_node) = stream.parent in
let current_parent_id = stream_id current_parent_node in
if
(not Stream_identifier.(stream_dependency === current_parent_id))
|| exclusive <> stream.priority.exclusive
then (
let (Parent new_parent_node) = new_parent in
(match new_parent_node with
| Stream new_parent_stream ->
if would_create_cycle ~new_parent stream_node then (
set_parent new_parent_node ~exclusive:false stream.parent;
new_parent_stream.priority <-
{ new_parent_stream.priority with
stream_dependency = current_parent_id
})
| Connection _ ->
());
set_parent stream_node ~exclusive new_parent);
stream.priority <- priority)
let add
(Connection root as t)
~priority
~initial_send_window_size
~initial_recv_window_size
descriptor
=
let stream =
create
~parent:(Parent t)
~initial_send_window_size
~initial_recv_window_size
descriptor
in
let stream_id = Streamd.id descriptor in
StreamsTbl.add root.all_streams stream_id stream;
root.children <- pq_add stream_id stream root.children;
if priority != Priority.default_priority then
reprioritize_stream t ~priority stream;
stream
let get_node (Connection root) stream_id =
StreamsTbl.find_opt root.all_streams stream_id
let find t stream_id =
match get_node t stream_id with
| Some (Stream { descriptor; _ }) ->
Some descriptor
| None ->
None
let iter (Connection { all_streams; _ }) ~f =
StreamsTbl.iter (fun _id -> f) all_streams
let allowed_to_transmit (Connection root) (Stream stream) =
Int32.compare root.flow 0l > 0 && Int32.compare stream.flow 0l > 0
let allowed_to_receive (Connection root) (Stream stream) size =
size <= root.inflow && size <= stream.inflow
let write (Connection root as t) stream_node =
let (Stream ({ descriptor; _ } as stream)) = stream_node in
let allowed_bytes =
if allowed_to_transmit t stream_node then
min root.flow stream.flow
else
0l
in
let written =
Int32.of_int
@@ Streamd.flush_write_body
~max_bytes:(Int32.to_int allowed_bytes)
descriptor
in
root.flow <- Int32.sub root.flow written;
stream.flow <- Int32.sub stream.flow written;
Int32.to_int written
let update_t stream n =
let (Stream ({ parent = Parent parent; _ } as stream)) = stream in
let tlast_p =
match parent with
| Connection { t_last; _ } ->
t_last
| Stream { t_last; _ } ->
t_last
in
stream.t <- tlast_p + (n * 256 / stream.priority.weight)
let mark_for_removal (Connection root) id closed =
root.marked_for_removal <- (id, closed) :: root.marked_for_removal
let implicitly_close_idle_stream descriptor max_seen_ids =
let implicitly_close_stream descriptor =
if Streamd.is_idle descriptor then
Streamd.finish_stream descriptor Finished
in
let max_client_stream_id, max_pushed_stream_id = max_seen_ids in
let stream_id = Streamd.id descriptor in
if Stream_identifier.is_request stream_id then (
if stream_id < max_client_stream_id then
implicitly_close_stream descriptor)
else if stream_id < max_pushed_stream_id then
implicitly_close_stream descriptor
let flush t max_seen_ids =
let rec schedule : type a. a node -> int * bool = function
| Connection p ->
(match PriorityQueue.pop p.children with
| Some ((id, (Stream i as i_node)), children') ->
p.t_last <- i.t;
let written, subtree_is_active = schedule i_node in
if subtree_is_active then (
update_t i_node written;
p.children <- PriorityQueue.add id i_node children')
else (
implicitly_close_idle_stream i.descriptor max_seen_ids;
p.children <- children');
written, subtree_is_active
| None ->
0, false)
| Stream ({ descriptor; _ } as p) as p_node ->
if Streamd.requires_output descriptor then
let written = write t p_node in
written, Streamd.requires_output descriptor
else (
match PriorityQueue.pop p.children with
| Some ((id, (Stream i as i_node)), children') ->
p.t_last <- i.t;
let written, subtree_is_active = schedule i_node in
if subtree_is_active then (
update_t i_node written;
p.children <- PriorityQueue.add id i_node children')
else (
implicitly_close_idle_stream i.descriptor max_seen_ids;
p.children <- children');
written, subtree_is_active
| None ->
0, false)
in
let (Connection root) = t in
ignore (schedule t);
root.marked_for_removal <-
List.fold_left
(fun acc (id, closed) ->
if closed.Stream.ttl = 0 then (
StreamsTbl.remove root.all_streams id;
acc)
else (
closed.ttl <- closed.ttl - 1;
(id, closed) :: acc))
[]
root.marked_for_removal
let check_flow flow growth flow' =
Int32.compare flow' growth > 0 = (Int32.compare flow 0l > 0)
&& Int32.compare flow' Settings.WindowSize.max_window_size <= 0
let add_flow : type a. a node -> int32 -> bool =
fun t growth ->
match t with
| Connection ({ flow; _ } as root) ->
let flow' = Int32.add flow growth in
let valid_flow = check_flow flow growth flow' in
if valid_flow then root.flow <- flow';
valid_flow
| Stream ({ flow; _ } as stream) ->
let flow' = Int32.add flow growth in
let valid_flow = check_flow flow growth flow' in
if valid_flow then stream.flow <- flow';
valid_flow
let add_inflow : type a. a node -> int32 -> bool =
fun t growth ->
match t with
| Connection ({ inflow; _ } as root) ->
let inflow' = Int32.add inflow growth in
let valid_inflow = check_flow inflow growth inflow' in
if valid_inflow then root.inflow <- inflow';
valid_inflow
| Stream ({ inflow; _ } as stream) ->
let inflow' = Int32.add inflow growth in
let valid_inflow = check_flow inflow growth inflow' in
if valid_inflow then stream.inflow <- inflow';
valid_inflow
let deduct_inflow : type a. a node -> int32 -> unit =
fun t size ->
match t with
| Connection ({ inflow; _ } as root) ->
root.inflow <- Int32.sub inflow size
| Stream ({ inflow; _ } as stream) ->
stream.inflow <- Int32.sub inflow size
let pp_hum fmt t =
let rec pp_hum_inner level fmt t =
let pp_binding fmt (i, Stream { children; t; _ }) =
Format.fprintf
fmt
"\n%s%ld, %d -> [%a]"
(String.make (level * 2) ' ')
i
t
(pp_hum_inner (level + 1))
children
in
PriorityQueue.pp pp_binding fmt t
in
pp_hum_inner 0 fmt t
end