package lwt-pipeline

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file pipeline.ml

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
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Nomadic Labs <contact@nomadic-labs.com>                *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

open Lwt.Infix

type limiter = {
  mutable current : int ;
  max : int ;
}
let with_limiter limiter f v =
  if limiter.current <= limiter.max then begin
    limiter.current <- limiter.current + 1 ;
    let p = f v in
    Lwt.on_termination p (fun () -> limiter.current <- limiter.current - 1) ;
    Some p
  end else
    None
let limiter = function
  | None -> { current = 0 ; max = max_int ; }
  | Some max -> { current = 0 ; max ; }


type ('a, 'b) step =
  | Async_p of ('a -> 'b Lwt.t)
  | Async_s of ('a -> 'b Lwt.t)
  | Sync of ('a -> 'b)
let sync f = Sync f
let async_p f = Async_p f
let async_s f = Async_s f

let all_ok = Sync (fun v -> Ok v)
let map_in_err m = function
  | Sync f ->
      Sync (function
          | Ok _ as ok -> f ok
          | Error e -> f (Error (m e)))
  | Async_s f ->
      Async_s (function
          | Ok _ as ok -> f ok
          | Error e -> f (Error (m e)))
  | Async_p f ->
      Async_p (function
          | Ok _ as ok -> f ok
          | Error e -> f (Error (m e)))
let map_out_err m = function
  | Sync f ->
      Sync (fun x -> match f x with
          | Ok _ as ok -> ok
          | Error e -> Error (m e))
  | Async_s f ->
      Async_s (fun x -> f x >>= function
        | Ok _ as ok -> Lwt.return ok
        | Error e -> Lwt.return_error (m e))
  | Async_p f ->
      Async_p (fun x -> f x >>= function
        | Ok _ as ok -> Lwt.return ok
        | Error e -> Lwt.return_error (m e))
let with_err = function
  | Async_s f -> Async_s (function | Ok v -> f v | Error e -> Lwt.return_error e)
  | Async_p f -> Async_p (function | Ok v -> f v | Error e -> Lwt.return_error e)
  | Sync f -> Sync (function | Ok v -> f v | Error e -> Error e)

let recover f =
  Sync (function | Ok v -> v | Error e -> f e)

let with_key = function
  | Async_s f ->
      Async_s (fun (key, a) -> f a >>= fun b -> Lwt.return (key, b))
  | Async_p f ->
      Async_p (fun (key, a) -> f a >>= fun b -> Lwt.return (key, b))
  | Sync f -> Sync (fun (key, a) -> let b = f a in (key, b))
let init_key = Sync (fun x -> (x, x))

type ('i, 'o) pipe =
  | Nil : ('x, 'x) pipe
  | Cons : ('a, 'input) step * ('input, 'output) pipe -> ('a, 'output) pipe
let nil : ('x, 'x) pipe = Nil
let cons
  : ('a, 'b) step -> ('b, 'c) pipe -> ('a, 'c) pipe
  = fun step pipe -> Cons (step, pipe)


(* Instantiated values: values with buffers attached *)
type ('i, 'o) istep =
  | ISync of { f : 'i -> 'o }
  | IAsync_s of {
      qin : 'i Queue.t ;
      f : 'i -> 'o Lwt.t ;
      vout : 'o Lwt.t option ref ;
    }
  | IAsync_p of {
      qin : 'i Queue.t ;
      f : 'i -> 'o Lwt.t ;
      qout : 'o Lwt.t Queue.t ;
    }

type (_, _) ipipe =
  | ICons : { step : ('a, 'b) istep ; pipe : ('b, 'c) ipipe } -> ('a, 'c) ipipe
  | IEnd : { q : 'i Queue.t } -> ('i, 'i) ipipe

let rec instantiate_pipe
  : type i o. (i, o) pipe -> (i, o) ipipe
  = function
    | Nil -> IEnd { q = Queue.create () }
    | Cons (Sync f, pipe) ->
        let step = ISync { f } in
        let pipe = instantiate_pipe pipe in
        ICons { step ; pipe }
    | Cons (Async_s f, pipe) ->
        let step = IAsync_s { qin = Queue.create () ; f ; vout = ref None } in
        let pipe = instantiate_pipe pipe in
        ICons { step ; pipe }
    | Cons (Async_p f, pipe) ->
        let step = IAsync_p { qin = Queue.create () ; f ; qout = Queue.create () } in
        let pipe = instantiate_pipe pipe in
        ICons { step ; pipe }

let cancel_istep
  : ('i, 'o) istep -> unit
  = function
    | ISync _ -> ()
    | IAsync_s { vout = { contents = None } ; _ } -> ()
    | IAsync_s { vout = { contents = Some p } ; _ } -> Lwt.cancel p
    | IAsync_p { qout ; _ } -> Queue.iter Lwt.cancel qout

let rec cancel_ipipe
  : type i o . (i, o) ipipe -> unit
  = function
    | ICons { step ; pipe } ->
        cancel_istep step ;
        cancel_ipipe pipe
    | IEnd _ -> ()



let wait_for p = (p >>= fun _ -> Lwt.return_unit)

let rec progress_async_s limiter waiters qin f vout data_out =
  match !vout with
  | None -> begin
      if Queue.is_empty qin then
        (data_out, waiters)
      else
        match with_limiter limiter f @@ Queue.peek qin with
        | Some p ->
            ignore (Queue.pop qin) ;
            vout := Some p ;
            (data_out, wait_for p :: waiters)
        | None ->
            (data_out, waiters)
    end
  | Some p -> begin
      match Lwt.state p with
      | Lwt.Sleep ->
          (data_out, wait_for p :: waiters)
      | Lwt.Return v ->
          vout := None ;
          progress_async_s limiter waiters qin f vout (v :: data_out)
      | Lwt.Fail exc ->
          vout := None ;
          raise exc
    end

let rec make_promises limiter qin f qout =
  if Queue.is_empty qin then
    ()
  else
    match with_limiter limiter f @@ Queue.peek qin with
    | Some p ->
        ignore (Queue.pop qin) ;
        Queue.push p qout ;
        make_promises limiter qin f qout
    | None ->
        ()
let rec get_resolved_top waiters qout resolved =
  if Queue.is_empty qout then
    (List.rev resolved, waiters)
  else
    let p = Queue.peek qout in
    match Lwt.state p with
    | Lwt.Sleep -> (List.rev resolved, wait_for p :: waiters)
    | Lwt.Return v ->
        ignore (Queue.pop qout) ;
        get_resolved_top waiters qout (v :: resolved)
    | Lwt.Fail exc ->
        ignore (Queue.pop qout) ;
        raise exc

let progress_async_p limiter waiters qin f qout =
  make_promises limiter qin f qout ;
  get_resolved_top waiters qout []


let rec progress
  : type i o . limiter -> i list -> unit Lwt.t list -> (i, o) ipipe -> unit Lwt.t option
  = fun limiter data waiters pipe ->
    match pipe with
    | ICons { step = ISync { f } ; pipe } ->
        progress limiter (List.map f data) waiters pipe
    | ICons { step = IAsync_s { qin ; f ; vout } ; pipe } ->
        List.iter (fun v -> Queue.push v qin) data ;
        let (data_out, waiters) = progress_async_s limiter waiters qin f vout [] in
        progress limiter data_out waiters pipe
    | ICons { step = IAsync_p { qin ; f ; qout } ; pipe } ->
        List.iter (fun v -> Queue.push v qin) data ;
        let (data_out, waiters) = progress_async_p limiter waiters qin f qout in
        progress limiter data_out waiters pipe
    | IEnd { q } ->
        List.iter (fun v -> Queue.push v q) data ;
        match waiters with
        | [] -> None
        | _ :: _ -> Some (Lwt.join waiters)


let rec get_result
  : type i o . (i, o) ipipe -> o list
  = function
    | ICons { step = ISync _ ; pipe } ->
        get_result pipe
    | ICons { step = IAsync_s { vout ; _ } ; pipe } ->
        assert (!vout = None) ;
        get_result pipe
    | ICons { step = IAsync_p { qout ; _ } ; pipe } ->
        assert (Queue.is_empty qout) ;
        get_result pipe
    | IEnd { q } ->
        let rec mk_list acc =
          if Queue.is_empty q then
            List.rev acc
          else
            mk_list (Queue.pop q :: acc)
        in
        mk_list []

let instantiate
  : ('input, 'output) pipe -> ('input, 'output) ipipe
  = fun pipe ->
    instantiate_pipe pipe

let run
  : limiter -> ('input, 'output) ipipe -> 'input list -> 'output list Lwt.t
  = fun limiter ipipe input ->
    let rec loop pipe =
      match progress limiter [] [] pipe with
      | Some wait ->
          wait >>= fun () ->
          loop pipe
      | None ->
          Lwt.return (get_result ipipe)
    in
    match progress limiter input [] ipipe with
    | None ->
        Lwt.return (get_result ipipe)
    | Some wait ->
        wait >>= fun () ->
        loop ipipe

let rec separate vs errs = function
  | [] -> (List.rev vs, List.rev errs)
  | (Ok v) :: res -> separate (v :: vs) errs res
  | (Error err) :: res -> separate vs (err :: errs) res
let partition_by_error res = separate [] [] res

let index_by_key vs empty add =
  List.fold_left
    (fun m (k, v) -> add k v m)
    empty
    vs

let run ?pool pipe input =
  let limiter = limiter pool in
  let ipipe = instantiate pipe in
  let p = run limiter ipipe input in
  Lwt.on_cancel p (fun () -> cancel_ipipe ipipe) ;
  p
OCaml

Innovation. Community. Security.