Source file eliommod_gc.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
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
(** Garbage collection of services and session data *)
let section = Lwt_log.Section.make "eliom:gc"
open Eliom_lib
open Lwt
let servicesessiongcfrequency = ref (Some 1200.)
let datasessiongcfrequency = ref (Some 1200.)
let persistentsessiongcfrequency = ref (Some 86400.)
let set_servicesessiongcfrequency i = servicesessiongcfrequency := i
let set_datasessiongcfrequency i = datasessiongcfrequency := i
let get_servicesessiongcfrequency () = !servicesessiongcfrequency
let get_datasessiongcfrequency () = !datasessiongcfrequency
let set_persistentsessiongcfrequency i = persistentsessiongcfrequency := i
let get_persistentsessiongcfrequency () = !persistentsessiongcfrequency
let gc_timeouted_services now tables =
let rec aux t filename direltr thr =
thr >>= fun () ->
match !direltr with
| Eliom_common.Dir r ->
empty_one r >>= fun () ->
(match !r with
| Eliom_common.Vide -> (
match !t with
| Eliom_common.Vide -> ()
| Eliom_common.Table tr ->
let newr = String.Table.remove filename tr in
if String.Table.is_empty newr
then t := Eliom_common.Vide
else t := Eliom_common.Table newr)
| _ -> ());
Lwt.return_unit
| Eliom_common.File ptr ->
Eliom_common.Serv_Table.fold
(fun ptk (`Ptc (nodeopt, l)) thr ->
let%lwt _ = thr in
(match nodeopt, l with
| Some node, {Eliom_common.s_expire = Some (_, e); _} :: _
when !e < now ->
Ocsigen_cache.Dlist.remove node
| Some node, [] ->
Ocsigen_cache.Dlist.remove node
| _ -> (
try
let `Ptc (nodeopt, l), ll =
( Eliom_common.Serv_Table.find ptk !ptr
, Eliom_common.Serv_Table.remove ptk !ptr )
in
if nodeopt = None
then
match
List.fold_right
(fun ({Eliom_common.s_expire; _} as a) foll ->
match s_expire with
| Some (_, e) when !e < now -> foll
| _ -> a :: foll)
l []
with
| [] -> ptr := ll
| newl ->
ptr :=
Eliom_common.Serv_Table.add ptk
(`Ptc (nodeopt, newl))
ll
with Not_found -> ()));
Lwt.pause ())
!ptr return_unit
>>= fun () ->
(if Eliom_common.Serv_Table.is_empty !ptr
then
match !t with
| Eliom_common.Vide -> ()
| Eliom_common.Table tr ->
let newr = String.Table.remove filename tr in
if String.Table.is_empty newr
then t := Eliom_common.Vide
else t := Eliom_common.Table newr);
Lwt.return_unit
and empty_one t =
match !t with
| Eliom_common.Vide -> Lwt.return_unit
| Eliom_common.Table r -> (
if String.Table.is_empty r
then (
t := Eliom_common.Vide;
Lwt.return_unit)
else
String.Table.fold (aux t) r Lwt.return_unit >>= fun () ->
match !t with
| Eliom_common.Vide -> Lwt.return_unit
| Eliom_common.Table r ->
if String.Table.is_empty r then t := Eliom_common.Vide;
Lwt.return_unit)
in
Lwt_list.iter_s
(fun (_, _prio, t) -> empty_one t)
tables.Eliom_common.table_services
>>= fun () ->
tables.Eliom_common.table_services <-
List.filter
(fun r -> !(Tuple3.thd r) <> Eliom_common.Vide)
tables.Eliom_common.table_services;
Lwt.return_unit
let gc_timeouted_naservices now tr =
match !tr with
| Eliom_common.AVide -> return_unit
| Eliom_common.ATable t ->
if Eliom_common.NAserv_Table.is_empty t
then (
tr := Eliom_common.AVide;
Lwt.return_unit)
else
Eliom_common.NAserv_Table.fold
(fun k (_, _, expdate, _, nodeopt) thr ->
thr >>= fun () ->
(match expdate with
| Some (_, e) when !e < now -> (
match nodeopt with
| Some node ->
Ocsigen_cache.Dlist.remove node
| _ -> tr := Eliom_common.remove_naservice_table !tr k)
| _ -> ());
Lwt.pause ())
t return_unit
let service_session_gc sitedata =
let tables = sitedata.Eliom_common.global_services in
match get_servicesessiongcfrequency () with
| None -> ()
| Some t ->
let rec f () =
Lwt_unix.sleep t >>= fun () ->
let service_cookie_table = sitedata.Eliom_common.session_services in
let now = Unix.time () in
Lwt_log.ign_info ~section "GC of service sessions";
(if tables.Eliom_common.table_contains_services_with_timeout
then gc_timeouted_services now tables
else return_unit)
>>= fun () ->
(if tables.Eliom_common.table_contains_naservices_with_timeout
then gc_timeouted_naservices now tables.Eliom_common.table_naservices
else return_unit)
>>= fun () ->
Eliom_common.SessionCookies.fold
(fun k
{ Eliom_common.Service_cookie.session_table = tables
; expiry
; session_group
; session_group_node
; _ }
thr ->
thr >>= fun () ->
(match !expiry with
| Some exp when exp < now ->
Eliommod_sessiongroups.Serv.remove session_group_node;
Lwt.return_unit
| _ ->
(if tables.Eliom_common.table_contains_services_with_timeout
then gc_timeouted_services now tables
else return_unit)
>>= fun () ->
(if tables
.Eliom_common.table_contains_naservices_with_timeout
then
gc_timeouted_naservices now
tables.Eliom_common.table_naservices
else return_unit)
>>= fun () ->
(match !session_group with
| _, _scope, Right _
when Eliommod_sessiongroups.Serv.group_size
( sitedata.Eliom_common.site_dir_string
, `Client_process
, Left k )
= 0
&& Eliom_common.service_tables_are_empty tables ->
Eliommod_sessiongroups.Serv.remove session_group_node
| _ -> () );
return_unit)
>>= Lwt.pause)
service_cookie_table return_unit
>>= f
in
Lwt.async f
let data_session_gc sitedata =
match get_datasessiongcfrequency () with
| None -> ()
| Some t ->
let rec f () =
Lwt_unix.sleep t >>= fun () ->
let data_cookie_table = sitedata.Eliom_common.session_data in
let not_bound_in_data_tables =
sitedata.Eliom_common.not_bound_in_data_tables
in
let now = Unix.time () in
Lwt_log.ign_info ~section "GC of session data";
Eliom_common.SessionCookies.fold
(fun k
{ Eliom_common.Data_cookie.expiry
; session_group
; session_group_node
; _ }
thr ->
thr >>= fun () ->
(match !expiry with
| Some exp when exp < now ->
Eliommod_sessiongroups.Data.remove session_group_node;
return_unit
| _ -> (
match !session_group with
| _, scope, Right _
when Eliommod_sessiongroups.Data.group_size
( sitedata.Eliom_common.site_dir_string
, `Client_process
, Left k )
= 0
&& not_bound_in_data_tables k ->
if scope <> `Session
then
Lwt_log.ign_error ~section
"Eliom: Group associated to IP has scope different from `Session. Please report the problem.";
Eliommod_sessiongroups.Data.remove session_group_node;
Lwt.return_unit
| _ -> Lwt.return_unit))
>>= Lwt.pause)
data_cookie_table return_unit
>>= f
in
Lwt.async f
let persistent_session_gc sitedata =
let gc () =
let now = Unix.time () in
let log_hash c = Eliom_common.Hashed_cookies.(sha256 c) in
let do_gc_cookie cookie
{Eliommod_cookies.full_state_name; expiry; session_group; _}
=
let scope = full_state_name.Eliom_common.user_scope in
match expiry with
| Some exp when exp <= now ->
Lwt_log.ign_info_f ~section "remove expired cookie %s"
(log_hash cookie);
Eliommod_persess.close_persistent_state2 ~scope sitedata session_group
cookie
| _ ->
Lwt_log.ign_info_f ~section "cookie not expired: %s" (log_hash cookie);
return_unit
in
let gc_cookie c =
Lwt.try_bind
(fun () -> Eliommod_cookies.Persistent_cookies.Cookies.find c)
(do_gc_cookie c)
(function
| Not_found ->
Lwt_log.ign_info_f ~section "cookie does not exist: %s"
(log_hash c);
Lwt.return_unit
| exn -> Lwt.fail exn)
in
Lwt_log.ign_info ~section "GC of persistent sessions";
Eliommod_cookies.Persistent_cookies.garbage_collect ~section gc_cookie
in
match get_persistentsessiongcfrequency () with
| None -> ()
| Some t ->
let rec f () = Lwt_unix.sleep t >>= gc >>= f in
Lwt.async f