forked from SWI-Prolog/packages-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_redis_plugin.pl
332 lines (290 loc) · 11.6 KB
/
http_redis_plugin.pl
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
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2020-2024, SWI-Prolog Solutions b.v.
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.
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 OWNER 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(http_redis_plugin, []).
:- use_module(library(http/http_session)).
:- autoload(library(error), [must_be/2]).
:- autoload(library(lists), [member/2]).
:- autoload(library(redis), [redis/3, redis/2, redis_get_list/4, redis_zscan/4]).
:- autoload(library(broadcast), [broadcast/1]).
:- use_module(library(debug), [debug/3]).
:- autoload(library(socket), [ip_name/2]).
/** <module> Hook session management to use Redis
This module acts as a plugin for library(http/http_session), storing
session information on a Redis server. This has several consequences:
- The Prolog server may be restarted without loosing session data.
This is notably useful when long session timeouts are used.
- Multiple Prolog servers can act as a cluster while session
management is used.
- Associating Prolog data with sessions is relatively slow. The
assert/retract is replaced by managing a Redis list. Data in
this list is matched sequentially, where each term needs to be
parsed before it can be matched.
- Associated data is currently limited to __ground terms__.
The library is activated by loading it in addition to
library(http/http_session) and using http_set_session_options/1 to
configure the Redis database as below. The redis_server/2 predicate from
library(redis) can be used to specify the parameters for the redis
server such as host, port or authentication.
```
:- http_set_session_options(
[ redis_db(default),
redis_prefix('swipl:http:session')
]).
```
## Redis key usage
All Redis keys reside under a prefix specified by the option
redis_prefix(Prefix), which defaults to `'swipl:http:session'`. Here we
find:
- An ordered set at <prefix>:expire that contains the session ids,
ordered by the time the session expires. Session enumeration and
garbage collection is based on this set.
- A hash at <prefix>:session:<id> which contains the _peer_ address,
the _last used_ time and optionally session specific settings.
- If there is session _data_, a list at <prefix>:data:<id> of Prolog
terms, represented as strings that contain the session data.
*/
:- multifile
http_session:hooked/0,
http_session:hook/1,
http_session:session_setting/1,
http_session:session_option/2.
http_session:session_option(redis_db, atom).
http_session:session_option(redis_ro, atom).
http_session:session_option(redis_prefix, atom).
http_session:hooked :-
http_session:session_setting(redis_db(_)).
%http_session:hook(assert_session(SessionID, Peer)).
%http_session:hook(set_session_option(SessionId, Setting)).
%http_session:hook(get_session_option(SessionId, Setting)).
%http_session:hook(active_session(SessionID, Peer, LastUsed)).
%http_session:hook(set_last_used(SessionID, Now, TimeOut)).
%http_session:hook(asserta(session_data(SessionId, Data))).
%http_session:hook(assertz(session_data(SessionId, Data))).
%http_session:hook(retract(session_data(SessionId, Data))).
%http_session:hook(retractall(session_data(SessionId, Data))).
%http_session:hook(session_data(SessionId, Data)).
%http_session:hook(current_session(SessionID, Data)).
%http_session:hook(close_session(?SessionID)).
%http_session:hook(gc_sessions).
:- dynamic
peer/2, % SessionID, Peer
last_used/2. % SessionID, Time
http_session:hook(assert_session(SessionID, Peer)) :-
session_db(rw, SessionID, DB, Key),
http_session:session_setting(timeout(Timeout)),
asserta(peer(SessionID, Peer)),
ip_name(Peer, PeerS),
get_time(Now),
redis(DB, hset(Key,
peer, PeerS,
last_used, Now)),
expire(SessionID, Timeout).
http_session:hook(set_session_option(SessionID, Setting)) :-
session_db(rw, SessionID, DB, Key),
Setting =.. [Name,Value],
redis(DB, hset(Key, Name, Value as prolog)),
( Setting = timeout(Timeout)
-> expire(SessionID, Timeout)
; true
).
http_session:hook(get_session_option(SessionID, Setting)) :-
session_db(ro, SessionID, DB, Key),
Setting =.. [Name,Value],
redis(DB, hget(Key, Name), Value).
http_session:hook(active_session(SessionID, Peer, LastUsed)) :-
( last_used(SessionID, LastUsed0),
peer(SessionID, Peer0)
-> LastUsed = LastUsed0,
Peer = Peer0
; session_db(ro, SessionID, DB, Key),
redis(DB, hget(Key, peer), PeerS),
ip_name(Peer, PeerS),
redis(DB, hget(Key, last_used), LastUsed as number),
update_session(SessionID, LastUsed, _, Peer)
).
http_session:hook(set_last_used(SessionID, Now, Timeout)) :-
http_session:session_setting(granularity(TimeGranularity)),
LastUsed is floor(Now/TimeGranularity)*TimeGranularity,
update_session(SessionID, LastUsed, Updated, _Peer),
( Updated == true
-> session_db(rw, SessionID, DB, Key),
redis(DB, hset(Key, last_used, Now)),
expire(SessionID, Timeout)
; true
).
http_session:hook(asserta(session_data(SessionID, Data))) :-
must_be(ground, Data),
session_data_db(rw, SessionID, DB, Key),
redis(DB, lpush(Key, Data as prolog)).
http_session:hook(assertz(session_data(SessionID, Data))) :-
must_be(ground, Data),
session_data_db(rw, SessionID, DB, Key),
redis(DB, rpush(Key, Data as prolog)).
http_session:hook(retract(session_data(SessionID, Data))) :-
session_data_db(rw, SessionID, DB, Key),
redis_get_list(DB, Key, 10, List),
member(Data, List),
redis(DB, lrem(Key, 1, Data as prolog)).
http_session:hook(retractall(session_data(SessionID, Data))) :-
forall(http_session:hook(retract(session_data(SessionID, Data))),
true).
http_session:hook(session_data(SessionID, Data)) :-
session_data_db(rw, SessionID, DB, Key),
redis_get_list(DB, Key, 10, List),
member(Data, List).
http_session:hook(current_session(SessionID, Data)) :-
session_db(ro, SessionID, DB, Key),
redis(DB, hget(Key, last_used), Time as number),
number(Time),
get_time(Now),
Idle is Now - Time,
( nonvar(Data)
-> ( Data = peer(Peer)
-> redis(DB, hget(Key, peer), PeerS),
ip_name(Peer, PeerS)
; Data = idle(Idle0)
-> Idle0 = Idle
; http_session:hook(session_data(SessionID, Data))
)
; ( Data = peer(Peer),
redis(DB, hget(Key, peer), PeerS),
ip_name(Peer, PeerS)
; Data = idle(Idle)
; non_reserved_property(Data),
http_session:hook(session_data(SessionID, Data))
)
).
http_session:hook(close_session(SessionID)) :-
gc_session(SessionID).
http_session:hook(gc_sessions) :-
gc_sessions.
non_reserved_property(P) :-
var(P),
!.
non_reserved_property(peer(_)) :- !, fail.
non_reserved_property(idle(_)) :- !, fail.
non_reserved_property(_).
%! update_session(+SessionID, ?LastUsed, -Updated, ?Peer) is det.
%
% Update cached last_used and peer notions.
update_session(SessionID, LastUsed, Updated, Peer) :-
transaction(update_session_(SessionID, LastUsed, Updated, Peer)).
update_session_(SessionID, LastUsed, Updated, Peer) :-
update_last_used(SessionID, Updated, LastUsed),
update_peer(SessionID, Peer).
update_last_used(SessionID, Updated, LastUsed), nonvar(LastUsed) =>
( last_used(SessionID, LastUsed)
-> true
; retractall(last_used(SessionID, _)),
asserta(last_used(SessionID, LastUsed)),
Updated = true
).
update_last_used(_, _, _) =>
true.
update_peer(SessionID, Peer), nonvar(Peer) =>
( peer(SessionID, Peer)
-> true
; retractall(peer(SessionID, _)),
asserta(peer(SessionID, Peer))
).
update_peer(_, _) =>
true.
/*******************************
* SCHEDULE TIMEOUT *
*******************************/
expire(SessionID, Timeout) :-
get_time(Now),
Time is integer(Now+Timeout),
session_expire_db(rw, DB, Key),
redis(DB, zadd(Key, Time, SessionID)).
gc_sessions :-
session_expire_db(ro, DB, Key),
session_expire_db(rw, DB, TMOKey),
get_time(Now),
End is integer(Now),
redis(DB, zrangebyscore(Key, "-inf", End), TimedOut as atom),
( member(SessionID, TimedOut),
redis(DB, zrem(TMOKey, SessionID), 1), % 0 if someone else deleted this
gc_session(SessionID),
fail
; true
).
%! gc_session(+SessionID) is semidet.
gc_session(_) :-
prolog_current_frame(Frame),
prolog_frame_attribute(Frame, parent, PFrame),
prolog_frame_attribute(PFrame, parent_goal, gc_session(_)),
!.
gc_session(SessionID) :-
debug(http_session(gc), 'GC session ~p', [SessionID]),
session_db(ro, SessionID, DB, SessionKey),
redis(DB, hget(SessionKey, peer), PeerS),
ip_name(Peer, PeerS),
broadcast(http_session(end(SessionID, Peer))),
session_db(rw, SessionID, DBw, SessionKey),
redis(DBw, del(SessionKey)),
session_data_db(rw, SessionID, DBw, DataKey),
redis(DBw, del(DataKey)),
retractall(peer(SessionID, _)),
retractall(last_used(SessionID, _)).
/*******************************
* UTIL *
*******************************/
%! session_db(+RW, ?SessionID, -DB, -Key) is det.
session_db(RW, SessionID, DB, Key) :-
nonvar(SessionID),
!,
redis_db(RW, DB),
key_prefix(Prefix),
atomics_to_string([Prefix,session,SessionID], :, Key).
session_db(RW, SessionID, DB, Key) :-
session_expire_db(RW, DB, TMOKey),
redis_zscan(DB, TMOKey, Pairs, []),
member(SessionIDS-_Timeout, Pairs),
atom_string(SessionID, SessionIDS),
key_prefix(Prefix),
atomics_to_string([Prefix,session,SessionID], :, Key).
session_expire_db(RW, DB, Key) :-
redis_db(RW, DB),
key_prefix(Prefix),
atomics_to_string([Prefix,expire], :, Key).
session_data_db(RW, SessionID, DB, Key) :-
redis_db(RW, DB),
key_prefix(Prefix),
atomics_to_string([Prefix,data,SessionID], :, Key).
key_prefix(Prefix) :-
http_session:session_setting(redis_prefix(Prefix)),
!.
key_prefix('swipl:http:sessions').
redis_db(ro, DB) :-
http_session:session_setting(redis_ro(DB0)),
!,
DB = DB0.
redis_db(_, DB) :-
http_session:session_setting(redis_db(DB)).