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
let src = Logs.Src.create "runtime"
let _minor = 16384 - 1
module Log = (val Logs.src_log src : Logs.LOG)
module Flow = Flow
module type S = sig
type t
val next_read_operation : t -> [ `Read | `Yield | `Close | `Upgrade ]
val read : t -> Bstr.t -> off:int -> len:int -> int
val read_eof : t -> Bstr.t -> off:int -> len:int -> int
val yield_reader : t -> (unit -> unit) -> unit
val next_write_operation :
t
-> [ `Write of Bstr.t Faraday.iovec list
| `Close of int
| `Yield
| `Upgrade ]
val report_write_result : t -> [ `Ok of int | `Closed ] -> unit
val yield_writer : t -> (unit -> unit) -> unit
val report_exn : t -> exn -> unit
val is_closed : t -> bool
end
module Buffer : sig
type t
val create : int -> t
val get : t -> fn:(Bstr.t -> off:int -> len:int -> int) -> int
val put : t -> fn:(Bstr.t -> off:int -> len:int -> int) -> int
end = struct
type t = { mutable buffer: Bstr.t; mutable off: int; mutable len: int }
let create size =
let buffer = Bstr.create size in
{ buffer; off= 0; len= 0 }
let compress t =
if t.len = 0 then begin
t.off <- 0;
t.len <- 0
end
else if t.off > 0 then begin
Bstr.blit t.buffer ~src_off:t.off t.buffer ~dst_off:0 ~len:t.len;
t.off <- 0
end
let get t ~fn =
let n = fn t.buffer ~off:t.off ~len:t.len in
t.off <- t.off + n;
t.len <- t.len - n;
if t.len = 0 then t.off <- 0;
n
let put t ~fn =
compress t;
let off = t.off + t.len in
let buf = t.buffer in
if Bstr.length buf = t.len then begin
t.buffer <- Bstr.create (2 * Bstr.length buf);
Bstr.blit buf ~src_off:t.off t.buffer ~dst_off:0 ~len:t.len
end;
let n = fn t.buffer ~off ~len:(Bstr.length t.buffer - off) in
t.len <- t.len + n;
n
end
let empty_bt = Printexc.get_callstack max_int
let rec terminate orphans =
match Miou.care orphans with
| None -> Miou.yield ()
| Some None -> Miou.yield (); terminate orphans
| Some (Some prm) -> (
match Miou.await prm with
| Ok () -> terminate orphans
| Error exn ->
Log.err (fun m ->
m "unexpected exception from an asynchronous task: %S"
(Printexc.to_string exn));
terminate orphans)
let rec clean orphans =
match Miou.care orphans with
| None | Some None -> ()
| Some (Some prm) ->
begin match Miou.await prm with
| Ok () -> clean orphans
| Error exn ->
Log.err (fun m ->
m "unexpected exception from an asynchronous task: %S"
(Printexc.to_string exn));
clean orphans
end
exception Closed_by_peer = Flow.Closed_by_peer
module Make (Flow : Flow.S) (Runtime : S) = struct
type conn = Runtime.t
type flow = Flow.t
let shutdown flow cmd =
try Flow.shutdown flow cmd
with exn ->
Log.err (fun m -> m "error when we shutdown: %S" (Printexc.to_string exn))
let recv flow read_buf buffer =
let bytes_read =
Buffer.put buffer ~fn:(fun bstr ~off:dst_off ~len ->
let len = min len _minor in
let len' = Flow.read flow read_buf ~off:0 ~len in
Bstr.blit_from_bytes read_buf ~src_off:0 bstr ~dst_off ~len:len';
len')
in
if bytes_read = 0 then `Eof else `Ok bytes_read
let writev flow write_buf bstrs =
let len = List.fold_left (fun a { Faraday.len; _ } -> a + len) 0 bstrs in
let fn { Faraday.buffer; off; len } =
let rec go src_off len =
if len > 0 then begin
let n = Int.min len _minor in
Bstr.blit_to_bytes buffer ~src_off write_buf ~dst_off:0 ~len:n;
Flow.write flow ~off:0 ~len:n (Bytes.unsafe_to_string write_buf);
go (src_off + n) (len - n)
end
in
go off len
in
try List.iter fn bstrs; `Ok len with
| Closed_by_peer -> `Closed
| _exn -> `Closed
type t = {
tags: Logs.Tag.set
; conn: Runtime.t
; flow: Flow.t
; tasks: (unit -> unit) Queue.t
; buffer: Buffer.t
; tmp: bytes
; stop: bool ref
; upgrade: unit Miou.Computation.t
; lock: Miou.Mutex.t
; cond: Miou.Condition.t
}
let reader t =
let rec protected () =
match Runtime.next_read_operation t.conn with
| `Read ->
let fn =
Log.debug (fun m -> m "+read reader");
match recv t.flow t.tmp t.buffer with
| `Eof ->
Log.debug (fun m -> m "the flow was closed by peer");
Runtime.read_eof t.conn
| `Ok len ->
Log.debug (fun m -> m "got %d byte(s) from the given flow" len);
Runtime.read t.conn
in
let _ = Buffer.get t.buffer ~fn in
protected ()
| `Yield ->
Log.debug (fun m -> m "+yield reader");
let k () =
Miou.Mutex.protect t.lock @@ fun () ->
Queue.push go t.tasks;
Miou.Condition.signal t.cond
in
Runtime.yield_reader t.conn k
| `Close ->
Log.debug (fun m -> m "+close reader");
shutdown t.flow `read;
t.stop := true
| `Upgrade -> ignore (Miou.Computation.try_return t.upgrade ())
and finally () =
Miou.Mutex.protect t.lock @@ fun () -> Miou.Condition.signal t.cond
and go () = Fun.protect ~finally protected in
go
let writer t =
let rec protected () =
match Runtime.next_write_operation t.conn with
| `Write iovecs ->
Log.debug (fun m -> m "+write writer");
writev t.flow t.tmp iovecs |> Runtime.report_write_result t.conn;
protected ()
| `Yield ->
Log.debug (fun m -> m "+yield writer");
let k () =
Miou.Mutex.protect t.lock @@ fun () ->
Queue.push go t.tasks;
Miou.Condition.signal t.cond
in
Runtime.yield_writer t.conn k
| `Close _ ->
Log.debug (fun m -> m "+close writer");
shutdown t.flow `write;
t.stop := true
| `Upgrade -> ignore (Miou.Computation.try_return t.upgrade ())
and finally () =
Miou.Mutex.protect t.lock @@ fun () -> Miou.Condition.signal t.cond
and go () = Fun.protect ~finally protected in
go
type g = {
tags: Logs.Tag.set
; conn: Runtime.t
; flow: Flow.t
; tasks: (unit -> unit) Queue.t
; buffer: Buffer.t
; rd_buf: bytes
; wr_buf: bytes
; rd_stop: bool ref
; wr_stop: bool ref
; errored: bool ref
; rd_resolver: unit Miou.Computation.t
; wr_resolver: unit Miou.Computation.t
; lock: Miou.Mutex.t
; cond: Miou.Condition.t
}
let report_exn g exn =
Log.err (fun m ->
m ~tags:g.tags "user's exception: %s" (Printexc.to_string exn));
if !(g.errored) = false then begin
Runtime.report_exn g.conn exn;
g.errored := true
end
let drain orphans =
let seq = Seq.of_dispenser @@ fun () -> Miou.take orphans in
let lst = List.of_seq seq in
List.iter Miou.cancel lst
let rec clean g orphans =
match Miou.care orphans with
| Some None | None -> ()
| Some (Some prm) ->
begin match Miou.await prm with
| Ok () -> clean g orphans
| Error exn ->
report_exn g exn;
g.rd_stop := true;
g.wr_stop := true;
clean g orphans
end
let to_reader g =
{
tags= g.tags
; conn= g.conn
; flow= g.flow
; tasks= g.tasks
; buffer= g.buffer
; tmp= g.rd_buf
; stop= g.rd_stop
; upgrade= g.rd_resolver
; lock= g.lock
; cond= g.cond
}
let to_writer g =
{
tags= g.tags
; conn= g.conn
; flow= g.flow
; tasks= g.tasks
; buffer= g.buffer
; tmp= g.wr_buf
; stop= g.wr_stop
; upgrade= g.wr_resolver
; lock= g.lock
; cond= g.cond
}
let global ~read_buffer_size ~tags conn flow =
let buffer = Buffer.create read_buffer_size in
let rd_buf = Bytes.create _minor in
let wr_buf = Bytes.create _minor in
let rd_stop = ref false in
let wr_stop = ref false in
let errored = ref false in
let rd_resolver = Miou.Computation.create () in
let wr_resolver = Miou.Computation.create () in
let tasks = Queue.create () in
let lock = Miou.Mutex.create () in
let cond = Miou.Condition.create () in
{
tags
; conn
; flow
; tasks
; buffer
; rd_buf
; wr_buf
; rd_stop
; wr_stop
; errored
; rd_resolver
; wr_resolver
; lock
; cond
}
let run conn ?(tags = Logs.Tag.empty) ?(read_buffer_size = _minor) ?upgrade
flow =
let g = global ~read_buffer_size ~tags conn flow in
let is_shutdown () = !(g.rd_stop) && !(g.wr_stop) in
let runner () =
let rec go orphans =
clean g orphans;
let () =
Miou.Mutex.protect g.lock @@ fun () ->
if Queue.is_empty g.tasks && not (is_shutdown ()) then
Miou.Condition.wait g.cond g.lock
in
let seq = Queue.to_seq g.tasks in
let lst = List.of_seq seq in
Queue.clear g.tasks;
Log.debug (fun m -> m "+%d task(s)" (List.length lst));
List.iter (fun fn -> ignore (Miou.async ~orphans fn)) lst;
if not (is_shutdown ()) then go orphans
else begin
Log.debug (fun m -> m ~tags "Connection closed");
let _ =
Miou.Computation.try_cancel g.rd_resolver (Miou.Cancelled, empty_bt)
in
let _ =
Miou.Computation.try_cancel g.wr_resolver (Miou.Cancelled, empty_bt)
in
if (not !(g.rd_stop)) || not !(g.wr_stop) then
shutdown flow `read_write
end
in
let orphans = Miou.orphans () in
let finally () = drain orphans in
Fun.protect ~finally @@ fun () ->
go orphans;
Log.debug (fun m -> m "Runtime terminated, drain tasks")
in
let upgrade () =
let rd = Miou.Computation.await g.rd_resolver in
let wr = Miou.Computation.await g.wr_resolver in
match (rd, wr, upgrade) with
| Error _, _, _ | _, Error _, _ -> ()
| _, _, None ->
Log.debug (fun m -> m ~tags "No handler for websocket was given");
Fmt.failwith "Upgrade unsupported"
| Ok (), Ok (), Some fn ->
let fn () =
fn flow;
Log.debug (fun m ->
m ~tags "Upgrade handler finished, shutdown the underlying flow");
shutdown flow `read;
shutdown flow `write;
g.rd_stop := true;
g.wr_stop := true;
Miou.Condition.signal g.cond
in
Queue.push fn g.tasks
in
let rd = reader (to_reader g) in
let wr = writer (to_writer g) in
Queue.push rd g.tasks;
Queue.push wr g.tasks;
Queue.push upgrade g.tasks;
Miou.async runner
end