./ct_report/coverage/mod_bosh_mnesia.COVER.html

1 -module(mod_bosh_mnesia).
2
3 -behaviour(mod_bosh_backend).
4
5 %% mod_bosh_backend callbacks
6 -export([start/0,
7 create_session/1,
8 delete_session/1,
9 get_session/1,
10 get_sessions/0,
11 node_cleanup/1]).
12
13 -include("mod_bosh.hrl").
14
15 -spec start() -> any().
16 start() ->
17 76 mnesia:create_table(bosh_session,
18 [{ram_copies, [node()]},
19 {attributes, record_info(fields, bosh_session)}]),
20 76 mnesia:add_table_copy(bosh_session, node(), ram_copies).
21
22 %% The choice of the operation context here (transaction vs dirty,
23 %% see man on mnesia:activity/4 for description of contexts) and the deletion
24 %% in delete_session/1 below depends on the availability of a load balancer
25 %% capable of doing server/session affiliation.
26 %%
27 %% With affiliation, it suffices for this write to be synchronous, since the
28 %% client can issue no subsequent request without a session ID and the ID is
29 %% returned to the client only after the dirty synchronous write returns.
30 %% Other nodes in the cluster eventually will have the current view of the
31 %% database, possibly (significantly) later than the write returns. However,
32 %% the only node serving the client in question always operates on valid data.
33 %%
34 %% Without affiliation, each BOSH/HTTP request may be handled by a different
35 %% node in the cluster. Hence, we must guarantee that once the write
36 %% operation returns, all nodes in the cluster will have access to currently
37 %% valid data -- that's why a transaction is used instead of a dirty write.
38
39 -spec create_session(mod_bosh:session()) -> any().
40 create_session(#bosh_session{} = Session) ->
41 129 mnesia:sync_transaction(fun mnesia:write/1, [Session]).
42
43
44 -spec delete_session(mod_bosh:sid()) -> any().
45 delete_session(Sid) ->
46 129 mnesia:transaction(fun mnesia:delete/1, [{bosh_session, Sid}]).
47
48
49 -spec get_session(mod_bosh:sid()) -> [mod_bosh:session()].
50 get_session(Sid) ->
51 2039 mnesia:dirty_read(bosh_session, Sid).
52
53
54 -spec get_sessions() -> [mod_bosh:session()].
55 get_sessions() ->
56 122 mnesia:dirty_match_object(mnesia:table_info(bosh_session, wild_pattern)).
57
58 -spec node_cleanup(atom()) -> any().
59 node_cleanup(Node) ->
60
:-(
F = fun() ->
61
:-(
SIDs = mnesia:select(
62 bosh_session,
63 [{#bosh_session{sid = '$1', socket = '$2'},
64 [{'==', {node, '$2'}, Node}],
65 ['$1']}]),
66
:-(
lists:foreach(fun(SID) ->
67
:-(
mnesia:delete({bosh_session, SID})
68 end, SIDs)
69 end,
70
:-(
mnesia:async_dirty(F).
71
Line Hits Source