-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathruntime.ml
171 lines (158 loc) · 6.61 KB
/
runtime.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
(*----------------------------------------------------------------------------
* Copyright (c) 2018 António Nuno Monteiro
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*---------------------------------------------------------------------------*)
module type LambdaEvent = sig
type t
val of_yojson : Yojson.Safe.t -> (t, string) result
end
module type LambdaResponse = sig
type t
val to_yojson : t -> Yojson.Safe.t
end
module type LambdaRuntime = sig
type event
type response
val lambda : (event -> Context.t -> (response, string) result) -> unit
end
module Make (Event : LambdaEvent) (Response : LambdaResponse) = struct
type 'a runtime =
{ client : Client.t
; settings : Config.function_settings
; handler : Event.t -> Context.t -> ('a, string) result
; max_retries : int
}
let make ~handler ~max_retries ~settings client =
{ client; settings; max_retries; handler }
let rec get_next_event ?error runtime retries =
match error with
| Some err when retries > runtime.max_retries ->
(match Errors.request_id err with
| Some req_id -> Client.event_error runtime.client req_id err
| None -> Client.fail_init runtime.client err)
|> ignore;
(* These errors are not recoverable. Either we can't communicate with the
* runtime APIs or we cannot parse the event. panic to restart the
* environment. *)
failwith "Could not retrieve next event"
| _ ->
(match Client.next_event runtime.client with
| Ok (ev_data, invocation_ctx) ->
(match ev_data |> Yojson.Safe.from_string |> Event.of_yojson with
| Ok ev ->
let handler_ctx =
Context.make
~invoked_function_arn:invocation_ctx.invoked_function_arn
~aws_request_id:invocation_ctx.aws_request_id
~xray_trace_id:invocation_ctx.xray_trace_id
~client_context:invocation_ctx.client_context
~identity:invocation_ctx.identity
~deadline:invocation_ctx.deadline
runtime.settings
in
ev, handler_ctx
| Error err ->
Logs.err (fun m -> m "Could not parse event to type: %s" err);
let error =
Errors.make_runtime_error
~recoverable:true
~request_id:invocation_ctx.aws_request_id
(Printf.sprintf "Could not unserialize from JSON: %s" err)
in
get_next_event ~error runtime (retries + 1)
| exception _ ->
let error =
Errors.make_runtime_error
~recoverable:false
~request_id:invocation_ctx.aws_request_id
(Printf.sprintf "Could not parse event to type: %s" ev_data)
in
get_next_event ~error runtime (retries + 1))
| Error e -> get_next_event ~error:e runtime (retries + 1))
let invoke { handler; _ } event ctx =
try handler event ctx with
| exn ->
let backtrace = Printexc.get_backtrace () in
let exn_str = Printexc.to_string exn in
Error (Printf.sprintf "Handler raised: %s\n%s" exn_str backtrace)
let rec start ~sw env runtime =
let event, ctx = get_next_event runtime 0 in
let request_id = ctx.aws_request_id in
match invoke runtime event { invocation_context = ctx; sw; env } with
| Ok response ->
let response_json = Response.to_yojson response in
(match Client.event_response runtime.client request_id response_json with
| Ok _ -> start ~sw env runtime
| Error e ->
if not (Errors.is_recoverable e)
then (
let (_ : _ result) = Client.fail_init runtime.client e in
Logs.err (fun m ->
m "Could not send error response %s" (Errors.message e));
failwith "Could not send error response")
else start ~sw env runtime)
| Error msg ->
let handler_error = Errors.make_handler_error msg in
(match Client.event_error runtime.client request_id handler_error with
| Ok _ -> start ~sw env runtime
| Error e ->
if not (Errors.is_recoverable e)
then (
Logs.err (fun m ->
m "Could not send error response %s" (Errors.message e));
let (_ : _ result) = Client.fail_init runtime.client e in
failwith "Could not send error response")
else start ~sw env runtime)
let start_with_runtime_endpoint ~sw env handler function_config endpoint =
match Client.make ~sw env endpoint with
| Ok client ->
let runtime =
make ~max_retries:3 ~settings:function_config ~handler client
in
start ~sw env runtime
| Error e ->
failwith
(Format.asprintf "Could not start HTTP client: %a" Piaf.Error.pp_hum e)
let lambda handler =
match Config.get_runtime_api_endpoint () with
| Ok endpoint ->
(match Config.get_function_settings () with
| Ok function_config ->
Eio_main.run (fun env ->
Eio.Switch.run (fun sw ->
start_with_runtime_endpoint
~sw
env
handler
function_config
endpoint))
| Error msg -> failwith msg)
| Error msg -> failwith msg
end