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
let src = Logs.Src.create "runtime"
let _minor = (Sys.word_size / 8 * 256) - 1
external reraise : exn -> 'a = "%reraise"
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 -> Bigstringaf.t -> off:int -> len:int -> int
val read_eof : t -> Bigstringaf.t -> off:int -> len:int -> int
val yield_reader : t -> (unit -> unit) -> unit
val next_write_operation :
t
-> [ `Write of Bigstringaf.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:(Bigstringaf.t -> off:int -> len:int -> int) -> int
val put : t -> fn:(Bigstringaf.t -> off:int -> len:int -> int) -> int
end = struct
type t = { mutable buffer: Bigstringaf.t; mutable off: int; mutable len: int }
let create size =
let buffer = Bigstringaf.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
Bigstringaf.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 Bigstringaf.length buf = t.len then begin
t.buffer <- Bigstringaf.create (2 * Bigstringaf.length buf);
Bigstringaf.blit buf ~src_off:t.off t.buffer ~dst_off:0 ~len:t.len
end;
let n = fn t.buffer ~off ~len:(Bigstringaf.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 -> Miou.yield ()
| Some None -> Miou.yield ()
| 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 buffer =
let bytes_read =
Buffer.put buffer ~fn:(fun bstr ~off:dst_off ~len ->
let len = min len _minor in
let buf = Bytes.create len in
try
let len' = Flow.read flow buf ~off:0 ~len in
Bigstringaf.blit_from_bytes buf ~src_off:0 bstr ~dst_off ~len:len';
len'
with exn -> Flow.close flow; reraise exn)
in
if bytes_read = 0 then `Eof else `Ok bytes_read
let rec split acc bstr off len =
if len <= _minor then List.rev (Bigstringaf.substring bstr ~off ~len :: acc)
else
let len' = min len _minor in
let str = Bigstringaf.substring bstr ~off ~len:len' in
split (str :: acc) bstr (off + len') (len - len')
let writev flow bstrs =
let =
List.map
(fun { Faraday.buffer; off; len } -> split [] buffer off len)
bstrs
in
let len = List.fold_left (fun a { Faraday.len; _ } -> a + len) 0 bstrs in
try
List.iter (List.iter (Flow.write flow)) strss;
`Ok len
with
| Closed_by_peer -> `Closed
| _exn -> Flow.close flow; `Closed
type t = {
tags: Logs.Tag.set
; conn: Runtime.t
; flow: Flow.t
; tasks: (unit -> unit) Queue.t
; buffer: Buffer.t
; 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 ->
Log.debug (fun m -> m ~tags:t.tags "+reader");
let fn =
match recv t.flow t.buffer with
| `Eof ->
Log.debug (fun m -> m ~tags:t.tags "+reader eof");
Runtime.read_eof t.conn
| `Ok len ->
Log.debug (fun m -> m ~tags:t.tags "+reader %d byte(s)" len);
Runtime.read t.conn
in
let _ = Buffer.get t.buffer ~fn in
protected ()
| `Yield ->
let k () =
Miou.Mutex.protect t.lock @@ fun () ->
Queue.push go t.tasks;
Miou.Condition.signal t.cond
in
Log.debug (fun m -> m ~tags:t.tags "+reader yield");
Runtime.yield_reader t.conn k
| `Close ->
shutdown t.flow `read;
t.stop := true;
Log.debug (fun m -> m ~tags:t.tags "+reader closed")
| `Upgrade ->
Log.debug (fun m -> m ~tags:t.tags "+reader upgrade");
ignore (Miou.Computation.try_return t.upgrade ())
and finally () =
Log.debug (fun m -> m ~tags:t.tags "+reader signals");
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 ->
let fn acc { Faraday.len; _ } = acc + len in
let len = List.fold_left fn 0 iovecs in
Log.debug (fun m -> m ~tags:t.tags "+write %d byte(s)" len);
writev t.flow iovecs |> Runtime.report_write_result t.conn;
protected ()
| `Yield ->
let k () =
Miou.Mutex.protect t.lock @@ fun () ->
Queue.push go t.tasks;
Miou.Condition.signal t.cond
in
Log.debug (fun m -> m ~tags:t.tags "+writer yield");
Runtime.yield_writer t.conn k
| `Close _ ->
shutdown t.flow `write;
t.stop := true;
Log.debug (fun m -> m ~tags:t.tags "+writer closed")
| `Upgrade ->
Log.debug (fun m -> m ~tags:t.tags "+writer upgrade");
ignore (Miou.Computation.try_return t.upgrade ())
and finally () =
Log.debug (fun m -> m ~tags:t.tags "+writer signals");
Miou.Mutex.protect t.lock @@ fun () -> Miou.Condition.signal t.cond
and go () = Fun.protect ~finally protected in
go
let report_exn tags error conn exn =
Log.err (fun m -> m ~tags "user's exception: %s" (Printexc.to_string exn));
if !error = false then begin
Runtime.report_exn conn exn;
error := true
end
let rec terminate tags error conn orphans =
match Miou.care orphans with
| None -> ()
| Some None ->
Miou.yield ();
terminate tags error conn orphans
| Some (Some prm) -> begin
match Miou.await prm with
| Ok () -> terminate tags error conn orphans
| Error exn ->
report_exn tags error conn exn;
terminate tags error conn orphans
end
let rec clean tags error conn orphans =
match Miou.care orphans with
| Some None | None -> Miou.yield ()
| Some (Some prm) -> begin
match Miou.await prm with
| Ok () -> clean tags error conn orphans
| Error exn ->
report_exn tags error conn exn;
clean tags error conn orphans
end
let run conn ?(tags = Logs.Tag.empty) ?(read_buffer_size = _minor) ?upgrade
flow =
let buffer = Buffer.create read_buffer_size in
let s_rd = ref false and s_wr = ref false and error = ref false in
let u_rd = Miou.Computation.create () in
let u_wr = Miou.Computation.create () in
let tasks = Queue.create () in
let lock = Miou.Mutex.create () in
let cond = Miou.Condition.create () in
let is_shutdown conn = Runtime.is_closed conn || (!s_rd && !s_wr) in
let runner () =
let rec go orphans =
clean tags error conn orphans;
let () =
Miou.Mutex.protect lock @@ fun () ->
if Queue.is_empty tasks && not (is_shutdown conn) then
Miou.Condition.wait cond lock
in
let seq = Queue.to_seq tasks in
let lst = List.of_seq seq in
Queue.clear tasks;
List.iter (fun fn -> ignore (Miou.async ~orphans fn)) lst;
if not (is_shutdown conn) then go orphans
else begin
Log.debug (fun m -> m ~tags "Connection closed");
let _ = Miou.Computation.try_cancel u_rd (Miou.Cancelled, empty_bt) in
let _ = Miou.Computation.try_cancel u_wr (Miou.Cancelled, empty_bt) in
()
end
in
let orphans = Miou.orphans () in
let finally () = terminate tags error conn orphans in
Fun.protect ~finally @@ fun () -> go orphans
in
let upgrade () =
let rd = Miou.Computation.await u_rd in
let wr = Miou.Computation.await u_wr 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");
s_rd := true;
shutdown flow `read;
s_wr := true;
shutdown flow `write;
assert (is_shutdown conn);
Miou.Condition.signal cond
in
Queue.push fn tasks
in
let rd =
let stop = s_rd and upgrade = u_rd in
reader { tags; conn; flow; tasks; buffer; stop; upgrade; lock; cond }
in
let wr =
let stop = s_wr and upgrade = u_wr in
writer { tags; conn; flow; tasks; buffer; stop; upgrade; lock; cond }
in
Queue.push rd tasks;
Queue.push wr tasks;
Queue.push upgrade tasks;
Miou.async runner
end