Source file durable_state_rpc.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
open Core
open Async_kernel
open Async_rpc_kernel
module Update = struct
type ('state, 'update, 'error, 'metadata) t =
| Attempting_new_connection
| Connection_success of 'metadata
| Lost_connection
| Failed_to_connect of Error.t
| Rpc_error of 'error
| Update of 'update
| State of 'state
[@@deriving sexp_of]
end
type ('state, 'update, 'error, 'metadata, 'connection) t =
{ updates_writer : ('state, 'update, 'error, 'metadata) Update.t Pipe.Writer.t
; connection : 'connection Durable.t
; resubscribe_delay : Time_ns.Span.t
; dispatch :
'connection
-> ('state * 'update Pipe.Reader.t * 'metadata, 'error) Result.t Or_error.t
Deferred.t
; time_source : Time_source.t
}
let subscription_active t = not (Pipe.is_closed t.updates_writer)
let write t update =
if subscription_active t then Pipe.write_without_pushback t.updates_writer update
;;
let try_to_get_fresh_pipe t =
write t Attempting_new_connection;
match%map Durable.with_ t.connection ~f:t.dispatch with
| Error err -> Error (`Failed_to_connect err)
| Ok result ->
(match result with
| Error e -> Error (`Rpc_error e)
| Ok result -> Ok result)
;;
let rec subscribe t =
if not (subscription_active t)
then return `Subscription_no_longer_active
else (
match%bind try_to_get_fresh_pipe t with
| Error err ->
(match err with
| `Failed_to_connect e -> write t (Failed_to_connect e)
| `Rpc_error e -> write t (Rpc_error e));
let%bind () = Time_source.after t.time_source t.resubscribe_delay in
subscribe t
| Ok (state, pipe, id) ->
write t (Connection_success id);
write t (State state);
return (`Ok pipe))
;;
let rec handle_update_pipe t deferred_pipe =
deferred_pipe
>>> function
| `Subscription_no_longer_active -> ()
| `Ok pipe ->
Pipe.transfer' pipe t.updates_writer ~f:(fun updates ->
return (Queue.map updates ~f:(fun update -> Update.Update update)))
>>> fun () ->
write t Lost_connection;
handle_update_pipe t (subscribe t)
;;
let create_internal
?(time_source = Time_source.wall_clock ())
connection
~dispatch
~resubscribe_delay
=
let updates_reader, updates_writer = Pipe.create () in
let resubscribe_delay =
Time_ns.Span.of_sec (Time_float.Span.to_sec resubscribe_delay)
in
let t = { updates_writer; connection; resubscribe_delay; dispatch; time_source } in
updates_reader, t
;;
let create' ?time_source connection ~dispatch ~resubscribe_delay =
let updates_reader, t =
create_internal ?time_source connection ~dispatch ~resubscribe_delay
in
handle_update_pipe t (subscribe t);
updates_reader
;;
let create_or_fail' ?time_source connection ~dispatch ~resubscribe_delay =
let updates_reader, t =
create_internal ?time_source connection ~dispatch ~resubscribe_delay
in
match%map try_to_get_fresh_pipe t with
| Error (`Failed_to_connect e) -> Error e
| Error (`Rpc_error e) -> Ok (Error e)
| Ok (new_state, fresh_pipe, id) ->
write t (Connection_success id);
write t (State new_state);
handle_update_pipe t (return (`Ok fresh_pipe));
Ok (Ok updates_reader)
;;
let create ?time_source connection rpc ~query ~resubscribe_delay =
let dispatch conn = Rpc.State_rpc.dispatch rpc conn query in
create' ?time_source connection ~dispatch ~resubscribe_delay
;;
let create_versioned
(type query state update error)
?time_source
connection
rpc_module
~(query : query)
~resubscribe_delay
=
let dispatch conn =
let module State_rpc =
(val rpc_module
: Versioned_rpc.Both_convert.State_rpc.S
with type caller_query = query
and type caller_state = state
and type caller_update = update
and type caller_error = error)
in
State_rpc.dispatch_multi conn query
in
create' ?time_source connection ~dispatch ~resubscribe_delay
;;
let create_versioned'
(type query state update error)
?time_source
connection
rpc_module
~(query : query)
~resubscribe_delay
=
let dispatch conn =
let module State_rpc =
(val rpc_module
: Versioned_rpc.Caller_converts.State_rpc.S
with type query = query
and type state = state
and type update = update
and type error = error)
in
State_rpc.dispatch_multi conn query
in
create' ?time_source connection ~dispatch ~resubscribe_delay
;;
let create_or_fail ?time_source connection rpc ~query ~resubscribe_delay =
let dispatch conn = Rpc.State_rpc.dispatch rpc conn query in
create_or_fail' ?time_source connection ~dispatch ~resubscribe_delay
;;
let create_or_fail_versioned
(type query state update error)
?time_source
connection
rpc_module
~(query : query)
~resubscribe_delay
=
let dispatch conn =
let module State_rpc =
(val rpc_module
: Versioned_rpc.Both_convert.State_rpc.S
with type caller_query = query
and type caller_state = state
and type caller_update = update
and type caller_error = error)
in
State_rpc.dispatch_multi conn query
in
create_or_fail' ?time_source connection ~dispatch ~resubscribe_delay
;;
let create_or_fail_versioned'
(type query state update error)
?time_source
connection
rpc_module
~(query : query)
~resubscribe_delay
=
let dispatch conn =
let module State_rpc =
(val rpc_module
: Versioned_rpc.Caller_converts.State_rpc.S
with type query = query
and type state = state
and type update = update
and type error = error)
in
State_rpc.dispatch_multi conn query
in
create_or_fail' ?time_source connection ~dispatch ~resubscribe_delay
;;